systhrd.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Peter Vreman,
  4. member of the Free Pascal development team.
  5. Linux (pthreads) threading support implementation
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*****************************************************************************
  13. Local Api imports
  14. *****************************************************************************}
  15. const
  16. pag_Read = 1;
  17. pag_Write = 2;
  18. pag_Execute = 4;
  19. pag_Guard = 8;
  20. pag_Commit = $10;
  21. obj_Tile = $40;
  22. sem_Indefinite_Wait = cardinal (-1);
  23. dtSuspended = 1;
  24. dtStack_Commited = 2;
  25. { import the necessary stuff from the OS }
  26. function DosAllocThreadLocalMemory (Count: cardinal; var P: pointer): cardinal;
  27. cdecl; external 'DOSCALLS' index 454;
  28. function DosFreeThreadLocalMemory (P: pointer): cardinal; cdecl;
  29. external 'DOSCALLS' index 455;
  30. function DosCreateThread (var TID: cardinal; Address: pointer;
  31. (* TThreadFunc *)
  32. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  33. external 'DOSCALLS' index 311;
  34. procedure DosExit (Action, Result: cardinal); cdecl;
  35. external 'DOSCALLS' index 234;
  36. function DosCreateMutExSem (Name: PChar; var Handle: longint; Attr: cardinal;
  37. State: boolean): cardinal; cdecl; external 'DOSCALLS' index 331;
  38. function DosCloseMutExSem (Handle: longint): cardinal; cdecl;
  39. external 'DOSCALLS' index 333;
  40. function DosQueryMutExSem (Handle: longint; var PID, TID, Count: cardinal):
  41. cardinal; cdecl; external 'DOSCALLS' index 336;
  42. function DosRequestMutExSem (Handle:longint; Timeout: cardinal): cardinal; cdecl;
  43. external 'DOSCALLS' index 334;
  44. function DosReleaseMutExSem (Handle: longint): cardinal; cdecl;
  45. external 'DOSCALLS' index 335;
  46. function DosAllocMem (var P: pointer; Size, Flag: cardinal): cardinal; cdecl;
  47. external 'DOSCALLS' index 299;
  48. function DosFreeMem (P: pointer): cardinal; cdecl;
  49. external 'DOSCALLS' index 304;
  50. function DosEnterCritSec:cardinal; cdecl; external 'DOSCALLS' index 232;
  51. function DosExitCritSec:cardinal; cdecl; external 'DOSCALLS' index 233;
  52. {*****************************************************************************
  53. Threadvar support
  54. *****************************************************************************}
  55. const
  56. ThreadVarBlockSize: dword = 0;
  57. var
  58. (* Pointer to an allocated dword space within the local thread *)
  59. (* memory area. Pointer to the real memory block allocated for *)
  60. (* thread vars in this block is then stored in this dword. *)
  61. DataIndex: PPointer;
  62. procedure SysInitThreadvar (var Offset: dword; Size: dword);
  63. begin
  64. Offset := ThreadVarBlockSize;
  65. Inc (ThreadVarBlockSize, Size);
  66. end;
  67. function SysRelocateThreadVar (Offset: dword): pointer;
  68. begin
  69. SysRelocateThreadVar := DataIndex^ + Offset;
  70. end;
  71. procedure SysAllocateThreadVars;
  72. begin
  73. { we've to allocate the memory from the OS }
  74. { because the FPC heap management uses }
  75. { exceptions which use threadvars but }
  76. { these aren't allocated yet ... }
  77. { allocate room on the heap for the thread vars }
  78. if DosAllocMem (DataIndex^, ThreadVarBlockSize, pag_Read or pag_Write
  79. or pag_Commit) <> 0 then HandleError (8);
  80. end;
  81. procedure SysReleaseThreadVars;
  82. begin
  83. { release thread vars }
  84. DosFreeMem (DataIndex^);
  85. end;
  86. procedure InitThreadVars;
  87. begin
  88. { allocate one ThreadVar entry from the OS, we use this entry }
  89. { for a pointer to our threadvars }
  90. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then HandleError (8);
  91. { initialize threadvars }
  92. init_all_unit_threadvars;
  93. { allocate mem for main thread threadvars }
  94. SysAllocateThreadVars;
  95. { copy main thread threadvars }
  96. copy_all_unit_threadvars;
  97. { install threadvar handler }
  98. fpc_threadvar_relocate_proc := @SysRelocateThreadvar;
  99. end;
  100. {*****************************************************************************
  101. Delphi/Win32 compatibility
  102. *****************************************************************************}
  103. { we implement these procedures for win32 by importing them }
  104. { directly from windows }
  105. procedure SysInitCriticalSection(var cs : TRTLCriticalSection);
  106. external 'kernel32' name 'InitializeCriticalSection';
  107. procedure SysDoneCriticalSection(var cs : TRTLCriticalSection);
  108. external 'kernel32' name 'DeleteCriticalSection';
  109. procedure SysEnterCriticalSection(var cs : TRTLCriticalSection);
  110. external 'kernel32' name 'EnterCriticalSection';
  111. procedure SysLeaveCriticalSection(var cs : TRTLCriticalSection);
  112. external 'kernel32' name 'LeaveCriticalSection';