1 | unit Kernel.Scheduler;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Kernel.List, Kernel.Classes;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TTask = class;
|
---|
10 | TScheduler = class;
|
---|
11 |
|
---|
12 | TTaskEvent = procedure(Task: TTask) of object;
|
---|
13 |
|
---|
14 | TTaskState = (tsStopped, tsRunning, tsSuspended, tsWaiting);
|
---|
15 |
|
---|
16 | TWaitReason = (wrSleep, wrMessage);
|
---|
17 |
|
---|
18 | { TTask }
|
---|
19 |
|
---|
20 | TTask = class(TNamedObject)
|
---|
21 | Scheduler: TScheduler;
|
---|
22 | State: TTaskState;
|
---|
23 | WaitReason: TWaitReason;
|
---|
24 | WakeUpTime: TDateTime;
|
---|
25 | Terminated: Boolean;
|
---|
26 | EntryPoint: TTaskEvent;
|
---|
27 | Messages: TObjectQueue; // TQueue<TIPMessage>
|
---|
28 | procedure WaitForMessage;
|
---|
29 | procedure Sleep(Duration: TDateTime);
|
---|
30 | constructor Create; virtual;
|
---|
31 | destructor Destroy; override;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | TTaskClass = class of TTask;
|
---|
35 |
|
---|
36 | { TScheduler }
|
---|
37 |
|
---|
38 | TScheduler = class
|
---|
39 | TaskClass: TTaskClass;
|
---|
40 | Tasks: TNamedObjects<TTask>;
|
---|
41 | Running: Boolean;
|
---|
42 | function GetCurrentTask: TTask; virtual;
|
---|
43 | procedure Start; virtual;
|
---|
44 | procedure Stop; virtual;
|
---|
45 | function AddTask(Name: string; EntryPoint: TTaskEvent): TTask; virtual;
|
---|
46 | procedure SwitchNext; virtual;
|
---|
47 | constructor Create; virtual;
|
---|
48 | destructor Destroy; override;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | TSchedulerClass = class of TScheduler;
|
---|
52 |
|
---|
53 |
|
---|
54 | implementation
|
---|
55 |
|
---|
56 | { TTask }
|
---|
57 |
|
---|
58 | procedure TTask.WaitForMessage;
|
---|
59 | begin
|
---|
60 | WaitReason := wrMessage;
|
---|
61 | State := tsWaiting;
|
---|
62 | Scheduler.SwitchNext;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TTask.Sleep(Duration: TDateTime);
|
---|
66 | begin
|
---|
67 | WakeUpTime := Now + Duration;
|
---|
68 | State := tsWaiting;
|
---|
69 | WaitReason := wrSleep;
|
---|
70 | Scheduler.SwitchNext;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | constructor TTask.Create;
|
---|
74 | begin
|
---|
75 | Messages := TObjectQueue.Create;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | destructor TTask.Destroy;
|
---|
79 | begin
|
---|
80 | FreeAndNil(Messages);
|
---|
81 | inherited;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | { TScheduler }
|
---|
85 |
|
---|
86 | function TScheduler.GetCurrentTask: TTask;
|
---|
87 | begin
|
---|
88 | Result := nil;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TScheduler.Start;
|
---|
92 | begin
|
---|
93 | Running := True;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TScheduler.Stop;
|
---|
97 | begin
|
---|
98 | Running := False;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | function TScheduler.AddTask(Name: string; EntryPoint: TTaskEvent): TTask;
|
---|
102 | var
|
---|
103 | NewTask: TTask;
|
---|
104 | begin
|
---|
105 | NewTask := TaskClass.Create;
|
---|
106 | NewTask.Scheduler := Self;
|
---|
107 | NewTask.Name := Name;
|
---|
108 | NewTask.EntryPoint := EntryPoint;
|
---|
109 | Tasks.Add(NewTask);
|
---|
110 | Result := NewTask;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TScheduler.SwitchNext;
|
---|
114 | begin
|
---|
115 |
|
---|
116 | end;
|
---|
117 |
|
---|
118 | constructor TScheduler.Create;
|
---|
119 | begin
|
---|
120 | TaskClass := TTask;
|
---|
121 | Tasks := TNamedObjects<TTask>.Create;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | destructor TScheduler.Destroy;
|
---|
125 | begin
|
---|
126 | if Running then Stop;
|
---|
127 | Tasks.Free;
|
---|
128 | inherited Destroy;
|
---|
129 | end;
|
---|
130 |
|
---|
131 |
|
---|
132 | end.
|
---|
133 |
|
---|