Changeset 3


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.
Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Apps/UClock.pas

    r2 r3  
    66
    77uses
    8   Classes, SysUtils, UAPI;
     8  Classes, SysUtils, UAPI, dateutils;
    99
    1010type
     
    2323procedure TAppClock.Run;
    2424begin
    25 
     25  while True do begin
     26    API.WriteText('test');
     27    API.Sleep(OneSecond);
     28  end;
    2629end;
    2730
  • trunk/ChronOS.lpi

    r2 r3  
    109109        <StackChecks Value="True"/>
    110110      </Checks>
    111       <VerifyObjMethodCallValidity Value="True"/>
    112111    </CodeGeneration>
    113112    <Linking>
  • trunk/ChronOS.lpr

    r2 r3  
    44
    55uses
    6   {$IFDEF UNIX}{$IFDEF UseCThreads}
     6  {$IFDEF UNIX}
     7  //{$IFDEF UseCThreads}
    78  cthreads,
    8   {$ENDIF}{$ENDIF}
     9  //{$ENDIF}
     10  {$ENDIF}
    911  Interfaces, // this includes the LCL widgetset
    1012  Forms, UFormMain, UKernel, UFileSystem, UClock, UMemoryManager, UAPI,
    11 UPlatformBase, UList;
     13  UPlatformBase, UList;
    1214
    1315{$R *.res}
  • 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.
  • trunk/UAPI.pas

    r2 r3  
    1414  TAPI = class
    1515    procedure WriteText(Text: string);
     16    procedure Sleep(Time: TDateTime);
    1617  end;
    1718
     
    3031
    3132procedure TAPI.WriteText(Text: string);
     33begin
     34
     35end;
     36
     37procedure TAPI.Sleep(Time: TDateTime);
    3238begin
    3339
  • trunk/UFileSystem.pas

    r2 r3  
    3333  end;
    3434
     35  TFileSystemClass = class of TFileSystem;
     36
    3537implementation
    3638
  • trunk/UFormMain.lfm

    r2 r3  
    11object Form1: TForm1
    2   Left = 819
    3   Height = 240
    4   Top = 429
    5   Width = 320
     2  Left = 565
     3  Height = 687
     4  Top = 259
     5  Width = 932
    66  Caption = 'Form1'
    77  OnCreate = FormCreate
  • trunk/UFormMain.pas

    r2 r3  
    2828
    2929uses
    30   UList, UAPI, UClock;
     30  UList, UAPI, UClock, UPlatformBase;
    3131
    3232{$R *.lfm}
     
    3939begin
    4040  Kernel := TKernel.Create;
     41  Kernel.SchedulerClass := TBaseScheduler;
     42  Kernel.FileSystemClass := TBaseFileSystem;
    4143
    4244  NewApp := TAppClock.Create;
  • trunk/UKernel.pas

    r2 r3  
    99
    1010type
     11  TEvent = procedure of object;
    1112
    1213  TTaskState = (tsStopped, tsRunning, tsSuspended);
    1314
    14   TTask = class
     15  { TTask }
     16
     17  TTask = class(TNamedObject)
    1518    Name: string;
    1619    State: TTaskState;
     20    EntryPoint: TEvent;
     21    constructor Create; virtual;
    1722  end;
    1823
    19   TTasks = class(TObjectList)
     24  TTaskClass = class of TTask;
    2025
     26  { TScheduler }
     27
     28  TScheduler = class
     29    TaskClass: TTaskClass;
     30    Tasks: TNamedObjectList; // TList<TTask>
     31    Running: Boolean;
     32    procedure Start; virtual;
     33    procedure Stop;
     34    function AddTask(Name: string; EntryPoint: TEvent): TTask; virtual;
     35    constructor Create; virtual;
     36    destructor Destroy; override;
    2137  end;
    2238
    23   TScheduler = class
    24     Tasks: TTasks;
    25   end;
     39  TSchedulerClass = class of TScheduler;
    2640
    2741  TDrive = class(TNamedObject)
     
    3448    Apps: TNamedObjectList; // TList<TApp>
    3549    Drives: TNamedObjectList; // TList<TDrive>
    36     procedure Execute(AFile: TFile);
     50    Scheduler: TScheduler;
     51    SchedulerClass: TSchedulerClass;
     52    FileSystemClass: TFileSystemClass;
     53    procedure AppExecute(AFile: TFile);
    3754    procedure Init;
    3855    procedure Run;
     
    4360implementation
    4461
    45 uses
    46   UPlatformBase;
     62{ TTask }
     63
     64constructor TTask.Create;
     65begin
     66
     67end;
     68
     69{ TScheduler }
     70
     71procedure TScheduler.Start;
     72begin
     73  Running := True;
     74end;
     75
     76procedure TScheduler.Stop;
     77begin
     78  Running := False;
     79end;
     80
     81function TScheduler.AddTask(Name: string; EntryPoint: TEvent): TTask;
     82var
     83  NewTask: TTask;
     84begin
     85  NewTask := TaskClass.Create;
     86  NewTask.Name := Name;
     87  NewTask.EntryPoint := EntryPoint;
     88  Tasks.Add(NewTask);
     89  Result := NewTask;
     90end;
     91
     92constructor TScheduler.Create;
     93begin
     94  TaskClass := TTask;
     95  Tasks := TNamedObjectList.Create;
     96end;
     97
     98destructor TScheduler.Destroy;
     99begin
     100  if Running then Stop;
     101  Tasks.Free;
     102  inherited Destroy;
     103end;
    47104
    48105{ TKernel }
    49106
    50 procedure TKernel.Execute(AFile: TFile);
     107procedure TKernel.AppExecute(AFile: TFile);
    51108var
    52109  App: TApp;
    53110begin
    54111  App := TApp(Apps.FindByName(AFile.Name));
    55   if Assigned(App) then App.Run
    56     else raise Exception.Create('Application ''' + AFile.Name + ''' no found');
     112  if Assigned(App) then begin
     113    Scheduler.AddTask(App.Name, App.Run);
     114  end else raise Exception.Create('Application ''' + AFile.Name + ''' no found');
    57115end;
    58116
     
    62120  NewFile: TFile;
    63121begin
     122  Scheduler.Free;
     123  Scheduler := SchedulerClass.Create;
     124
    64125  NewDrive := TDrive.Create;
    65126  NewDrive.Name := 'System';
    66   NewDrive.FileSystem := TBaseFileSystem.Create;
     127  NewDrive.FileSystem := FileSystemClass.Create;
    67128  Drives.Add(NewDrive);
    68129
     
    83144    SystemFileSystem := SystemDrive.FileSystem;
    84145    AppFile := TFile(SystemFileSystem.TopDirectory.Items.FindByName('Clock'));
    85     if Assigned(AppFile) then Execute(AppFile)
     146    if Assigned(AppFile) then AppExecute(AppFile)
    86147      else raise Exception.Create('Cannot start first application');
     148    Scheduler.Start;
    87149  end else raise Exception.Create('Missing System drive');
    88150end;
     
    92154  Drives := TNamedObjectList.Create;
    93155  Apps := TNamedObjectList.Create;
     156  Scheduler := TScheduler.Create;
    94157end;
    95158
    96159destructor TKernel.Destroy;
    97160begin
     161  Scheduler.Free;
    98162  Apps.Free;
    99163  Drives.Free;
Note: See TracChangeset for help on using the changeset viewer.