thread.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2000 by the Free Pascal development team
  4. OS independent thread functions/overloads
  5. See the File COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Var
  12. CurrentTM : TThreadManager;
  13. {$ifndef THREADVAR_RELOCATED_ALREADY_DEFINED}
  14. fpc_threadvar_relocate_proc : TRelocateThreadVarHandler; public name 'FPC_THREADVAR_RELOCATE';
  15. {$endif THREADVAR_RELOCATED_ALREADY_DEFINED}
  16. {$ifndef HAS_GETCPUCOUNT}
  17. function GetCPUCount: LongWord;
  18. begin
  19. Result := 1;
  20. end;
  21. {$endif}
  22. {*****************************************************************************
  23. Threadvar initialization
  24. *****************************************************************************}
  25. procedure InitThread(stklen:SizeUInt);
  26. begin
  27. {$ifndef FPUNONE}
  28. SysResetFPU;
  29. SysInitFPU;
  30. {$endif}
  31. {$ifndef HAS_MEMORYMANAGER}
  32. {$ifndef FPC_NO_DEFAULT_HEAP}
  33. { initialize this thread's heap }
  34. InitHeapThread;
  35. {$endif ndef FPC_NO_DEFAULT_HEAP}
  36. {$else HAS_MEMORYMANAGER}
  37. if MemoryManager.InitThread <> nil then
  38. MemoryManager.InitThread();
  39. {$endif HAS_MEMORYMANAGER}
  40. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  41. if assigned(widestringmanager.ThreadInitProc) then
  42. widestringmanager.ThreadInitProc;
  43. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  44. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  45. { ExceptAddrStack and ExceptObjectStack are threadvars }
  46. { so every thread has its on exception handling capabilities }
  47. SysInitExceptions;
  48. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  49. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  50. {$ifndef EMBEDDED}
  51. { Open all stdio fds again }
  52. SysInitStdio;
  53. InOutRes:=0;
  54. // ErrNo:=0;
  55. {$endif EMBEDDED}
  56. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  57. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  58. { Stack checking }
  59. StackLength:= CheckInitialStkLen(stkLen);
  60. StackBottom:=Sptr - StackLength;
  61. {$endif FPC_HAS_FEATURE_STACKCHECK}
  62. ThreadID := CurrentTM.GetCurrentThreadID();
  63. end;
  64. procedure DoneThread;
  65. begin
  66. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  67. if assigned(widestringmanager.ThreadFiniProc) then
  68. widestringmanager.ThreadFiniProc;
  69. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  70. {$ifndef HAS_MEMORYMANAGER}
  71. {$ifndef FPC_NO_DEFAULT_HEAP}
  72. FinalizeHeap;
  73. {$endif ndef FPC_NO_DEFAULT_HEAP}
  74. {$endif HAS_MEMORYMANAGER}
  75. if MemoryManager.DoneThread <> nil then
  76. MemoryManager.DoneThread();
  77. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  78. { Open all stdio fds again }
  79. SysFlushStdio;
  80. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  81. { Support platforms where threadvar memory is managed outside of the RTL:
  82. reset ThreadID and allow ReleaseThreadVars to be unassigned }
  83. ThreadID := TThreadID(0);
  84. if assigned(CurrentTM.ReleaseThreadVars) then
  85. CurrentTM.ReleaseThreadVars;
  86. end;
  87. {*****************************************************************************
  88. Overloaded functions
  89. *****************************************************************************}
  90. function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
  91. var
  92. dummy : TThreadID;
  93. begin
  94. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,nil,0,dummy);
  95. end;
  96. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
  97. var
  98. dummy : TThreadID;
  99. begin
  100. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,dummy);
  101. end;
  102. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;var ThreadId : TThreadID) : TThreadID;
  103. begin
  104. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,ThreadId);
  105. end;
  106. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  107. var ThreadId : TThreadID; const stacksize: SizeUInt) : TThreadID;
  108. begin
  109. BeginThread:=BeginThread(nil,stacksize,ThreadFunction,p,0,ThreadId);
  110. end;
  111. procedure EndThread;
  112. begin
  113. EndThread(0);
  114. end;
  115. function BeginThread(sa : Pointer;stacksize : SizeUInt; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  116. begin
  117. Result:=CurrentTM.BeginThread(sa,stacksize,threadfunction,P,creationflags,ThreadID);
  118. end;
  119. procedure FlushThread;
  120. begin
  121. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  122. SysFlushStdio;
  123. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  124. end;
  125. procedure EndThread(ExitCode : DWord);
  126. begin
  127. CurrentTM.EndThread(ExitCode);
  128. end;
  129. function SuspendThread (threadHandle : TThreadID) : dword;
  130. begin
  131. Result:=CurrentTM.SuspendThread(ThreadHandle);
  132. end;
  133. function ResumeThread (threadHandle : TThreadID) : dword;
  134. begin
  135. Result:=CurrentTM.ResumeThread(ThreadHandle);
  136. end;
  137. function CloseThread (threadHandle : TThreadID):dword;
  138. begin
  139. result:=CurrentTM.CloseThread(ThreadHandle);
  140. end;
  141. procedure ThreadSwitch;
  142. begin
  143. CurrentTM.ThreadSwitch;
  144. end;
  145. function KillThread (threadHandle : TThreadID) : dword;
  146. begin
  147. Result:=CurrentTM.KillThread(ThreadHandle);
  148. end;
  149. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword;
  150. begin
  151. Result:=CurrentTM.WaitForThreadTerminate(ThreadHandle,TimeOutMS);
  152. end;
  153. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean;
  154. begin
  155. Result:=CurrentTM.ThreadSetPriority(ThreadHandle,Prio);
  156. end;
  157. function ThreadGetPriority (threadHandle : TThreadID): longint;
  158. begin
  159. Result:=CurrentTM.ThreadGetPriority(ThreadHandle);
  160. end;
  161. function GetCurrentThreadId : TThreadID;
  162. begin
  163. Result:=CurrentTM.GetCurrentThreadID();
  164. end;
  165. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: AnsiString);
  166. begin
  167. CurrentTM.SetThreadDebugNameA(threadHandle, ThreadName);
  168. end;
  169. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  170. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: UnicodeString);
  171. begin
  172. CurrentTM.SetThreadDebugNameU(threadHandle, ThreadName);
  173. end;
  174. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  175. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  176. begin
  177. CurrentTM.InitCriticalSection(cs);
  178. end;
  179. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  180. begin
  181. CurrentTM.DoneCriticalSection(cs);
  182. end;
  183. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  184. begin
  185. CurrentTM.EnterCriticalSection(cs);
  186. end;
  187. function TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  188. begin
  189. result:=CurrentTM.TryEnterCriticalSection(cs);
  190. end;
  191. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  192. begin
  193. CurrentTM.LeaveCriticalSection(cs);
  194. end;
  195. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  196. begin
  197. TM:=CurrentTM;
  198. Result:=True;
  199. end;
  200. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  201. begin
  202. GetThreadManager(OldTM);
  203. Result:=SetThreadManager(NewTM);
  204. end;
  205. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  206. begin
  207. Result:=True;
  208. If Assigned(CurrentTM.DoneManager) then
  209. Result:=CurrentTM.DoneManager();
  210. If Result then
  211. begin
  212. CurrentTM:=NewTM;
  213. If Assigned(CurrentTM.InitManager) then
  214. Result:=CurrentTM.InitManager();
  215. end;
  216. end;
  217. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  218. begin
  219. result:=currenttm.BasicEventCreate(EventAttributes,AManualReset,InitialState, Name);
  220. end;
  221. procedure BasicEventDestroy(state:peventstate);
  222. begin
  223. currenttm.BasicEventDestroy(state);
  224. end;
  225. procedure BasicEventResetEvent(state:peventstate);
  226. begin
  227. currenttm.BasicEventResetEvent(state);
  228. end;
  229. procedure BasicEventSetEvent(state:peventstate);
  230. begin
  231. currenttm.BasicEventSetEvent(state);
  232. end;
  233. function BasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  234. begin
  235. result:=currenttm.BasicEventWaitFor(Timeout,state);
  236. end;
  237. function RTLEventCreate :PRTLEvent;
  238. begin
  239. result:=currenttm.RTLEventCreate();
  240. end;
  241. procedure RTLeventDestroy(state:pRTLEvent);
  242. begin
  243. currenttm.RTLEventDestroy(state);
  244. end;
  245. procedure RTLeventSetEvent(state:pRTLEvent);
  246. begin
  247. currenttm.RTLEventSetEvent(state);
  248. end;
  249. procedure RTLeventResetEvent(state:pRTLEvent);
  250. begin
  251. currenttm.RTLEventResetEvent(state);
  252. end;
  253. procedure RTLeventWaitFor(state:pRTLEvent);
  254. begin
  255. currenttm.RTLEventWaitFor(state);
  256. end;
  257. procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
  258. begin
  259. currenttm.RTLEventWaitForTimeout(state,timeout);
  260. end;
  261. { ---------------------------------------------------------------------
  262. ThreadManager which gives run-time error. Use if no thread support.
  263. ---------------------------------------------------------------------}
  264. {$ifndef DISABLE_NO_THREAD_MANAGER}
  265. { resourcestrings are not supported by the system unit,
  266. they are in the objpas unit and not available for fpc/tp modes }
  267. const
  268. SNoThreads = 'This binary has no thread support compiled in.';
  269. SRecompileWithThreads = 'Recompile the application with a thread-driver in the program uses clause before other units using thread.';
  270. Procedure NoThreadError;
  271. begin
  272. {$ifndef EMBEDDED}
  273. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  274. If IsConsole then
  275. begin
  276. Writeln(StdErr,SNoThreads);
  277. Writeln(StdErr,SRecompileWithThreads);
  278. end;
  279. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  280. {$endif EMBEDDED}
  281. RunError(232)
  282. end;
  283. function NoBeginThread(sa : Pointer;stacksize : PtrUInt;
  284. ThreadFunction : tthreadfunc;p : pointer;
  285. creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  286. begin
  287. NoThreadError;
  288. result:=tthreadid(-1);
  289. end;
  290. procedure NoEndThread(ExitCode : DWord);
  291. begin
  292. NoThreadError;
  293. end;
  294. function NoThreadHandler (threadHandle : TThreadID) : dword;
  295. begin
  296. NoThreadError;
  297. result:=dword(-1);
  298. end;
  299. function NoWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  300. begin
  301. NoThreadError;
  302. result:=dword(-1);
  303. end;
  304. function NoThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  305. begin
  306. NoThreadError;
  307. result:=false;
  308. end;
  309. function NoThreadGetPriority (threadHandle : TThreadID): longint;
  310. begin
  311. NoThreadError;
  312. result:=-1;
  313. end;
  314. function NoGetCurrentThreadId : TThreadID;
  315. begin
  316. if IsMultiThread then
  317. NoThreadError
  318. else
  319. ThreadingAlreadyUsed:=true;
  320. result:=TThreadID(1);
  321. end;
  322. procedure NoSetThreadDebugNameA(threadHandle: TThreadID; const ThreadName: AnsiString);
  323. begin
  324. NoThreadError;
  325. end;
  326. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  327. procedure NoSetThreadDebugNameU(threadHandle: TThreadID; const ThreadName: UnicodeString);
  328. begin
  329. NoThreadError;
  330. end;
  331. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  332. procedure NoCriticalSection(var CS);
  333. begin
  334. if IsMultiThread then
  335. NoThreadError
  336. else
  337. ThreadingAlreadyUsed:=true;
  338. end;
  339. function NoTryEnterCriticalSection(var CS):longint;
  340. begin
  341. if IsMultiThread then
  342. NoThreadError
  343. else
  344. ThreadingAlreadyUsed:=true;
  345. Result:=-1;
  346. end;
  347. procedure NoInitThreadvar(var offset : {$ifdef cpu16}word{$else}dword{$endif};size : dword);
  348. begin
  349. NoThreadError;
  350. end;
  351. function NoRelocateThreadvar(offset : {$ifdef cpu16}word{$else}dword{$endif}) : pointer;
  352. begin
  353. NoThreadError;
  354. result:=nil;
  355. end;
  356. function NoBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  357. begin
  358. if IsMultiThread then
  359. NoThreadError
  360. else
  361. ThreadingAlreadyUsed:=true;
  362. result:=nil;
  363. end;
  364. procedure NoBasicEvent(state:peventstate);
  365. begin
  366. if IsMultiThread then
  367. NoThreadError
  368. else
  369. ThreadingAlreadyUsed:=true;
  370. end;
  371. function NoBasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  372. begin
  373. if IsMultiThread then
  374. NoThreadError
  375. else
  376. ThreadingAlreadyUsed:=true;
  377. result:=-1;
  378. end;
  379. function NoRTLEventCreate :PRTLEvent;
  380. begin
  381. if IsMultiThread then
  382. NoThreadError
  383. else
  384. ThreadingAlreadyUsed:=true;
  385. result:=nil;
  386. end;
  387. procedure NoRTLEvent(state:pRTLEvent);
  388. begin
  389. if IsMultiThread then
  390. NoThreadError
  391. else
  392. ThreadingAlreadyUsed:=true
  393. end;
  394. procedure NoRTLEventWaitForTimeout(state:pRTLEvent;timeout : longint);
  395. begin
  396. if IsMultiThread then
  397. NoThreadError
  398. else
  399. ThreadingAlreadyUsed:=true;
  400. end;
  401. const
  402. NoThreadManager : TThreadManager = (
  403. InitManager : Nil;
  404. DoneManager : Nil;
  405. {$ifdef EMBEDDED}
  406. { while this is pretty hacky, it reduces the size of typical embedded programs
  407. and works fine on arm and avr }
  408. BeginThread : @NoBeginThread;
  409. EndThread : TEndThreadHandler(@NoThreadError);
  410. SuspendThread : TThreadHandler(@NoThreadError);
  411. ResumeThread : TThreadHandler(@NoThreadError);
  412. KillThread : TThreadHandler(@NoThreadError);
  413. CloseThread : TThreadHandler(@NoThreadError);
  414. ThreadSwitch : TThreadSwitchHandler(@NoThreadError);
  415. WaitForThreadTerminate : TWaitForThreadTerminateHandler(@NoThreadError);
  416. ThreadSetPriority : TThreadSetPriorityHandler(@NoThreadError);
  417. ThreadGetPriority : TThreadGetPriorityHandler(@NoThreadError);
  418. GetCurrentThreadId : @NoGetCurrentThreadId;
  419. SetThreadDebugNameA : TThreadSetThreadDebugNameHandlerA(@NoThreadError);
  420. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  421. SetThreadDebugNameU : TThreadSetThreadDebugNameHandlerU(@NoThreadError);
  422. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  423. InitCriticalSection : TCriticalSectionHandler(@NoThreadError);
  424. DoneCriticalSection : TCriticalSectionHandler(@NoThreadError);
  425. EnterCriticalSection : TCriticalSectionHandler(@NoThreadError);
  426. TryEnterCriticalSection: TCriticalSectionHandlerTryEnter(@NoThreadError);
  427. LeaveCriticalSection : TCriticalSectionHandler(@NoThreadError);
  428. InitThreadVar : TInitThreadVarHandler(@NoThreadError);
  429. RelocateThreadVar : TRelocateThreadVarHandler(@NoThreadError);
  430. AllocateThreadVars : @NoThreadError;
  431. ReleaseThreadVars : @NoThreadError;
  432. BasicEventCreate : TBasicEventCreateHandler(@NoThreadError);
  433. BasicEventdestroy : TBasicEventHandler(@NoThreadError);
  434. BasicEventResetEvent : TBasicEventHandler(@NoThreadError);
  435. BasicEventSetEvent : TBasicEventHandler(@NoThreadError);
  436. BasicEventWaitFor : TBasicEventWaitForHandler(@NoThreadError);
  437. RTLEventCreate : TRTLCreateEventHandler(@NoThreadError);
  438. RTLEventdestroy : TRTLEventHandler(@NoThreadError);
  439. RTLEventSetEvent : TRTLEventHandler(@NoThreadError);
  440. RTLEventResetEvent : TRTLEventHandler(@NoThreadError);
  441. RTLEventWaitFor : TRTLEventHandler(@NoThreadError);
  442. RTLEventwaitfortimeout : TRTLEventHandlerTimeout(@NoThreadError);
  443. {$else EMBEDDED}
  444. BeginThread : @NoBeginThread;
  445. EndThread : @NoEndThread;
  446. SuspendThread : @NoThreadHandler;
  447. ResumeThread : @NoThreadHandler;
  448. KillThread : @NoThreadHandler;
  449. CloseThread : @NoThreadHandler;
  450. ThreadSwitch : @NoThreadError;
  451. WaitForThreadTerminate : @NoWaitForThreadTerminate;
  452. ThreadSetPriority : @NoThreadSetPriority;
  453. ThreadGetPriority : @NoThreadGetPriority;
  454. GetCurrentThreadId : @NoGetCurrentThreadId;
  455. SetThreadDebugNameA : @NoSetThreadDebugNameA;
  456. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  457. SetThreadDebugNameU : @NoSetThreadDebugNameU;
  458. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  459. InitCriticalSection : @NoCriticalSection;
  460. DoneCriticalSection : @NoCriticalSection;
  461. EnterCriticalSection : @NoCriticalSection;
  462. TryEnterCriticalSection: @NoTryEnterCriticalSection;
  463. LeaveCriticalSection : @NoCriticalSection;
  464. InitThreadVar : @NoInitThreadVar;
  465. RelocateThreadVar : @NoRelocateThreadVar;
  466. AllocateThreadVars : @NoThreadError;
  467. ReleaseThreadVars : @NoThreadError;
  468. BasicEventCreate : @NoBasicEventCreate;
  469. BasicEventDestroy : @NoBasicEvent;
  470. BasicEventResetEvent : @NoBasicEvent;
  471. BasicEventSetEvent : @NoBasicEvent;
  472. BasicEventWaitFor : @NoBasiceventWaitFor;
  473. RTLEventCreate : @NoRTLEventCreate;
  474. RTLEventDestroy : @NoRTLevent;
  475. RTLEventSetEvent : @NoRTLevent;
  476. RTLEventResetEvent : @NoRTLEvent;
  477. RTLEventWaitFor : @NoRTLEvent;
  478. RTLEventWaitforTimeout : @NoRTLEventWaitForTimeout;
  479. {$endif EMBEDDED}
  480. );
  481. Procedure SetNoThreadManager;
  482. begin
  483. SetThreadManager(NoThreadManager);
  484. end;
  485. Procedure InitSystemThreads; public name '_FPC_InitSystemThreads';
  486. begin
  487. { This should be changed to a real value during
  488. thread driver initialization if appropriate. }
  489. ThreadID := TThreadID(1);
  490. SetNoThreadManager;
  491. end;
  492. {$endif DISABLE_NO_THREAD_MANAGER}