thread.inc 16 KB

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