thread.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. procedure InitThread;
  88. begin
  89. { we should find a reasonable value here }
  90. InitThread($1000000);
  91. end;
  92. {*****************************************************************************
  93. Overloaded functions
  94. *****************************************************************************}
  95. function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
  96. var
  97. dummy : TThreadID;
  98. begin
  99. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,nil,0,dummy);
  100. end;
  101. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
  102. var
  103. dummy : TThreadID;
  104. begin
  105. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,dummy);
  106. end;
  107. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;var ThreadId : TThreadID) : TThreadID;
  108. begin
  109. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,ThreadId);
  110. end;
  111. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  112. var ThreadId : TThreadID; const stacksize: SizeUInt) : TThreadID;
  113. begin
  114. BeginThread:=BeginThread(nil,stacksize,ThreadFunction,p,0,ThreadId);
  115. end;
  116. procedure EndThread;
  117. begin
  118. EndThread(0);
  119. end;
  120. function BeginThread(sa : Pointer;stacksize : SizeUInt; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  121. begin
  122. Result:=CurrentTM.BeginThread(sa,stacksize,threadfunction,P,creationflags,ThreadID);
  123. end;
  124. procedure FlushThread;
  125. begin
  126. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  127. SysFlushStdio;
  128. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  129. end;
  130. procedure EndThread(ExitCode : DWord);
  131. begin
  132. CurrentTM.EndThread(ExitCode);
  133. end;
  134. function SuspendThread (threadHandle : TThreadID) : dword;
  135. begin
  136. Result:=CurrentTM.SuspendThread(ThreadHandle);
  137. end;
  138. function ResumeThread (threadHandle : TThreadID) : dword;
  139. begin
  140. Result:=CurrentTM.ResumeThread(ThreadHandle);
  141. end;
  142. function CloseThread (threadHandle : TThreadID):dword;
  143. begin
  144. result:=CurrentTM.CloseThread(ThreadHandle);
  145. end;
  146. procedure ThreadSwitch;
  147. begin
  148. CurrentTM.ThreadSwitch;
  149. end;
  150. function KillThread (threadHandle : TThreadID) : dword;
  151. begin
  152. Result:=CurrentTM.KillThread(ThreadHandle);
  153. end;
  154. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword;
  155. begin
  156. Result:=CurrentTM.WaitForThreadTerminate(ThreadHandle,TimeOutMS);
  157. end;
  158. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean;
  159. begin
  160. Result:=CurrentTM.ThreadSetPriority(ThreadHandle,Prio);
  161. end;
  162. function ThreadGetPriority (threadHandle : TThreadID): longint;
  163. begin
  164. Result:=CurrentTM.ThreadGetPriority(ThreadHandle);
  165. end;
  166. function GetCurrentThreadId : TThreadID;
  167. begin
  168. Result:=CurrentTM.GetCurrentThreadID();
  169. end;
  170. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: AnsiString);
  171. begin
  172. CurrentTM.SetThreadDebugNameA(threadHandle, ThreadName);
  173. end;
  174. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  175. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: UnicodeString);
  176. begin
  177. CurrentTM.SetThreadDebugNameU(threadHandle, ThreadName);
  178. end;
  179. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  180. procedure InitCriticalSection(out cs : TRTLCriticalSection);
  181. begin
  182. CurrentTM.InitCriticalSection(cs);
  183. end;
  184. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  185. begin
  186. CurrentTM.DoneCriticalSection(cs);
  187. end;
  188. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  189. begin
  190. CurrentTM.EnterCriticalSection(cs);
  191. end;
  192. function TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  193. begin
  194. result:=CurrentTM.TryEnterCriticalSection(cs);
  195. end;
  196. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  197. begin
  198. CurrentTM.LeaveCriticalSection(cs);
  199. end;
  200. Function GetThreadManager(Out TM : TThreadManager) : Boolean;
  201. begin
  202. TM:=CurrentTM;
  203. Result:=True;
  204. end;
  205. Function SetThreadManager(Const NewTM : TThreadManager; Out OldTM : TThreadManager) : Boolean;
  206. begin
  207. GetThreadManager(OldTM);
  208. Result:=SetThreadManager(NewTM);
  209. end;
  210. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  211. begin
  212. Result:=True;
  213. If Assigned(CurrentTM.DoneManager) then
  214. Result:=CurrentTM.DoneManager();
  215. If Result then
  216. begin
  217. CurrentTM:=NewTM;
  218. If Assigned(CurrentTM.InitManager) then
  219. Result:=CurrentTM.InitManager();
  220. end;
  221. end;
  222. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  223. begin
  224. result:=currenttm.BasicEventCreate(EventAttributes,AManualReset,InitialState, Name);
  225. end;
  226. procedure BasicEventDestroy(state:peventstate);
  227. begin
  228. currenttm.BasicEventDestroy(state);
  229. end;
  230. procedure BasicEventResetEvent(state:peventstate);
  231. begin
  232. currenttm.BasicEventResetEvent(state);
  233. end;
  234. procedure BasicEventSetEvent(state:peventstate);
  235. begin
  236. currenttm.BasicEventSetEvent(state);
  237. end;
  238. function BasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  239. begin
  240. result:=currenttm.BasicEventWaitFor(Timeout,state);
  241. end;
  242. function RTLEventCreate :PRTLEvent;
  243. begin
  244. result:=currenttm.RTLEventCreate();
  245. end;
  246. procedure RTLeventDestroy(state:pRTLEvent);
  247. begin
  248. currenttm.RTLEventDestroy(state);
  249. end;
  250. procedure RTLeventSetEvent(state:pRTLEvent);
  251. begin
  252. currenttm.RTLEventSetEvent(state);
  253. end;
  254. procedure RTLeventResetEvent(state:pRTLEvent);
  255. begin
  256. currenttm.RTLEventResetEvent(state);
  257. end;
  258. procedure RTLeventWaitFor(state:pRTLEvent);
  259. begin
  260. currenttm.RTLEventWaitFor(state);
  261. end;
  262. procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
  263. begin
  264. currenttm.RTLEventWaitForTimeout(state,timeout);
  265. end;
  266. { ---------------------------------------------------------------------
  267. ThreadManager which gives run-time error. Use if no thread support.
  268. ---------------------------------------------------------------------}
  269. {$ifndef DISABLE_NO_THREAD_MANAGER}
  270. { resourcestrings are not supported by the system unit,
  271. they are in the objpas unit and not available for fpc/tp modes }
  272. const
  273. SNoThreads = 'This binary has no thread support compiled in.';
  274. SRecompileWithThreads = 'Recompile the application with a thread-driver in the program uses clause before other units using thread.';
  275. Procedure NoThreadError;
  276. begin
  277. {$ifndef EMBEDDED}
  278. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  279. If IsConsole then
  280. begin
  281. Writeln(StdErr,SNoThreads);
  282. Writeln(StdErr,SRecompileWithThreads);
  283. end;
  284. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  285. {$endif EMBEDDED}
  286. RunError(232)
  287. end;
  288. function NoBeginThread(sa : Pointer;stacksize : PtrUInt;
  289. ThreadFunction : tthreadfunc;p : pointer;
  290. creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  291. begin
  292. NoThreadError;
  293. result:=tthreadid(-1);
  294. end;
  295. procedure NoEndThread(ExitCode : DWord);
  296. begin
  297. NoThreadError;
  298. end;
  299. function NoThreadHandler (threadHandle : TThreadID) : dword;
  300. begin
  301. NoThreadError;
  302. result:=dword(-1);
  303. end;
  304. function NoWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  305. begin
  306. NoThreadError;
  307. result:=dword(-1);
  308. end;
  309. function NoThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  310. begin
  311. NoThreadError;
  312. result:=false;
  313. end;
  314. function NoThreadGetPriority (threadHandle : TThreadID): longint;
  315. begin
  316. NoThreadError;
  317. result:=-1;
  318. end;
  319. function NoGetCurrentThreadId : TThreadID;
  320. begin
  321. if IsMultiThread then
  322. NoThreadError
  323. else
  324. ThreadingAlreadyUsed:=true;
  325. result:=TThreadID(1);
  326. end;
  327. procedure NoSetThreadDebugNameA(threadHandle: TThreadID; const ThreadName: AnsiString);
  328. begin
  329. NoThreadError;
  330. end;
  331. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  332. procedure NoSetThreadDebugNameU(threadHandle: TThreadID; const ThreadName: UnicodeString);
  333. begin
  334. NoThreadError;
  335. end;
  336. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  337. procedure NoCriticalSection(var CS);
  338. begin
  339. if IsMultiThread then
  340. NoThreadError
  341. else
  342. ThreadingAlreadyUsed:=true;
  343. end;
  344. function NoTryEnterCriticalSection(var CS):longint;
  345. begin
  346. if IsMultiThread then
  347. NoThreadError
  348. else
  349. ThreadingAlreadyUsed:=true;
  350. Result:=-1;
  351. end;
  352. procedure NoInitThreadvar(var offset : {$ifdef cpu16}word{$else}dword{$endif};size : dword);
  353. begin
  354. NoThreadError;
  355. end;
  356. function NoRelocateThreadvar(offset : {$ifdef cpu16}word{$else}dword{$endif}) : pointer;
  357. begin
  358. NoThreadError;
  359. result:=nil;
  360. end;
  361. function NoBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  362. begin
  363. if IsMultiThread then
  364. NoThreadError
  365. else
  366. ThreadingAlreadyUsed:=true;
  367. result:=nil;
  368. end;
  369. procedure NoBasicEvent(state:peventstate);
  370. begin
  371. if IsMultiThread then
  372. NoThreadError
  373. else
  374. ThreadingAlreadyUsed:=true;
  375. end;
  376. function NoBasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  377. begin
  378. if IsMultiThread then
  379. NoThreadError
  380. else
  381. ThreadingAlreadyUsed:=true;
  382. result:=-1;
  383. end;
  384. function NoRTLEventCreate :PRTLEvent;
  385. begin
  386. if IsMultiThread then
  387. NoThreadError
  388. else
  389. ThreadingAlreadyUsed:=true;
  390. result:=nil;
  391. end;
  392. procedure NoRTLEvent(state:pRTLEvent);
  393. begin
  394. if IsMultiThread then
  395. NoThreadError
  396. else
  397. ThreadingAlreadyUsed:=true
  398. end;
  399. procedure NoRTLEventWaitForTimeout(state:pRTLEvent;timeout : longint);
  400. begin
  401. if IsMultiThread then
  402. NoThreadError
  403. else
  404. ThreadingAlreadyUsed:=true;
  405. end;
  406. const
  407. NoThreadManager : TThreadManager = (
  408. InitManager : Nil;
  409. DoneManager : Nil;
  410. {$ifdef EMBEDDED}
  411. { while this is pretty hacky, it reduces the size of typical embedded programs
  412. and works fine on arm and avr }
  413. BeginThread : @NoBeginThread;
  414. EndThread : TEndThreadHandler(@NoThreadError);
  415. SuspendThread : TThreadHandler(@NoThreadError);
  416. ResumeThread : TThreadHandler(@NoThreadError);
  417. KillThread : TThreadHandler(@NoThreadError);
  418. CloseThread : TThreadHandler(@NoThreadError);
  419. ThreadSwitch : TThreadSwitchHandler(@NoThreadError);
  420. WaitForThreadTerminate : TWaitForThreadTerminateHandler(@NoThreadError);
  421. ThreadSetPriority : TThreadSetPriorityHandler(@NoThreadError);
  422. ThreadGetPriority : TThreadGetPriorityHandler(@NoThreadError);
  423. GetCurrentThreadId : @NoGetCurrentThreadId;
  424. SetThreadDebugNameA : TThreadSetThreadDebugNameHandlerA(@NoThreadError);
  425. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  426. SetThreadDebugNameU : TThreadSetThreadDebugNameHandlerU(@NoThreadError);
  427. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  428. InitCriticalSection : TCriticalSectionHandler(@NoThreadError);
  429. DoneCriticalSection : TCriticalSectionHandler(@NoThreadError);
  430. EnterCriticalSection : TCriticalSectionHandler(@NoThreadError);
  431. TryEnterCriticalSection: TCriticalSectionHandlerTryEnter(@NoThreadError);
  432. LeaveCriticalSection : TCriticalSectionHandler(@NoThreadError);
  433. InitThreadVar : TInitThreadVarHandler(@NoThreadError);
  434. RelocateThreadVar : TRelocateThreadVarHandler(@NoThreadError);
  435. AllocateThreadVars : @NoThreadError;
  436. ReleaseThreadVars : @NoThreadError;
  437. BasicEventCreate : TBasicEventCreateHandler(@NoThreadError);
  438. BasicEventdestroy : TBasicEventHandler(@NoThreadError);
  439. BasicEventResetEvent : TBasicEventHandler(@NoThreadError);
  440. BasicEventSetEvent : TBasicEventHandler(@NoThreadError);
  441. BasicEventWaitFor : TBasicEventWaitForHandler(@NoThreadError);
  442. RTLEventCreate : TRTLCreateEventHandler(@NoThreadError);
  443. RTLEventdestroy : TRTLEventHandler(@NoThreadError);
  444. RTLEventSetEvent : TRTLEventHandler(@NoThreadError);
  445. RTLEventResetEvent : TRTLEventHandler(@NoThreadError);
  446. RTLEventWaitFor : TRTLEventHandler(@NoThreadError);
  447. RTLEventwaitfortimeout : TRTLEventHandlerTimeout(@NoThreadError);
  448. {$else EMBEDDED}
  449. BeginThread : @NoBeginThread;
  450. EndThread : @NoEndThread;
  451. SuspendThread : @NoThreadHandler;
  452. ResumeThread : @NoThreadHandler;
  453. KillThread : @NoThreadHandler;
  454. CloseThread : @NoThreadHandler;
  455. ThreadSwitch : @NoThreadError;
  456. WaitForThreadTerminate : @NoWaitForThreadTerminate;
  457. ThreadSetPriority : @NoThreadSetPriority;
  458. ThreadGetPriority : @NoThreadGetPriority;
  459. GetCurrentThreadId : @NoGetCurrentThreadId;
  460. SetThreadDebugNameA : @NoSetThreadDebugNameA;
  461. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  462. SetThreadDebugNameU : @NoSetThreadDebugNameU;
  463. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  464. InitCriticalSection : @NoCriticalSection;
  465. DoneCriticalSection : @NoCriticalSection;
  466. EnterCriticalSection : @NoCriticalSection;
  467. TryEnterCriticalSection: @NoTryEnterCriticalSection;
  468. LeaveCriticalSection : @NoCriticalSection;
  469. InitThreadVar : @NoInitThreadVar;
  470. RelocateThreadVar : @NoRelocateThreadVar;
  471. AllocateThreadVars : @NoThreadError;
  472. ReleaseThreadVars : @NoThreadError;
  473. BasicEventCreate : @NoBasicEventCreate;
  474. BasicEventDestroy : @NoBasicEvent;
  475. BasicEventResetEvent : @NoBasicEvent;
  476. BasicEventSetEvent : @NoBasicEvent;
  477. BasicEventWaitFor : @NoBasiceventWaitFor;
  478. RTLEventCreate : @NoRTLEventCreate;
  479. RTLEventDestroy : @NoRTLevent;
  480. RTLEventSetEvent : @NoRTLevent;
  481. RTLEventResetEvent : @NoRTLEvent;
  482. RTLEventWaitFor : @NoRTLEvent;
  483. RTLEventWaitforTimeout : @NoRTLEventWaitForTimeout;
  484. {$endif EMBEDDED}
  485. );
  486. Procedure SetNoThreadManager;
  487. begin
  488. SetThreadManager(NoThreadManager);
  489. end;
  490. Procedure InitSystemThreads; public name '_FPC_InitSystemThreads';
  491. begin
  492. { This should be changed to a real value during
  493. thread driver initialization if appropriate. }
  494. ThreadID := TThreadID(1);
  495. SetNoThreadManager;
  496. end;
  497. {$endif DISABLE_NO_THREAD_MANAGER}