arosthreads.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. type
  2. TThreadEntryfunction = function(data: Pointer): Pointer; cdecl;
  3. TMutextKind = (mkExclusive, mkShared);
  4. TAROSMutex = record
  5. Semaphore: TSignalSemaphore;
  6. end;
  7. PAROSMutex = ^TAROSMutex;
  8. TCondition = record
  9. Lock: TSignalSemaphore;
  10. Waiters: array of Pointer;
  11. end;
  12. PCondition = ^TCondition;
  13. TAROSThread = record
  14. Entry: TThreadEntryfunction;
  15. Data: Pointer;
  16. ThreadID: LongWord;
  17. Priority: LongInt;
  18. StackSize: LongInt;
  19. Task: PProcess;
  20. Lock: TSignalSemaphore;
  21. StartupSemaphore: TSignalSemaphore;
  22. EndCondition: PCondition;
  23. EndMutex: PAROSMutex;
  24. EndCount: Integer;
  25. end;
  26. PAROSThread = ^TAROSThread;
  27. TAROSThreadStruct = record
  28. MutexListSem: TSignalSemaphore;
  29. MutexList: array of PAROSMutex;
  30. //
  31. ThreadListSem: TSignalSemaphore;
  32. ThreadList: array of PAROSThread;
  33. //
  34. ConditionListSem: TSignalSemaphore;
  35. ConditionList: array of PCondition;
  36. //
  37. ThreadMemSem: TSignalSemaphore;
  38. EmptySemaphore: TSignalSemaphore;
  39. //
  40. LastThreadNum: LongWord;
  41. end;
  42. PAROSThreadStruct = ^TAROSThreadStruct;
  43. var
  44. AROSThreadStruct: PAROSThreadStruct external name 'AROS_THREADLIB';
  45. function CreateNewProcTags(const Tags: array of PtrUInt): PProcess;
  46. begin
  47. CreateNewProcTags := CreateNewProc(@Tags[0]);
  48. end;
  49. // Mutexe
  50. function CreateMutex: PAROSMutex;
  51. var
  52. Mutex: PAROSMutex;
  53. Idx, i: Integer;
  54. begin
  55. if not Assigned(AROSThreadStruct) then
  56. Exit;
  57. New(Mutex);
  58. InitSemaphore(@(Mutex^.Semaphore));
  59. ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  60. Idx := -1;
  61. for i := 0 to High(AROSThreadStruct^.MutexList) do
  62. begin
  63. if not Assigned(AROSThreadStruct^.MutexList[i]) then
  64. begin
  65. Idx := i;
  66. Break;
  67. end;
  68. end;
  69. if Idx < 0 then
  70. begin
  71. Idx := Length(AROSThreadStruct^.MutexList);
  72. SetLength(AROSThreadStruct^.MutexList, Idx + 1);
  73. end;
  74. AROSThreadStruct^.MutexList[Idx] := Mutex;
  75. ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));
  76. Result := Mutex;
  77. end;
  78. procedure DestroyMutex(Mutex: PAROSMutex);
  79. var
  80. i: Integer;
  81. begin
  82. if not Assigned(AROSThreadStruct) then
  83. Exit;
  84. ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  85. for i := 0 to High(AROSThreadStruct^.MutexList) do
  86. begin
  87. if AROSThreadStruct^.MutexList[i] = Mutex then
  88. begin
  89. FillChar(Mutex^.Semaphore, SizeOf(TSignalSemaphore), 0);
  90. Dispose(Mutex);
  91. AROSThreadStruct^.MutexList[i] := nil;
  92. end;
  93. end;
  94. ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));
  95. end;
  96. function IsValidMutex(Mutex: PAROSMutex): Boolean;
  97. var
  98. i: Integer;
  99. begin
  100. Result := False;
  101. if not Assigned(AROSThreadStruct) then
  102. Exit;
  103. ObtainSemaphore(@(AROSThreadStruct^.MutexListSem));
  104. for i := 0 to High(AROSThreadStruct^.MutexList) do
  105. begin
  106. if AROSThreadStruct^.MutexList[i] = Mutex then
  107. begin
  108. Result := True;
  109. Break;
  110. end;
  111. end;
  112. ReleaseSemaphore(@(AROSThreadStruct^.MutexListSem));
  113. end;
  114. procedure LockMutex(Mutex: PAROSMutex);
  115. begin
  116. if IsValidMutex(Mutex) then
  117. begin
  118. ObtainSemaphore(@(Mutex^.Semaphore));
  119. end;
  120. end;
  121. function TryLockMutex(Mutex: PAROSMutex): Boolean;
  122. begin
  123. Result := False;
  124. if IsValidMutex(Mutex) then
  125. begin
  126. Result := AttemptSemaphore(@(Mutex^.Semaphore)) <> 0;
  127. end;
  128. end;
  129. procedure UnLockMutex(Mutex: PAROSMutex);
  130. begin
  131. if IsValidMutex(Mutex) then
  132. begin
  133. ReleaseSemaphore(@(Mutex^.Semaphore));
  134. end;
  135. end;
  136. // Conditions
  137. function CreateCondition: PCondition;
  138. var
  139. Idx, i: Integer;
  140. NewCond: PCondition;
  141. begin
  142. if not Assigned(AROSThreadStruct) then
  143. Exit;
  144. New(NewCond);
  145. SetLength(NewCond^.Waiters, 0);
  146. InitSemaphore(@(NewCond^.Lock));
  147. ObtainSemaphore(@(AROSThreadStruct^.ConditionListSem));
  148. Idx := -1;
  149. for i := 0 to High(AROSThreadStruct^.ConditionList) do
  150. begin
  151. if not Assigned(AROSThreadStruct^.ConditionList[i]) then
  152. begin
  153. Idx := i;
  154. Break;
  155. end;
  156. end;
  157. if Idx < 0 then
  158. begin
  159. Idx := Length(AROSThreadStruct^.ConditionList);
  160. SetLength(AROSThreadStruct^.ConditionList, Idx + 1);
  161. end;
  162. AROSThreadStruct^.ConditionList[Idx] := NewCond;
  163. ReleaseSemaphore(@(AROSThreadStruct^.ConditionListSem));
  164. Result := NewCond;
  165. end;
  166. function DestroyCondition(Cond: PCondition): boolean;
  167. var
  168. Idx, i: Integer;
  169. begin
  170. if not Assigned(AROSThreadStruct) then
  171. Exit;
  172. ObtainSemaphore(@(Cond^.Lock));
  173. if Length(Cond^.Waiters) > 0 then
  174. begin
  175. ReleaseSemaphore(@(Cond^.Lock));
  176. Result := False;
  177. Exit;
  178. end;
  179. ObtainSemaphore(@(AROSThreadStruct^.ConditionListSem));
  180. Idx := -1;
  181. for i := 0 to High(AROSThreadStruct^.ConditionList) do
  182. begin
  183. if AROSThreadStruct^.ConditionList[i] = Cond then
  184. begin
  185. AROSThreadStruct^.ConditionList[i] := nil;
  186. Dispose(Cond);
  187. Break;
  188. end;
  189. end;
  190. ReleaseSemaphore(@(AROSThreadStruct^.ConditionListSem));
  191. Result := True;
  192. end;
  193. function WaitCondition(Cond: PCondition; Mutex: PAROSMutex): boolean;
  194. var
  195. Idx: Integer;
  196. begin
  197. if (not Assigned(Cond)) or (not Assigned(Mutex)) then
  198. begin
  199. Result := False;
  200. Exit;
  201. end;
  202. ObtainSemaphore(@Cond^.Lock);
  203. Idx := Length(Cond^.Waiters);
  204. SetLength(Cond^.Waiters, Idx + 1);
  205. Cond^.Waiters[Idx] := FindTask(nil);
  206. ReleaseSemaphore(@Cond^.Lock);
  207. Forbid();
  208. UnLockMutex(Mutex);
  209. Wait(SIGF_SINGLE);
  210. Permit();
  211. LockMutex(Mutex);
  212. Result := True;
  213. end;
  214. procedure SignalCondition(Cond: PCondition);
  215. var
  216. Waiter: PTask;
  217. Idx: Integer;
  218. begin
  219. if not Assigned(Cond) then
  220. Exit;
  221. ObtainSemaphore(@Cond^.Lock);
  222. Waiter := nil;
  223. //debugln(' found ' + IntToStr(Cond^.Waiters.Count) + ' Waiter');
  224. if Length(Cond^.Waiters) > 0 then
  225. begin
  226. Idx := High(Cond^.Waiters);
  227. Waiter := Cond^.Waiters[Idx];
  228. SetLength(Cond^.Waiters, Idx);
  229. end;
  230. ReleaseSemaphore(@Cond^.Lock);
  231. if not Assigned(Waiter) then
  232. begin
  233. //debugln('Waiter not assigned');
  234. Exit;
  235. end;
  236. //debugln('Signal Waiter');
  237. Signal(Waiter, SIGF_SINGLE);
  238. end;
  239. procedure BroadcastCondition(Cond: PCondition);
  240. var
  241. Waiter: PTask;
  242. I: Integer;
  243. begin
  244. if not Assigned(Cond) then
  245. Exit;
  246. Waiter := nil;
  247. ObtainSemaphore(@Cond^.Lock);
  248. for i := 0 to High(Cond^.Waiters) do
  249. begin
  250. Waiter := Cond^.Waiters[i];
  251. Signal(Waiter, SIGF_SINGLE);
  252. end;
  253. SetLength(Cond^.Waiters, 0);
  254. ReleaseSemaphore(@Cond^.Lock);
  255. end;
  256. // Threads
  257. procedure StarterFunc; cdecl;
  258. var
  259. NewThread: PAROSThread;
  260. StackMem: Pointer;
  261. sswap: TStackSwapStruct;
  262. Proc: PTask;
  263. begin
  264. Proc := FindTask(nil);
  265. NewThread := PAROSThread(Proc^.tc_UserData);
  266. // create New Stack
  267. StackMem := GetMem(NewThread^.StackSize);
  268. sswap.stk_Lower := StackMem;
  269. sswap.stk_Upper := Pointer(PtrUInt(sswap.stk_Lower) + NewThread^.StackSize);
  270. sswap.stk_Pointer := sswap.stk_Upper;
  271. ReleaseSemaphore(@AROSThreadStruct^.ThreadMemSem);
  272. // semaphore against too fast startup
  273. ReleaseSemaphore(@(NewThread^.StartupSemaphore));
  274. // swap stack, run program, swap stack back
  275. Stackswap(@sswap);
  276. NewThread^.Entry(NewThread^.Data);
  277. Stackswap(@sswap);
  278. //debugln('5');
  279. // Free stack memory
  280. ObtainSemaphore(@AROSThreadStruct^.ThreadMemSem);
  281. FreeMem(StackMem);
  282. ReleaseSemaphore(@AROSThreadStruct^.ThreadMemSem);
  283. // finished mark as finished
  284. ObtainSemaphore(@NewThread^.Lock);
  285. NewThread^.Task := nil;
  286. ReleaseSemaphore(@NewThread^.Lock);
  287. // tell the others we are finished!
  288. //Debugln('wait for end ' + IntToStr(NewThread^.ThreadId));
  289. LockMutex(NewThread^.EndMutex);
  290. BroadcastCondition(NewThread^.EndCondition);
  291. UnLockMutex(NewThread^.EndMutex);
  292. //Debugln('End ' + IntToStr(NewThread^.ThreadId));
  293. end;
  294. procedure EmptyFunc;
  295. begin
  296. Delay(1);
  297. ReleaseSemaphore(@AROSThreadStruct^.EmptySemaphore);
  298. end;
  299. function AROSCreateThread(Entry: TThreadEntryfunction; data: Pointer; StackSize: Integer = 262144; Priority: Integer = 0): LongWord;
  300. var
  301. NewThread: PAROSThread;
  302. Idx, i: Integer;
  303. begin
  304. if not Assigned(AROSThreadStruct) then
  305. Exit;
  306. New(NewThread);
  307. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  308. Idx := -1;
  309. for i := 0 to High(AROSThreadStruct^.ThreadList) do
  310. begin
  311. if not Assigned(AROSThreadStruct^.ThreadList[i]) then
  312. begin
  313. Idx := i;
  314. Break;
  315. end;
  316. end;
  317. if Idx < 0 then
  318. begin
  319. Idx := Length(AROSThreadStruct^.ThreadList);
  320. SetLength(AROSThreadStruct^.ThreadList, Idx + 1);
  321. end;
  322. Inc(AROSThreadStruct^.LastThreadNum);
  323. AROSThreadStruct^.ThreadList[Idx] := NewThread;
  324. NewThread^.ThreadID := AROSThreadStruct^.LastThreadNum;
  325. NewThread^.Entry := Entry;
  326. NewThread^.Data := Data;
  327. NewThread^.Priority := Priority;
  328. NewThread^.StackSize := StackSize;
  329. InitSemaphore(@(NewThread^.Lock));
  330. InitSemaphore(@(NewThread^.StartupSemaphore));
  331. NewThread^.EndCondition := CreateCondition;
  332. NewThread^.EndMutex := CreateMutex;
  333. NewThread^.EndCount := 0;
  334. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  335. ObtainSemaphore(@AROSThreadStruct^.ThreadMemSem);
  336. // Semaphore for too fast startup
  337. ObtainSemaphore(@(NewThread^.StartupSemaphore));
  338. // a very ugly Bugfix, for crashing AROS, on the very first Task after reboot
  339. // recheck later if can be removed
  340. if NewThread^.ThreadID = 1 then
  341. begin
  342. //debugln('make empty thread');
  343. ObtainSemaphore(@AROSThreadStruct^.EmptySemaphore);
  344. NewThread^.Task := CreateNewProcTags([
  345. NP_Entry, PtrUInt(@EmptyFunc),
  346. TAG_DONE, TAG_END]);
  347. ObtainSemaphore(@AROSThreadStruct^.EmptySemaphore);
  348. Delay(1);
  349. end;
  350. //
  351. NewThread^.Task := CreateNewProcTags([
  352. NP_Entry, PtrUInt(@StarterFunc),
  353. //NP_Name, PtrUInt(PChar('Thread' + IntToStr(LastThreadNum))),
  354. //NP_StackSize, 10024 * 1024,
  355. NP_Priority, Priority,
  356. NP_UserData, PtrUInt(NewThread),
  357. TAG_DONE, TAG_END]);
  358. Result := NewThread^.ThreadID;
  359. end;
  360. function AROSCurrentThread: LongInt;
  361. var
  362. Task: PProcess;
  363. i: Integer;
  364. begin
  365. Result := 0;
  366. Task := PProcess(FindTask(nil));
  367. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  368. for i := 0 to High(AROSThreadStruct^.ThreadList) do
  369. begin
  370. if Assigned(AROSThreadStruct^.ThreadList[i]) then
  371. begin
  372. if AROSThreadStruct^.ThreadList[i]^.Task = Task then
  373. begin
  374. Result := AROSThreadStruct^.ThreadList[i]^.ThreadID;
  375. Break;
  376. end;
  377. end;
  378. end;
  379. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  380. end;
  381. function AROSWaitThread(ThreadID: LongWord): Boolean;
  382. var
  383. Thread: PAROSThread;
  384. Idx, i: Integer;
  385. begin
  386. if not Assigned(AROSThreadStruct) then
  387. Exit;
  388. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  389. Thread := nil;
  390. Idx := -1;
  391. for i := 0 to High(AROSThreadStruct^.ThreadList) do
  392. begin
  393. if Assigned(AROSThreadStruct^.ThreadList[i]) then
  394. begin
  395. if AROSThreadStruct^.ThreadList[i]^.ThreadID = ThreadID then
  396. begin
  397. Thread := AROSThreadStruct^.ThreadList[i];
  398. Idx := i;
  399. break;
  400. end;
  401. end;
  402. end;
  403. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  404. if Thread = nil then
  405. begin
  406. //debugln('Thread not found');
  407. Result := False;
  408. Exit;
  409. end;
  410. // check some
  411. ObtainSemaphore(@Thread^.Lock);
  412. // hmm thats me... I do not wait for myself
  413. if Thread^.Task = PProcess(FindTask(nil)) then
  414. begin
  415. //debugln(' hmm its me :O ' + IntToStr(ThreadID));
  416. ReleaseSemaphore(@Thread^.Lock);
  417. Result := False;
  418. Exit;
  419. end;
  420. // wait that the thread start is finished somehow ;)
  421. ObtainSemaphore(@(Thread^.StartupSemaphore));
  422. ReleaseSemaphore(@(Thread^.StartupSemaphore));
  423. // check if Task is still running
  424. if Thread^.Task <> nil then
  425. begin
  426. Inc(Thread^.EndCount);
  427. ReleaseSemaphore(@Thread^.Lock);
  428. LockMutex(Thread^.EndMutex);
  429. //debugln(' Wait condition ' + IntToStr(ThreadID));
  430. WaitCondition(Thread^.EndCondition, Thread^.EndMutex);
  431. //debugln(' got condition ' + IntToStr(ThreadID));
  432. UnlockMutex(Thread^.EndMutex);
  433. ObtainSemaphore(@Thread^.Lock);
  434. Dec(Thread^.EndCount);
  435. end;
  436. if Thread^.EndCount > 0 then
  437. begin
  438. ReleaseSemaphore(@Thread^.Lock);
  439. Result := True;
  440. Exit;
  441. end;
  442. if Assigned(AROSThreadStruct) then
  443. begin
  444. // destroy Thread
  445. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  446. AROSThreadStruct^.ThreadList[Idx] := nil;
  447. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  448. end;
  449. DestroyCondition(Thread^.EndCondition);
  450. DestroyMutex(Thread^.EndMutex);
  451. Dispose(Thread);
  452. Result := true;
  453. end;
  454. function AROSCurrentThread: LongWord;
  455. var
  456. i: Integer;
  457. CurTask: PProcess;
  458. begin
  459. if not Assigned(AROSThreadStruct) then
  460. Exit;
  461. Result := 0;
  462. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  463. CurTask := PProcess(FindTask(nil));
  464. for i := 0 to High(AROSThreadStruct^.ThreadList) do
  465. begin
  466. if Assigned(AROSThreadStruct^.ThreadList[i]) then
  467. begin
  468. if AROSThreadStruct^.ThreadList[i]^.Task = CurTask then
  469. begin
  470. Result := AROSThreadStruct^.ThreadList[i]^.ThreadID;
  471. Break;
  472. end;
  473. end;
  474. end;
  475. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  476. end;
  477. procedure WaitAllThreads;
  478. var
  479. i: Integer;
  480. TID: LongWord;
  481. begin
  482. if not Assigned(AROSThreadStruct) then
  483. Exit;
  484. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  485. i := 0;
  486. while i <= High(AROSThreadStruct^.ThreadList) do
  487. begin
  488. if Assigned(AROSThreadStruct^.ThreadList[i]) then
  489. begin
  490. TID := AROSThreadStruct^.ThreadList[i]^.ThreadID;
  491. //
  492. ObtainSemaphore(@(AROSThreadStruct^.ThreadList[i]^.StartupSemaphore));
  493. ReleaseSemaphore(@(AROSThreadStruct^.ThreadList[i]^.StartupSemaphore));
  494. //
  495. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  496. AROSWaitThread(TID);
  497. ObtainSemaphore(@AROSThreadStruct^.ThreadListSem);
  498. end;
  499. Inc(i);
  500. end;
  501. ReleaseSemaphore(@AROSThreadStruct^.ThreadListSem);
  502. end;
  503. {$ifdef THREAD_SYSTEM}
  504. procedure InitThreadLib;
  505. begin
  506. New(AROSThreadStruct);
  507. AROSThreadStruct^.LastThreadNum := 0;
  508. InitSemaphore(@(AROSThreadStruct^.MutexListSem));
  509. InitSemaphore(@(AROSThreadStruct^.ConditionListSem));
  510. InitSemaphore(@(AROSThreadStruct^.ThreadListSem));
  511. InitSemaphore(@(AROSThreadStruct^.ThreadMemSem));
  512. InitSemaphore(@(AROSThreadStruct^.EmptySemaphore));
  513. end;
  514. procedure FinishThreadLib;
  515. var
  516. i: Integer;
  517. begin
  518. if not Assigned(AROSThreadStruct) then
  519. Exit;
  520. WaitAllThreads;
  521. ObtainSemaphore(@AROSThreadStruct^.MutexListSem);
  522. i := 0;
  523. for i := 0 to High(AROSThreadStruct^.MutexList) do
  524. begin
  525. if Assigned(AROSThreadStruct^.MutexList[i]) then
  526. begin
  527. Dispose(AROSThreadStruct^.MutexList[i]);
  528. end;
  529. end;
  530. ReleaseSemaphore(@AROSThreadStruct^.MutexListSem);
  531. ObtainSemaphore(@AROSThreadStruct^.ConditionListSem);
  532. i := 0;
  533. for i := 0 to High(AROSThreadStruct^.ConditionList) do
  534. begin
  535. if Assigned(AROSThreadStruct^.ConditionList[i]) then
  536. begin
  537. Dispose(AROSThreadStruct^.ConditionList[i]);
  538. end;
  539. end;
  540. ReleaseSemaphore(@AROSThreadStruct^.ConditionListSem);
  541. Dispose(AROSThreadStruct);
  542. AROSThreadStruct := nil;
  543. end;
  544. {$endif THREAD_SYSTEM}