thread.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 InitCriticalSection(var cs : TRTLCriticalSection);
  166. begin
  167. CurrentTM.InitCriticalSection(cs);
  168. end;
  169. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  170. begin
  171. CurrentTM.DoneCriticalSection(cs);
  172. end;
  173. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  174. begin
  175. CurrentTM.EnterCriticalSection(cs);
  176. end;
  177. function TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  178. begin
  179. result:=CurrentTM.TryEnterCriticalSection(cs);
  180. end;
  181. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  182. begin
  183. CurrentTM.LeaveCriticalSection(cs);
  184. end;
  185. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  186. begin
  187. TM:=CurrentTM;
  188. Result:=True;
  189. end;
  190. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  191. begin
  192. GetThreadManager(OldTM);
  193. Result:=SetThreadManager(NewTM);
  194. end;
  195. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  196. begin
  197. Result:=True;
  198. If Assigned(CurrentTM.DoneManager) then
  199. Result:=CurrentTM.DoneManager();
  200. If Result then
  201. begin
  202. CurrentTM:=NewTM;
  203. If Assigned(CurrentTM.InitManager) then
  204. Result:=CurrentTM.InitManager();
  205. end;
  206. end;
  207. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  208. begin
  209. result:=currenttm.BasicEventCreate(EventAttributes,AManualReset,InitialState, Name);
  210. end;
  211. procedure BasicEventDestroy(state:peventstate);
  212. begin
  213. currenttm.BasicEventDestroy(state);
  214. end;
  215. procedure BasicEventResetEvent(state:peventstate);
  216. begin
  217. currenttm.BasicEventResetEvent(state);
  218. end;
  219. procedure BasicEventSetEvent(state:peventstate);
  220. begin
  221. currenttm.BasicEventSetEvent(state);
  222. end;
  223. function BasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  224. begin
  225. result:=currenttm.BasicEventWaitFor(Timeout,state);
  226. end;
  227. function RTLEventCreate :PRTLEvent;
  228. begin
  229. result:=currenttm.RTLEventCreate();
  230. end;
  231. procedure RTLeventDestroy(state:pRTLEvent);
  232. begin
  233. currenttm.RTLEventDestroy(state);
  234. end;
  235. procedure RTLeventSetEvent(state:pRTLEvent);
  236. begin
  237. currenttm.RTLEventSetEvent(state);
  238. end;
  239. procedure RTLeventResetEvent(state:pRTLEvent);
  240. begin
  241. currenttm.RTLEventResetEvent(state);
  242. end;
  243. procedure RTLeventWaitFor(state:pRTLEvent);
  244. begin
  245. currenttm.RTLEventWaitFor(state);
  246. end;
  247. procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
  248. begin
  249. currenttm.RTLEventWaitForTimeout(state,timeout);
  250. end;
  251. { ---------------------------------------------------------------------
  252. ThreadManager which gives run-time error. Use if no thread support.
  253. ---------------------------------------------------------------------}
  254. {$ifndef DISABLE_NO_THREAD_MANAGER}
  255. { resourcestrings are not supported by the system unit,
  256. they are in the objpas unit and not available for fpc/tp modes }
  257. const
  258. SNoThreads = 'This binary has no thread support compiled in.';
  259. SRecompileWithThreads = 'Recompile the application with a thread-driver in the program uses clause before other units using thread.';
  260. Procedure NoThreadError;
  261. begin
  262. {$ifndef EMBEDDED}
  263. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  264. If IsConsole then
  265. begin
  266. Writeln(StdErr,SNoThreads);
  267. Writeln(StdErr,SRecompileWithThreads);
  268. end;
  269. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  270. {$endif EMBEDDED}
  271. RunError(232)
  272. end;
  273. function NoBeginThread(sa : Pointer;stacksize : PtrUInt;
  274. ThreadFunction : tthreadfunc;p : pointer;
  275. creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  276. begin
  277. NoThreadError;
  278. result:=tthreadid(-1);
  279. end;
  280. procedure NoEndThread(ExitCode : DWord);
  281. begin
  282. NoThreadError;
  283. end;
  284. function NoThreadHandler (threadHandle : TThreadID) : dword;
  285. begin
  286. NoThreadError;
  287. result:=dword(-1);
  288. end;
  289. function NoWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  290. begin
  291. NoThreadError;
  292. result:=dword(-1);
  293. end;
  294. function NoThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  295. begin
  296. NoThreadError;
  297. result:=false;
  298. end;
  299. function NoThreadGetPriority (threadHandle : TThreadID): longint;
  300. begin
  301. NoThreadError;
  302. result:=-1;
  303. end;
  304. function NoGetCurrentThreadId : TThreadID;
  305. begin
  306. if IsMultiThread then
  307. NoThreadError
  308. else
  309. ThreadingAlreadyUsed:=true;
  310. result:=TThreadID(1);
  311. end;
  312. procedure NoCriticalSection(var CS);
  313. begin
  314. if IsMultiThread then
  315. NoThreadError
  316. else
  317. ThreadingAlreadyUsed:=true;
  318. end;
  319. function NoTryEnterCriticalSection(var CS):longint;
  320. begin
  321. if IsMultiThread then
  322. NoThreadError
  323. else
  324. ThreadingAlreadyUsed:=true;
  325. Result:=-1;
  326. end;
  327. procedure NoInitThreadvar(var offset : {$ifdef cpu16}word{$else}dword{$endif};size : dword);
  328. begin
  329. NoThreadError;
  330. end;
  331. function NoRelocateThreadvar(offset : {$ifdef cpu16}word{$else}dword{$endif}) : pointer;
  332. begin
  333. NoThreadError;
  334. result:=nil;
  335. end;
  336. function NoBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  337. begin
  338. if IsMultiThread then
  339. NoThreadError
  340. else
  341. ThreadingAlreadyUsed:=true;
  342. result:=nil;
  343. end;
  344. procedure NoBasicEvent(state:peventstate);
  345. begin
  346. if IsMultiThread then
  347. NoThreadError
  348. else
  349. ThreadingAlreadyUsed:=true;
  350. end;
  351. function NoBasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  352. begin
  353. if IsMultiThread then
  354. NoThreadError
  355. else
  356. ThreadingAlreadyUsed:=true;
  357. result:=-1;
  358. end;
  359. function NoRTLEventCreate :PRTLEvent;
  360. begin
  361. if IsMultiThread then
  362. NoThreadError
  363. else
  364. ThreadingAlreadyUsed:=true;
  365. result:=nil;
  366. end;
  367. procedure NoRTLEvent(state:pRTLEvent);
  368. begin
  369. if IsMultiThread then
  370. NoThreadError
  371. else
  372. ThreadingAlreadyUsed:=true
  373. end;
  374. procedure NoRTLEventWaitForTimeout(state:pRTLEvent;timeout : longint);
  375. begin
  376. if IsMultiThread then
  377. NoThreadError
  378. else
  379. ThreadingAlreadyUsed:=true;
  380. end;
  381. const
  382. NoThreadManager : TThreadManager = (
  383. InitManager : Nil;
  384. DoneManager : Nil;
  385. {$ifdef EMBEDDED}
  386. { while this is pretty hacky, it reduces the size of typical embedded programs
  387. and works fine on arm and avr }
  388. BeginThread : @NoBeginThread;
  389. EndThread : TEndThreadHandler(@NoThreadError);
  390. SuspendThread : TThreadHandler(@NoThreadError);
  391. ResumeThread : TThreadHandler(@NoThreadError);
  392. KillThread : TThreadHandler(@NoThreadError);
  393. CloseThread : TThreadHandler(@NoThreadError);
  394. ThreadSwitch : TThreadSwitchHandler(@NoThreadError);
  395. WaitForThreadTerminate : TWaitForThreadTerminateHandler(@NoThreadError);
  396. ThreadSetPriority : TThreadSetPriorityHandler(@NoThreadError);
  397. ThreadGetPriority : TThreadGetPriorityHandler(@NoThreadError);
  398. GetCurrentThreadId : @NoGetCurrentThreadId;
  399. InitCriticalSection : TCriticalSectionHandler(@NoThreadError);
  400. DoneCriticalSection : TCriticalSectionHandler(@NoThreadError);
  401. EnterCriticalSection : TCriticalSectionHandler(@NoThreadError);
  402. TryEnterCriticalSection: TCriticalSectionHandlerTryEnter(@NoThreadError);
  403. LeaveCriticalSection : TCriticalSectionHandler(@NoThreadError);
  404. InitThreadVar : TInitThreadVarHandler(@NoThreadError);
  405. RelocateThreadVar : TRelocateThreadVarHandler(@NoThreadError);
  406. AllocateThreadVars : @NoThreadError;
  407. ReleaseThreadVars : @NoThreadError;
  408. BasicEventCreate : TBasicEventCreateHandler(@NoThreadError);
  409. BasicEventdestroy : TBasicEventHandler(@NoThreadError);
  410. BasicEventResetEvent : TBasicEventHandler(@NoThreadError);
  411. BasicEventSetEvent : TBasicEventHandler(@NoThreadError);
  412. BasicEventWaitFor : TBasicEventWaitForHandler(@NoThreadError);
  413. RTLEventCreate : TRTLCreateEventHandler(@NoThreadError);
  414. RTLEventdestroy : TRTLEventHandler(@NoThreadError);
  415. RTLEventSetEvent : TRTLEventHandler(@NoThreadError);
  416. RTLEventResetEvent : TRTLEventHandler(@NoThreadError);
  417. RTLEventWaitFor : TRTLEventHandler(@NoThreadError);
  418. RTLEventwaitfortimeout : TRTLEventHandlerTimeout(@NoThreadError);
  419. {$else EMBEDDED}
  420. BeginThread : @NoBeginThread;
  421. EndThread : @NoEndThread;
  422. SuspendThread : @NoThreadHandler;
  423. ResumeThread : @NoThreadHandler;
  424. KillThread : @NoThreadHandler;
  425. CloseThread : @NoThreadHandler;
  426. ThreadSwitch : @NoThreadError;
  427. WaitForThreadTerminate : @NoWaitForThreadTerminate;
  428. ThreadSetPriority : @NoThreadSetPriority;
  429. ThreadGetPriority : @NoThreadGetPriority;
  430. GetCurrentThreadId : @NoGetCurrentThreadId;
  431. InitCriticalSection : @NoCriticalSection;
  432. DoneCriticalSection : @NoCriticalSection;
  433. EnterCriticalSection : @NoCriticalSection;
  434. TryEnterCriticalSection: @NoTryEnterCriticalSection;
  435. LeaveCriticalSection : @NoCriticalSection;
  436. InitThreadVar : @NoInitThreadVar;
  437. RelocateThreadVar : @NoRelocateThreadVar;
  438. AllocateThreadVars : @NoThreadError;
  439. ReleaseThreadVars : @NoThreadError;
  440. BasicEventCreate : @NoBasicEventCreate;
  441. BasicEventDestroy : @NoBasicEvent;
  442. BasicEventResetEvent : @NoBasicEvent;
  443. BasicEventSetEvent : @NoBasicEvent;
  444. BasicEventWaitFor : @NoBasiceventWaitFor;
  445. RTLEventCreate : @NoRTLEventCreate;
  446. RTLEventDestroy : @NoRTLevent;
  447. RTLEventSetEvent : @NoRTLevent;
  448. RTLEventResetEvent : @NoRTLEvent;
  449. RTLEventWaitFor : @NoRTLEvent;
  450. RTLEventWaitforTimeout : @NoRTLEventWaitForTimeout;
  451. {$endif EMBEDDED}
  452. );
  453. Procedure SetNoThreadManager;
  454. begin
  455. SetThreadManager(NoThreadManager);
  456. end;
  457. Procedure InitSystemThreads; public name '_FPC_InitSystemThreads';
  458. begin
  459. { This should be changed to a real value during
  460. thread driver initialization if appropriate. }
  461. ThreadID := TThreadID(1);
  462. SetNoThreadManager;
  463. end;
  464. {$endif DISABLE_NO_THREAD_MANAGER}