Changeset 3 for trunk/Platform/Base


Ignore:
Timestamp:
Nov 22, 2014, 11:50:49 PM (10 years ago)
Author:
chronos
Message:
  • Added: Application is executed as Task by Scheduler. OS threads is used for scheduler for efficient execution.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Platform/Base/UPlatformBase.pas

    r2 r3  
    66
    77uses
    8   Classes, SysUtils, UFileSystem;
     8  Classes, SysUtils, UFileSystem, UKernel;
    99
    1010type
     11  { TCustomThread }
     12
     13  TCustomThread = class(TThread)
     14    Task: TTask;
     15    procedure Execute; override;
     16  end;
     17
     18  { TBaseTask }
     19
     20  TBaseTask = class(TTask)
     21    Thread: TCustomThread;
     22    constructor Create; override;
     23    destructor Destroy; override;
     24  end;
     25
     26
    1127  TBaseFileSystem = class(TFileSystem)
    1228
    1329  end;
    1430
     31  { TBaseScheduler }
     32
     33  TBaseScheduler = class(TScheduler)
     34    function AddTask(Name: string; EntryPoint: TEvent): TTask; override;
     35    constructor Create; override;
     36    procedure Start; override;
     37  end;
     38
    1539implementation
     40
     41{ TBaseTask }
     42
     43constructor TBaseTask.Create;
     44begin
     45  inherited;
     46  Thread := TCustomThread.Create(True);
     47  Thread.Task := Self;
     48end;
     49
     50destructor TBaseTask.Destroy;
     51begin
     52  Thread.Free;
     53  inherited Destroy;
     54end;
     55
     56{ TCustomThread }
     57
     58procedure TCustomThread.Execute;
     59begin
     60  Task.EntryPoint;
     61end;
     62
     63{ TBaseScheduler }
     64
     65function TBaseScheduler.AddTask(Name: string; EntryPoint: TEvent): TTask;
     66var
     67  Task: TBaseTask;
     68begin
     69  Task := TBaseTask(inherited AddTask(Name, EntryPoint));
     70  if Running then Task.Thread.Start;
     71end;
     72
     73constructor TBaseScheduler.Create;
     74begin
     75  inherited Create;
     76  TaskClass := TBaseTask;
     77end;
     78
     79procedure TBaseScheduler.Start;
     80var
     81  I: Integer;
     82begin
     83  inherited Start;
     84  for I := 0 to Tasks.Count - 1 do
     85    TBaseTask(Tasks[I]).Thread.Start;
     86end;
    1687
    1788end.
Note: See TracChangeset for help on using the changeset viewer.