summaryrefslogtreecommitdiff
path: root/rtl/aros/arosthreads.inc
blob: fd0d9d0798c316fb5b84da2e1328da03c37ab6d7 (plain)
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

type
  TThreadEntryfunction = function(data: Pointer): Pointer; cdecl;


  TMutextKind = (mkExclusive, mkShared);

  TAROSMutex = record
    Semaphore: TSignalSemaphore;
  end;
  PAROSMutex = ^TAROSMutex;
  
  TCondition = record
    Lock: TSignalSemaphore;
    Waiters: array of Pointer;
  end;
  PCondition = ^TCondition;

  TAROSThread = record
    Entry: TThreadEntryfunction;
    Data: Pointer;
    ThreadID: LongWord;
    Priority: LongInt;
    StackSize: LongInt;
    Task: PProcess;
    Lock: TSignalSemaphore;
    StartupSemaphore: TSignalSemaphore;
    EndCondition: PCondition;
    EndMutex: PAROSMutex;
    EndCount: Integer;
  end;
  PAROSThread = ^TAROSThread;
  
  TAROSThreadStruct = record
    MutexListSem: TSignalSemaphore;
    MutexList: array of PAROSMutex;
    //
    ThreadListSem: TSignalSemaphore;
    ThreadList: array of PAROSThread;
    //
    ConditionListSem: TSignalSemaphore;
    ConditionList: array of PCondition;
    //
    ThreadMemSem: TSignalSemaphore;
    EmptySemaphore: TSignalSemaphore;
    //
    LastThreadNum: LongWord;
  end;
  PAROSThreadStruct = ^TAROSThreadStruct;

var
  AROSThreadStruct: PAROSThreadStruct external name 'AROS_THREADLIB';


function CreateNewProcTags(const Tags: array of PtrUInt): PProcess;
begin
  CreateNewProcTags := CreateNewProc(@Tags[0]);
end; 

// Mutexe

function CreateMutex: PAROSMutex;
var
  Mutex: PAROSMutex;
  Idx, i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  New(Mutex);
  InitSemaphore(@(Mutex^.Semaphore));
  ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  Idx := -1;
  for i := 0 to High(AROSThreadStruct^.MutexList) do
  begin
    if not Assigned(AROSThreadStruct^.MutexList[i]) then
    begin
      Idx := i;
      Break;
    end;
  end;
  if Idx < 0 then
  begin
    Idx := Length(AROSThreadStruct^.MutexList);
    SetLength(AROSThreadStruct^.MutexList, Idx + 1);
  end;
  AROSThreadStruct^.MutexList[Idx] := Mutex;
  ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));
  Result := Mutex;
end;

procedure DestroyMutex(Mutex: PAROSMutex);
var
  i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  for i := 0 to High(AROSThreadStruct^.MutexList) do
  begin
    if AROSThreadStruct^.MutexList[i] = Mutex then
    begin
      FillChar(Mutex^.Semaphore, SizeOf(TSignalSemaphore), 0);
      Dispose(Mutex);
      AROSThreadStruct^.MutexList[i] := nil;
    end;
  end;
  ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));  
end;


function IsValidMutex(Mutex: PAROSMutex): Boolean;
var
  i: Integer;
begin
  Result := False;
  if not Assigned(AROSThreadStruct) then
    Exit;
  ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  for i := 0 to High(AROSThreadStruct^.MutexList) do
  begin
    if AROSThreadStruct^.MutexList[i] = Mutex then
    begin
      Result := True;
      Break;
    end;
  end;
  ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));
end;

procedure LockMutex(Mutex: PAROSMutex);
begin
  if IsValidMutex(Mutex) then
  begin
    ObtainSemaphore(@(Mutex^.Semaphore));  
  end;
end;

function TryLockMutex(Mutex: PAROSMutex): Boolean;
begin
  Result := False;
  if IsValidMutex(Mutex) then
  begin
    Result := AttemptSemaphore(@(Mutex^.Semaphore)) <> 0;  
  end;
end;

procedure UnLockMutex(Mutex: PAROSMutex);
begin
  if IsValidMutex(Mutex) then
  begin
    ReleaseSemaphore(@(Mutex^.Semaphore));  
  end;
end;

// Conditions

function CreateCondition: PCondition;
var
  Idx, i: Integer;
  NewCond: PCondition;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  New(NewCond);
  SetLength(NewCond^.Waiters, 0);
  InitSemaphore(@(NewCond^.Lock));
  ObtainSemaphore(@(AROSThreadStruct^.ConditionListSem));
  Idx := -1;
  for i := 0 to High(AROSThreadStruct^.ConditionList) do
  begin
    if not Assigned(AROSThreadStruct^.ConditionList[i]) then
    begin
      Idx := i;
      Break;
    end;
  end;
  if Idx < 0 then
  begin
    Idx := Length(AROSThreadStruct^.ConditionList);
    SetLength(AROSThreadStruct^.ConditionList, Idx + 1);
  end;
  AROSThreadStruct^.ConditionList[Idx] := NewCond;
  ReleaseSemaphore(@(AROSThreadStruct^.ConditionListSem));
  Result := NewCond;
end;

function DestroyCondition(Cond: PCondition): boolean;
var
  Idx, i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  ObtainSemaphore(@(Cond^.Lock));
  if Length(Cond^.Waiters) > 0 then
  begin
    ReleaseSemaphore(@(Cond^.Lock));
    Result := False;
    Exit;
  end;
  ObtainSemaphore(@(AROSThreadStruct^.ConditionListSem));
  Idx := -1;
  for i := 0 to High(AROSThreadStruct^.ConditionList) do
  begin
    if AROSThreadStruct^.ConditionList[i] = Cond then
    begin
      AROSThreadStruct^.ConditionList[i] := nil;
      Dispose(Cond);
      Break;
    end;
  end;
  ReleaseSemaphore(@(AROSThreadStruct^.ConditionListSem));
  Result := True;
end;

function WaitCondition(Cond: PCondition; Mutex: PAROSMutex): boolean;
var
  Idx: Integer;
begin
  if (not Assigned(Cond)) or (not Assigned(Mutex)) then
  begin
    Result := False;
    Exit;
  end;
  ObtainSemaphore(@Cond^.Lock);
  Idx := Length(Cond^.Waiters);
  SetLength(Cond^.Waiters, Idx + 1);
  Cond^.Waiters[Idx] := FindTask(nil);
  ReleaseSemaphore(@Cond^.Lock);
  
  Forbid();
  UnLockMutex(Mutex);
  Wait(SIGF_SINGLE);
  Permit();
  LockMutex(Mutex);
  Result := True;
end;

procedure SignalCondition(Cond: PCondition);
var
  Waiter: PTask;
  Idx: Integer;
begin
  if not Assigned(Cond) then
    Exit;
  ObtainSemaphore(@Cond^.Lock);
  Waiter := nil;
  //debugln(' found ' + IntToStr(Cond^.Waiters.Count) + ' Waiter');
  if Length(Cond^.Waiters) > 0 then
  begin
    Idx := High(Cond^.Waiters);
    Waiter := Cond^.Waiters[Idx];
    SetLength(Cond^.Waiters, Idx);
  end;  
  ReleaseSemaphore(@Cond^.Lock);
  if not Assigned(Waiter) then
  begin
    //debugln('Waiter not assigned');
    Exit;
  end;
  //debugln('Signal Waiter');  
  Signal(Waiter, SIGF_SINGLE);
end;

procedure BroadcastCondition(Cond: PCondition);
var
  Waiter: PTask;
  I: Integer;
begin
  if not Assigned(Cond) then
    Exit;
  Waiter := nil;
  ObtainSemaphore(@Cond^.Lock);
  for i := 0 to High(Cond^.Waiters) do
  begin
    Waiter := Cond^.Waiters[i];
    Signal(Waiter, SIGF_SINGLE);
  end;
  SetLength(Cond^.Waiters, 0);
  ReleaseSemaphore(@Cond^.Lock);   
end;

// Threads

procedure StarterFunc; cdecl;
var
  NewThread: PAROSThread;
  StackMem: Pointer;
  sswap: TStackSwapStruct; 
  Proc: PTask;
begin
  Proc := FindTask(nil);
  NewThread := PAROSThread(Proc^.tc_UserData);
  // create New Stack
  StackMem := GetMem(NewThread^.StackSize);
  sswap.stk_Lower := StackMem;
  sswap.stk_Upper := Pointer(PtrUInt(sswap.stk_Lower) + NewThread^.StackSize);
  sswap.stk_Pointer := sswap.stk_Upper;
  ReleaseSemaphore(@AROSThreadStruct^.ThreadMemSem);
  // semaphore against too fast startup
  ReleaseSemaphore(@(NewThread^.StartupSemaphore));
  // swap stack, run program, swap stack back
  Stackswap(@sswap);
  NewThread^.Entry(NewThread^.Data);
  Stackswap(@sswap);
  //debugln('5');

  // Free stack memory
  ObtainSemaphore(@AROSThreadStruct^.ThreadMemSem);
  FreeMem(StackMem);
  ReleaseSemaphore(@AROSThreadStruct^.ThreadMemSem);
  // finished mark as finished
	ObtainSemaphore(@NewThread^.Lock);
	NewThread^.Task := nil;
	ReleaseSemaphore(@NewThread^.Lock);
	// tell the others we are finished!
	//Debugln('wait for end ' + IntToStr(NewThread^.ThreadId));
	LockMutex(NewThread^.EndMutex);
	BroadcastCondition(NewThread^.EndCondition);
	UnLockMutex(NewThread^.EndMutex);
	//Debugln('End ' + IntToStr(NewThread^.ThreadId));	
end;

procedure EmptyFunc;
begin
  Delay(1);
  ReleaseSemaphore(@AROSThreadStruct^.EmptySemaphore);
end;


function AROSCreateThread(Entry: TThreadEntryfunction; data: Pointer; StackSize: Integer = 262144; Priority: Integer = 0): LongWord;
var
  NewThread: PAROSThread;
  Idx, i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  New(NewThread);
  ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  Idx := -1;
  for i := 0 to High(AROSThreadStruct^.ThreadList) do
  begin
    if not Assigned(AROSThreadStruct^.ThreadList[i]) then
    begin
      Idx := i;
      Break;
    end;
  end;
  if Idx < 0 then
  begin
    Idx := Length(AROSThreadStruct^.ThreadList);
    SetLength(AROSThreadStruct^.ThreadList, Idx + 1);
  end;
  Inc(AROSThreadStruct^.LastThreadNum);
  AROSThreadStruct^.ThreadList[Idx] := NewThread; 
  NewThread^.ThreadID := AROSThreadStruct^.LastThreadNum;
  NewThread^.Entry := Entry;
  NewThread^.Data := Data;
  NewThread^.Priority := Priority;
  NewThread^.StackSize := StackSize;
  InitSemaphore(@(NewThread^.Lock));
  InitSemaphore(@(NewThread^.StartupSemaphore));
  NewThread^.EndCondition := CreateCondition;
  NewThread^.EndMutex := CreateMutex;
  NewThread^.EndCount := 0;
  ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  
  ObtainSemaphore(@AROSThreadStruct^.ThreadMemSem);
  
  // Semaphore for too fast startup
  ObtainSemaphore(@(NewThread^.StartupSemaphore));
  
  // a very ugly Bugfix, for crashing AROS, on the very first Task after reboot
  // recheck later if can be removed
  if NewThread^.ThreadID  = 1 then
  begin
    //debugln('make empty thread');
    ObtainSemaphore(@AROSThreadStruct^.EmptySemaphore);
    NewThread^.Task := CreateNewProcTags([
      NP_Entry, PtrUInt(@EmptyFunc),
      TAG_DONE, TAG_END]);
    ObtainSemaphore(@AROSThreadStruct^.EmptySemaphore);
    Delay(1);
  end;
  //
  NewThread^.Task := CreateNewProcTags([
    NP_Entry, PtrUInt(@StarterFunc),
    //NP_Name, PtrUInt(PChar('Thread' + IntToStr(LastThreadNum))),
    //NP_StackSize, 10024 * 1024,
    NP_Priority, Priority, 
    NP_UserData, PtrUInt(NewThread),
    TAG_DONE, TAG_END]);
  Result := NewThread^.ThreadID;
end;

function AROSCurrentThread: LongInt;
var
  Task: PProcess;
  i: Integer;
begin
  Result := 0;
  Task := PProcess(FindTask(nil));
  ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  for i := 0 to High(AROSThreadStruct^.ThreadList) do
  begin
    if Assigned(AROSThreadStruct^.ThreadList[i]) then
    begin
      if AROSThreadStruct^.ThreadList[i]^.Task = Task then
      begin
        Result := AROSThreadStruct^.ThreadList[i]^.ThreadID;
        Break;
      end;  
    end;
  end; 
  ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
end;


function AROSWaitThread(ThreadID: LongWord): Boolean;
var
  Thread: PAROSThread;
  Idx, i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  Thread := nil;
  Idx := -1;
  for i := 0 to High(AROSThreadStruct^.ThreadList) do
  begin
    if Assigned(AROSThreadStruct^.ThreadList[i]) then
    begin
      if AROSThreadStruct^.ThreadList[i]^.ThreadID = ThreadID then
      begin
        Thread := AROSThreadStruct^.ThreadList[i];
        Idx := i;
        break;
      end;  
    end;
  end;  
  ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  if Thread = nil then
  begin
    //debugln('Thread not found');
    Result := False;
    Exit;
  end;
  
  // check some 
  ObtainSemaphore(@Thread^.Lock);
  // hmm thats me... I do not wait for myself
  if Thread^.Task = PProcess(FindTask(nil)) then
  begin
    //debugln(' hmm its me :O ' + IntToStr(ThreadID));
    ReleaseSemaphore(@Thread^.Lock);
    Result := False;
    Exit;
  end;
  // wait that the thread start is finished somehow ;)
  ObtainSemaphore(@(Thread^.StartupSemaphore));
  ReleaseSemaphore(@(Thread^.StartupSemaphore));
  // check if Task is still running
  if Thread^.Task <> nil then
  begin
    Inc(Thread^.EndCount);
    ReleaseSemaphore(@Thread^.Lock);
    LockMutex(Thread^.EndMutex);
    //debugln(' Wait condition ' + IntToStr(ThreadID));
    WaitCondition(Thread^.EndCondition, Thread^.EndMutex);
    //debugln(' got condition ' + IntToStr(ThreadID));
    UnlockMutex(Thread^.EndMutex);
    ObtainSemaphore(@Thread^.Lock);  
    Dec(Thread^.EndCount);
  end;
  if Thread^.EndCount > 0 then
  begin
    ReleaseSemaphore(@Thread^.Lock);
    Result := True;
    Exit;  
  end;
  if Assigned(AROSThreadStruct) then
  begin
    // destroy Thread
    ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
    AROSThreadStruct^.ThreadList[Idx] := nil;
    ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  end;
  DestroyCondition(Thread^.EndCondition);
  DestroyMutex(Thread^.EndMutex);
  Dispose(Thread);
  Result := true;
end;

function AROSCurrentThread: LongWord;
var
  i: Integer;
  CurTask: PProcess;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  Result := 0;
  ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  CurTask := PProcess(FindTask(nil));
  for i := 0 to High(AROSThreadStruct^.ThreadList) do
  begin
    if Assigned(AROSThreadStruct^.ThreadList[i]) then
    begin
      if AROSThreadStruct^.ThreadList[i]^.Task = CurTask then
      begin
        Result := AROSThreadStruct^.ThreadList[i]^.ThreadID;
        Break;
      end;
    end;
  end;
  ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
end;


procedure WaitAllThreads;
var
  i: Integer;
  TID: LongWord;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  i := 0; 
  while i <= High(AROSThreadStruct^.ThreadList) do
  begin
    if Assigned(AROSThreadStruct^.ThreadList[i]) then
    begin
      TID := AROSThreadStruct^.ThreadList[i]^.ThreadID;      
      //
      ObtainSemaphore(@(AROSThreadStruct^.ThreadList[i]^.StartupSemaphore));
      ReleaseSemaphore(@(AROSThreadStruct^.ThreadList[i]^.StartupSemaphore));
      //
      ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
      AROSWaitThread(TID);
      ObtainSemaphore(@AROSThreadStruct^.ThreadListSem); 
    end;  
    Inc(i);
  end;
  ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
end;

{$ifdef THREAD_SYSTEM}

procedure InitThreadLib;
begin
  New(AROSThreadStruct);
  AROSThreadStruct^.LastThreadNum := 0;
  InitSemaphore(@(AROSThreadStruct^.MutexListSem));
  InitSemaphore(@(AROSThreadStruct^.ConditionListSem));
  InitSemaphore(@(AROSThreadStruct^.ThreadListSem));
  InitSemaphore(@(AROSThreadStruct^.ThreadMemSem));
  InitSemaphore(@(AROSThreadStruct^.EmptySemaphore));
end;

procedure FinishThreadLib;
var
  i: Integer;
begin
  if not Assigned(AROSThreadStruct) then
    Exit;
  WaitAllThreads;
  ObtainSemaphore(@AROSThreadStruct^.MutexListSem);
  i := 0;
  for i := 0 to High(AROSThreadStruct^.MutexList) do
  begin
    if Assigned(AROSThreadStruct^.MutexList[i]) then
    begin
      Dispose(AROSThreadStruct^.MutexList[i]);
    end;  
  end;
  ReleaseSemaphore(@AROSThreadStruct^.MutexListSem);
  ObtainSemaphore(@AROSThreadStruct^.ConditionListSem);
  i := 0;
  for i := 0 to High(AROSThreadStruct^.ConditionList) do
  begin
    if Assigned(AROSThreadStruct^.ConditionList[i]) then
    begin
      Dispose(AROSThreadStruct^.ConditionList[i]);
    end;  
  end;
  ReleaseSemaphore(@AROSThreadStruct^.ConditionListSem);
  Dispose(AROSThreadStruct);
  AROSThreadStruct := nil;
end;

{$endif THREAD_SYSTEM}