-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathradiomodule.pas
1758 lines (1507 loc) · 39.9 KB
/
radiomodule.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit RadioModule;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Types, Graphics, GraphType, UComplex, radiomessage, gen_graph;
type
TModuleId = Cardinal;
PDataStreamRec = ^TDataStreamRec;
TDataStreamRec = record
Next: PDataStreamRec;
Counter: Integer;
Index: Integer;
Data: array of Complex;
end;
TRadioModule = class;
TRadioLogLevel = (llVerbose, llInfo, llWarn, llError, llSystem);
PRadioStringNode = ^TRadioStringNode;
TRadioStringNode = record
Next: PRadioStringNode;
S: string;
end;
{ TRadioStringStorage }
TRadioStringStorage = class
private
FHead: TRadioStringNode;
FLast: PRadioStringNode;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Store(const S: string): PtrUInt;
end;
{ TRadioDataStream }
TRadioDataStream = class
const
BLOCK_NUM = 3;
private
FName: string;
FBlockSize: Integer;
FFreeFlag: Boolean;
FAllocted: PDataStreamRec;
FBuffers: array of PDataStreamRec;
FFree: PDataStreamRec;
FModule: TRadioModule;
FPortId: Integer;
function GetBuffer(const Index: Integer): PComplex;
function GetBufferCount: Integer;
function GetDefBufferSize: Cardinal;
procedure SetDefBufferSize(AValue: Cardinal);
procedure FreeBlock(X: PDataStreamRec); // Lock is required
procedure DumpList;
public
constructor Create(Module: TRadioModule; const AName: string; const BlockSize: Integer);
destructor Destroy; override;
procedure SafeFree;
procedure EnsureBlockNumber(const AtLeast: Integer);
procedure Lock;
procedure Unlock;
function TryAlloc(out Index: Integer): PComplex;
procedure Broadcast(const Index: Integer; Listeners: TList);
procedure Release(const Index: Integer); // Listeners call this to release buffer // Alert: multi-thread
function GetBufferSize(const Index: Integer): Integer;
property Name: string read FName;
property Buffer[const Index: Integer]: PComplex read GetBuffer;
property BufferSize: Cardinal read GetDefBufferSize write SetDefBufferSize;
property BufferCount: Integer read GetBufferCount;
property PortId: Integer read FPortId write FPortId;
end;
TReceiveData = procedure (const P: PComplex; const Len: Integer) of object;
{ TStreamRegulator }
TStreamRegulator = class
private
FData: array of Complex;
FOnReceiveData: TReceiveData;
FSize: Integer;
FCursor: Integer;
FOverlap: Integer;
procedure SetOnReceiveData(AValue: TReceiveData);
procedure SetOverlap(AValue: Integer);
procedure SetSize(AValue: Integer);
procedure CallOnRegulatedData;
public
constructor Create;
procedure ReceiveData(P: PComplex; Len: Integer);
property Size: Integer read FSize write SetSize;
property Overlap: Integer read FOverlap write SetOverlap;
property OnRegulatedData: TReceiveData read FOnReceiveData write SetOnReceiveData ;
end;
TRadioMessageId = 0..31;
TRadioMessageIdSet = Cardinal;
PRadioMessageNode = ^TRadioMessageNode;
TRadioMessageNode = record
Next: PRadioMessageNode;
Msg: TRadioMessage;
end;
TRadioMessageQueue = class;
TRadioRunQueue = class;
PRadioThreadNode = ^TRadioThreadNode;
{ TRadioThread }
TRadioThread = class(TThread)
private
FJob: TRadioMessageQueue;
FNode: PRadioThreadNode;
FRunQueue: TRadioRunQueue;
FJobScheduled: PRTLEvent;
FRunTimeAcc: TTime; // maintained by TRadioRunQueue
FStartTime: TTime;
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
// Job want to yield, worker will pick a new job
function Yield(Job: TRadioMessageQueue): Cardinal;
property Job: TRadioMessageQueue read FJob write FJob;
property Node: PRadioThreadNode read FNode write FNode;
property Terminated;
end;
TRadioThreadNode = record
Next: PRadioThreadNode;
Thread: TRadioThread;
end;
PMessageQueueNode = ^TMessageQueueNode;
TMessageQueueNode = record
Next: PMessageQueueNode;
Queue: TRadioMessageQueue;
end;
{ TRadioRunQueue }
TRadioRunQueue = class
private
FFirstJob: TMessageQueueNode;
FIdleNode: TRadioThreadNode;
FWorkers: array of TRadioThread;
FDestroying: Boolean;
FStatStart: TDateTime;
private
function GetSMP: Integer;
procedure Schedule;
procedure Lock;
procedure UnLock;
procedure WorkerIdle(Worker: TRadioThread);
public
constructor Create(const SMP: Integer = 4);
destructor Destroy; override;
function PickJob: TRadioMessageQueue;
procedure Request(Job: TRadioMessageQueue);
public
function GetWorkerLoadInfo(var Load: array of Double): Boolean;
function GetRunningModules(var Modules: array of string): Boolean;
property SMP: Integer read GetSMP;
end;
{ TRadioMessageQueue }
TRadioMessageQueue = class
private
FName: string;
FInQueue: Boolean;
FFirstExecMsg: TRadioMessageNode;
FLastExecMsg: PRadioMessageNode;
FMessageFilter: TRadioMessageIdSet;
FRunQueue: TRadioRunQueue;
FCPUTime: TTime;
FYieldTime: TTime;
FRunThread: TRadioThread;
FDestroying: Boolean;
function GetNeedExec: Boolean;
procedure SetInQueue(AValue: Boolean);
procedure RequestSchudule;
protected
procedure MessageExceute; // execute one message in a single call
procedure ProccessMessage(const Msg: TRadioMessage; var Ret: PtrInt); virtual; abstract;
public
constructor Create(RunQueue: TRadioRunQueue); virtual;
procedure Lock;
procedure UnLock;
procedure StoreMessage(const Msg: TRadioMessage);
procedure MessageQueueReset;
property NeedExecution: Boolean read GetNeedExec;
property InQueue: Boolean read FInQueue write SetInQueue;
property Name: string read FName write FName;
property CPUTime: TTime read FCPUTime;
property RunThread: TRadioThread read FRunThread write FRunThread;
end;
TDataListener = record
M: TModuleId;
SourcePort: Integer;
TargetPort: Integer;
end;
PDataListener = ^TDataListener;
{ TRadioModule }
TRadioModule = class(TRadioMessageQueue, IGenDrawable)
const
ICON_SIZE = 50;
HEADER_SIZE = 20;
HEADER_FONT_HEIGHT = 15;
BTN_CONFIG = '::';
BTN_GUI = 'gui';
BODER_WIDTH = 2;
private
FGraphNode: TGenEntityNode;
FId: TModuleId;
FRefCount: Integer;
FDefOutput: TRadioDataStream;
FDescStr: TStringList;
FGUIBtnRect: TRect;
FConfigBtnRect: TRect;
procedure SetId(AValue: TModuleId);
protected
FIcon: string;
FHasGUI: Boolean;
FHasConfig: Boolean;
FInvalidated: Boolean;
{$IFDEF FPC}
function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid: tguid; out obj): longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
{$ELSE}
function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
{$ENDIF}
function _AddRef: Integer; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _Release: Integer; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
procedure Measure(ACanvas: TCanvas; out Extent: TSize);
procedure Draw(ACanvas: TCanvas; ARect: TRect);
procedure MouseClick(const Pt: TPoint);
function Invalidated: Boolean;
protected
FDataListeners: TList;
FFeatureListeners: TList;
function FindDataListener(Listener: TModuleId): Integer;
procedure AddDataListener(Listener: TModuleId; const SourcePort, TargetPort: Integer);
procedure AddFeatureListener(Listener: TModuleId);
procedure RemoveDataListener(Listener: TModuleId);
procedure RemoveFeatureListener(Listener: TModuleId);
procedure ClearDataListeners;
procedure ClearFeatureListeners;
function Alloc(Stream: TRadioDataStream; out Index: Integer): PComplex;
procedure ProccessMessage(const Msg: TRadioMessage; var Ret: Integer); override;
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); virtual;
procedure RMControl(const Msg: TRadioMessage; var Ret: Integer); virtual;
procedure RMData(const Msg: TRadioMessage; var Ret: Integer); virtual;
procedure RMSetFeature(const Msg: TRadioMessage; var Ret: Integer); virtual;
procedure RMReport(const Msg: TRadioMessage; var Ret: Integer); virtual;
procedure RMTimer(const Msg: TRadioMessage; var Ret: Integer); virtual;
function RMSetFrequency(const Msg: TRadioMessage; const Freq: Cardinal): Integer; virtual;
function RMSetBandwidth(const Msg: TRadioMessage; const Bandwidth: Cardinal): Integer; virtual;
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer; virtual;
function RMPhaseAdjust(const Msg: TRadioMessage; const Rate: Cardinal): Integer; virtual;
procedure DoConfigure; virtual;
procedure DoShowGUI; virtual;
procedure DoReset; virtual;
procedure DoBeforeDestroy; virtual;
procedure DoSyncDestroy; virtual;
procedure Describe(Strs: TStrings); virtual;
public
procedure ShowConnections(Graph: TGenGraph);
property GraphNode: TGenEntityNode read FGraphNode write FGraphNode;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
class function LoadDefIconRes(ResName: string = ''): string;
procedure GraphInvalidate;
procedure PostMessage(const Id: Integer; const ParamH, ParamL: PtrUInt);
procedure PostMessage(const Msg: TRadioMessage); virtual;
procedure Broadcast(const Msg: TRadioMessage); overload;
procedure Broadcast(const AId: Integer; const AParamH, AParamL: PtrUInt); overload;
procedure ReceiveData(const P: PComplex; const Len: Integer); virtual; overload;
procedure ReceiveData(const Port: Integer; const P: PComplex; const Len: Integer); virtual;
property DefOutput: TRadioDataStream read FDefOutput;
property Id: TModuleId read FId write SetId;
end;
TRadioModuleClass = class of TRadioModule;
TBackgroundRadioModule = class;
{ TGenericRadioThread }
TGenericRadioThread = class(TThread)
private
FModule: TBackgroundRadioModule;
FParam: Pointer;
protected
procedure Execute; override;
public
constructor Create(Module: TBackgroundRadioModule);
property Module: TBackgroundRadioModule read FModule write FModule;
property Param: Pointer read FParam write FParam;
property Terminated;
end;
{ TBackgroundRadioModule }
TBackgroundRadioModule = class(TRadioModule)
protected
FThread: TGenericRadioThread;
procedure ThreadFun(Thread: TGenericRadioThread); virtual;
procedure DoStopThreadFun; virtual; abstract;
procedure DoBeforeDestroy; override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
end;
{ TRadioLogger }
TRadioLogger = class
private
FLevel: TRadioLogLevel; static;
protected
FInstance: TRadioLogger; static;
procedure DoReport(const ALevel: TRadioLogLevel; const S: string); virtual;
public
class procedure Report(const ALevel: TRadioLogLevel; const AFormat: string; Params: array of const); overload;
class procedure Report(const ALevel: TRadioLogLevel; const AFormat: string);
class property Level: TRadioLogLevel read FLevel write FLevel;
class function MsgToStr(const M: TRadioMessage): string;
class function GetInstance: TRadioLogger;
end;
// I don't like to creae too many CriticalSections
procedure RadioGlobalLock;
procedure RadioGlobalUnlock;
function MakeMessage(const Id: Integer; const ParamH, ParamL: PtrUInt;
const Sender: TRadioModule = nil): TRadioMessage;
function ClassNameToModuleName(const S: string): string;
implementation
uses
Math, SignalBasic, utils, util_math, util_config, RadioSystem;
var
RadioGlobalCS: TRTLCriticalSection;
procedure RadioGlobalLock;
begin
EnterCriticalsection(RadioGlobalCS);
end;
procedure RadioGlobalUnlock;
begin
LeaveCriticalsection(RadioGlobalCS);
end;
function MakeMessage(const Id: Integer; const ParamH, ParamL: PtrUInt;
const Sender: TRadioModule): TRadioMessage;
begin
Result.Id := Id;
Result.ParamH := ParamH;
Result.ParamL := ParamL;
if Assigned(Sender) then Result.Sender := Sender.Name;
end;
function ClassNameToModuleName(const S: string): string;
begin
Result := S;
if Length(Result) < 1 then Exit;
if Pos('TRadio', S) = 1 then Delete(Result, 1, 6)
else
if Result[1] in ['t', 'T'] then Delete(Result, 1, 1);
if Copy(Result, Length(Result) - 5, 6) = 'Module' then Delete(Result, Length(Result) - 5, 6);
end;
{ TRadioStringStorage }
constructor TRadioStringStorage.Create;
begin
FLast := @FHead;
end;
destructor TRadioStringStorage.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TRadioStringStorage.Clear;
var
X: PRadioStringNode;
begin
X := FHead.Next;
while Assigned(X) do
begin
FHead.Next := X^.Next;
Dispose(X);
X := FHead.Next;
end;
FLast := @FHead;
end;
function TRadioStringStorage.Store(const S: string): PtrUInt;
var
X: PRadioStringNode;
begin
New(X);
X^.S := Copy(S, 1, Length(S));
X^.Next := nil;
FLast^.Next := X;
Result := PtrUInt(@X^.S);
end;
{ TRadioLogger }
procedure TRadioLogger.DoReport(const ALevel: TRadioLogLevel; const S: string);
begin
end;
class procedure TRadioLogger.Report(const ALevel: TRadioLogLevel;
const AFormat: string; Params: array of const);
var
S: string;
begin
if not Assigned(FInstance) then Exit;
if FLevel > ALevel then Exit;
S := Format(AFormat, Params);
FInstance.DoReport(ALevel, S);
end;
class procedure TRadioLogger.Report(const ALevel: TRadioLogLevel;
const AFormat: string);
begin
if not Assigned(FInstance) then Exit;
if FLevel > ALevel then Exit;
FInstance.DoReport(ALevel, AFormat);
end;
class function TRadioLogger.MsgToStr(const M: TRadioMessage): string;
begin
if M.Sender <> '' then
Result := 'from ' + M.Sender
else
Result := 'from unknown';
Result := Result + Format(' {%d, %d, %d}', [M.Id, M.ParamH, M.ParamL]);
end;
class function TRadioLogger.GetInstance: TRadioLogger;
begin
Result := FInstance;
end;
{ TRadioMessageQueue }
function TRadioMessageQueue.GetNeedExec: Boolean;
begin
Result := (not FDestroying) and Assigned(FFirstExecMsg.Next);
end;
procedure TRadioMessageQueue.SetInQueue(AValue: Boolean);
var
Req: Boolean = False;
begin
Lock;
if FInQueue <> AValue then
begin
FInQueue := AValue;
Req := not AValue;
end;
Unlock;
if FDestroying and (not FInQueue) then
begin
Free;
Exit;
end;
if Req and NeedExecution then
RequestSchudule;
end;
procedure TRadioMessageQueue.RequestSchudule;
begin
if (not FDestroying) and (not FInQueue) then FRunQueue.Request(Self);
end;
procedure TRadioMessageQueue.MessageExceute;
var
Msg: TRadioMessage;
Ret: Integer = 0;
P: PRadioMessageNode;
begin
if FDestroying then Exit;
if not Assigned(FFirstExecMsg.Next) then Exit;
Lock;
P := FFirstExecMsg.Next;
Msg := P^.Msg;
FFirstExecMsg.Next := P^.Next;
if not Assigned(FFirstExecMsg.Next) then
FLastExecMsg := @FFirstExecMsg;
UnLock;
Dispose(P);
try
TRadioLogger.Report(llVerbose, Format('in thread #%d, %s start exec msg %s', [GetThreadID, Name, TRadioLogger.MsgToStr(Msg)]));
ProccessMessage(Msg, Ret);
TRadioLogger.Report(llVerbose, Name + ' msg exec done');
except
on E: Exception do
TRadioLogger.Report(llError, Format('MessageExceute Exception: %s when handling %s', [E.Message, TRadioLogger.MsgToStr(Msg)]));
end;
end;
constructor TRadioMessageQueue.Create(RunQueue: TRadioRunQueue);
begin
FMessageFilter := $FFFFFFFF;
FLastExecMsg := @FFirstExecMsg;
FRunQueue := RunQueue;
end;
procedure TRadioMessageQueue.Lock;
begin
RadioGlobalLock;
end;
procedure TRadioMessageQueue.UnLock;
begin
RadioGlobalUnlock;
end;
procedure TRadioMessageQueue.StoreMessage(const Msg: TRadioMessage);
var
P: PRadioMessageNode;
begin
if FDestroying then Exit;
New(P);
P^.Msg := Msg;
P^.Next := nil;
Lock;
FLastExecMsg^.Next := P;
FLastExecMsg := P;
UnLock;
RequestSchudule;
end;
procedure TRadioMessageQueue.MessageQueueReset;
procedure FreeList(P: PRadioMessageNode);
var
T: PRadioMessageNode;
begin
while Assigned(P) do
begin
T := P^.Next;
Dispose(P);
P := T;
end;
end;
begin
Lock;
FreeList(FFirstExecMsg.Next);
FFirstExecMsg.Next := nil;
FLastExecMsg := @FFirstExecMsg;
UnLock;
end;
{ TBackgroundRadioModule }
procedure TBackgroundRadioModule.ThreadFun(Thread: TGenericRadioThread);
begin
Sleep(10);
end;
procedure TBackgroundRadioModule.DoBeforeDestroy;
begin
FThread.Terminate;
DoStopThreadFun;
FThread.WaitFor;
inherited DoBeforeDestroy;
end;
constructor TBackgroundRadioModule.Create(RunQueue: TRadioRunQueue);
begin
inherited;
FThread := TGenericRadioThread.Create(Self);
end;
destructor TBackgroundRadioModule.Destroy;
begin
FThread.Free;
inherited;
end;
{ TGenericRadioThread }
procedure TGenericRadioThread.Execute;
begin
while not Terminated do
if Assigned(FModule) then FModule.ThreadFun(Self);
end;
constructor TGenericRadioThread.Create(Module: TBackgroundRadioModule);
begin
inherited Create(True);
FModule := Module;
Start;
end;
{ TRadioRunQueue }
function TRadioRunQueue.GetSMP: Integer;
begin
Result := Length(FWorkers);
end;
procedure TRadioRunQueue.Schedule;
label
again;
var
T: PRadioThreadNode;
P: PMessageQueueNode;
begin
again:
if FDestroying then Exit;
Lock;
if Assigned(FFirstJob.Next) and Assigned(FIdleNode.Next) then
begin
T := FIdleNode.Next;
FIdleNode.Next := T^.Next;
P := FFirstJob.Next;
FFirstJob.Next := P^.Next;
UnLock;
T^.Thread.FStartTime := Now;
T^.Thread.Job := P^.Queue;
Dispose(P);
RTLEventSetEvent(T^.Thread.FJobScheduled);
goto again;
end
else
Unlock;
end;
procedure TRadioRunQueue.Lock;
begin
RadioGlobalLock;
end;
procedure TRadioRunQueue.UnLock;
begin
RadioGlobalUnlock;
end;
procedure TRadioRunQueue.WorkerIdle(Worker: TRadioThread);
var
J: TRadioMessageQueue;
begin
if FDestroying then Exit;
Worker.FRunTimeAcc := Worker.FRunTimeAcc + (Now - Worker.FStartTime);
Worker.FStartTime := 0;
Lock;
Worker.Node^.Next := FIdleNode.Next;
FIdleNode.Next := Worker.Node;
Unlock;
if Assigned(Worker.FJob) then
begin
J := Worker.FJob;
Worker.FJob := nil;
J.InQueue := False; // here, worker might be re-scheduled!
end;
Schedule;
end;
constructor TRadioRunQueue.Create(const SMP: Integer);
var
I: Integer;
P: PRadioThreadNode;
begin
SetLength(FWorkers, SMP);
for I := 1 to SMP do
begin
New(P);
P^.Thread := TRadioThread.Create;
P^.Thread.Node := P;
P^.Thread.FRunQueue := Self;
P^.Next := FIdleNode.Next;
FIdleNode.Next := P;
FWorkers[I - 1] := P^.Thread;
TRadioLogger.Report(llVerbose, 'TRadioRunQueue: thread #%d added', [P^.Thread.ThreadID]);
end;
FStatStart := Now;
end;
destructor TRadioRunQueue.Destroy;
var
T: TRadioThread;
begin
FDestroying := True;
Lock;
FIdleNode.Next := nil;
Unlock;
for T in FWorkers do T.Free;
inherited;
end;
function TRadioRunQueue.PickJob: TRadioMessageQueue;
var
P: PMessageQueueNode;
begin
Result := nil;
if FDestroying then Exit;
if not Assigned(FFirstJob.Next) then Exit;
Lock;
if not Assigned(FFirstJob.Next) then
begin
UnLock;
Exit;
end;
P := FFirstJob.Next;
FFirstJob.Next := P^.Next;
UnLock;
Result := P^.Queue;
Dispose(P);
end;
procedure TRadioRunQueue.Request(Job: TRadioMessageQueue);
var
T: PRadioThreadNode;
P: PMessageQueueNode;
F: Boolean = False;
begin
Lock;
if not Job.InQueue then
begin
F := True;
Job.InQueue := True;
end;
Unlock;
if not F then Exit;
Lock;
if Assigned(FIdleNode.Next) then
begin
T := FIdleNode.Next;
FIdleNode.Next := T^.Next;
UnLock;
T^.Thread.FStartTime := Now;
T^.Thread.Job := Job;
RTLEventSetEvent(T^.Thread.FJobScheduled);
Exit;
end
else
UnLock;
New(P);
P^.Queue := Job;
Lock;
P^.Next := FFirstJob.Next;
FFirstJob.Next := P;
Unlock;
Schedule;
end;
function TRadioRunQueue.GetWorkerLoadInfo(var Load: array of Double): Boolean;
var
I: Integer;
T: TDateTime;
S: TDateTime;
begin
Result := Length(Load) = Length(FWorkers);
if not Result then Exit;
T := Now;
S := Max(1 / MSecsPerDay, T - FStatStart);
FStatStart := Now;
for I := 0 to High(FWorkers) do
begin
if FWorkers[I].FStartTime > 0 then
begin
Load[I] := (FWorkers[I].FRunTimeAcc + (T - FWorkers[I].FStartTime)) / S;
FWorkers[I].FStartTime := T;
end
else begin
Load[I] := FWorkers[I].FRunTimeAcc / S;
end;
FWorkers[I].FRunTimeAcc := 0.0;
end;
end;
function TRadioRunQueue.GetRunningModules(var Modules: array of string
): Boolean;
var
I: Integer;
M: TRadioMessageQueue;
begin
Result := Length(Modules) = Length(FWorkers);
if not Result then Exit;
for I := 0 to High(FWorkers) do
begin
Modules[I] := '<nil>';
M := FWorkers[I].Job;
if not Assigned(M) then Continue;
try
Modules[I] := M.Name;
except
end;
end;
end;
{ TRadioThread }
procedure TRadioThread.Execute;
var
T: TTime;
begin
while not Terminated do
begin
RTLEventWaitFor(FJobScheduled);
if Terminated then Break;
RTLEventResetEvent(FJobScheduled);
if Assigned(FJob) then
begin
T := Now;
with FJob do
begin
RunThread := Self;
while (not Terminated) and NeedExecution do
MessageExceute;
RunThread := nil;
FCPUTime := FCPUTime + Now - T - FYieldTime;
FYieldTime := 0;
end;
FRunQueue.WorkerIdle(Self);
end
else;
end;
end;
constructor TRadioThread.Create;
begin
FJobScheduled := RTLEventCreate;
inherited Create(False);
end;
destructor TRadioThread.Destroy;
begin
Terminate;
RTLeventSetEvent(FJobScheduled);
WaitFor;
RTLEventDestroy(FJobScheduled);
inherited Destroy;
end;
function TRadioThread.Yield(Job: TRadioMessageQueue): Cardinal;
var
J: TRadioMessageQueue;
T: TTime;
begin
Result := 0;
J := FRunQueue.PickJob;
if not Assigned(J) then Exit;
// we only exec one message here, and this job will be at the tail of the queue
// if it has more messaages.
if J.NeedExecution then
begin
T := Now;
J.RunThread := Self;
J.MessageExceute;
T := Now - T;
J.FCPUTime := J.FCPUTime + T;
J.RunThread := nil;
Job.FYieldTime := Job.FYieldTime + T;
end;
J.InQueue := False;
Result := Max(1, Round(T * MSecsPerDay));
end;
{ TStreamRegulator }
procedure TStreamRegulator.SetOnReceiveData(AValue: TReceiveData);
begin
if FOnReceiveData = AValue then Exit;
FOnReceiveData := AValue;
end;
procedure TStreamRegulator.SetOverlap(AValue: Integer);
begin
if AValue < 0 then
FOverlap := 0
else if AValue > FSize - 1 then
FOverlap := FSize - 1
else
FOverlap := AValue;
end;
procedure TStreamRegulator.SetSize(AValue: Integer);
begin
if AValue < 1 then Exit;
if FSize = AValue then Exit;
FSize := AValue;
CallOnRegulatedData;
SetLength(FData, FSize);
Overlap := FOverlap;
end;
procedure TStreamRegulator.CallOnRegulatedData;
var
I: Integer = 0;
begin
while FCursor >= FSize do
begin
if Assigned(FOnReceiveData) then FOnReceiveData(@FData[I], FSize);
Inc(I, FSize - FOverlap);
Dec(FCursor, FSize - FOverlap);
end;
if FCursor > 0 then
Move(FData[I], FData[0], FCursor * SizeOf(FData[0]));
end;
constructor TStreamRegulator.Create;
begin
FSize := 1024;
SetLength(FData, FSize);
end;
procedure TStreamRegulator.ReceiveData(P: PComplex; Len: Integer);
var
F: Integer;
S: Integer;
begin
while Len > 0 do
begin
F := FSize - FCursor;
if F <= 0 then