threadvr.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt
  4. member of the Free Pascal development team
  5. Threadvar support, platform independent part
  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. Threadvar support
  14. *****************************************************************************}
  15. {$ifndef FPC_SECTION_THREADVARS}
  16. type
  17. pltvInitEntry = ^ltvInitEntry;
  18. ppltvInitEntry = ^pltvInitEntry;
  19. ltvInitEntry = packed record
  20. varaddr : {$ifdef cpu16}pword{$else}pdword{$endif};
  21. size : longint;
  22. end;
  23. TltvInitTablesTable = packed record
  24. count : dword;
  25. tables : packed array [1..{$ifdef cpu16}16{$else}32767{$endif}] of {$ifdef ver3_0}pltvInitEntry{$else}ppltvInitEntry{$endif};
  26. end;
  27. PltvInitTablesTable = ^TltvInitTablesTable;
  28. {$ifndef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  29. var
  30. ThreadvarTablesTable : TltvInitTablesTable; external name 'FPC_THREADVARTABLES';
  31. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  32. procedure init_unit_threadvars (tableEntry : pltvInitEntry);
  33. begin
  34. while tableEntry^.varaddr <> nil do
  35. begin
  36. CurrentTM.InitThreadvar (tableEntry^.varaddr^, tableEntry^.size);
  37. inc (pchar (tableEntry), sizeof (tableEntry^));
  38. end;
  39. end;
  40. procedure init_all_unit_threadvars;
  41. var
  42. i : longint;
  43. begin
  44. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  45. with PltvInitTablesTable(EntryInformation.ThreadvarTablesTable)^ do
  46. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  47. with ThreadvarTablesTable do
  48. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  49. begin
  50. {$ifdef DEBUG_MT}
  51. WriteLn ('init_all_unit_threadvars (',count,') units');
  52. {$endif}
  53. for i := 1 to count do
  54. init_unit_threadvars (tables[i]{$ifndef ver3_0}^{$endif});
  55. end;
  56. end;
  57. procedure copy_unit_threadvars (tableEntry : pltvInitEntry);
  58. var
  59. oldp,
  60. newp : pointer;
  61. begin
  62. while tableEntry^.varaddr <> nil do
  63. begin
  64. newp:=CurrentTM.RelocateThreadVar(tableEntry^.varaddr^);
  65. oldp:=pointer(pchar(tableEntry^.varaddr)+sizeof(pointer));
  66. move(oldp^,newp^,tableEntry^.size);
  67. inc (pchar (tableEntry), sizeof (tableEntry^));
  68. end;
  69. end;
  70. procedure copy_all_unit_threadvars;
  71. var
  72. i: longint;
  73. begin
  74. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  75. with PltvInitTablesTable(EntryInformation.ThreadvarTablesTable)^ do
  76. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  77. with ThreadvarTablesTable do
  78. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  79. begin
  80. {$ifdef DEBUG_MT}
  81. WriteLn ('copy_all_unit_threadvars (',count,') units');
  82. {$endif}
  83. for i := 1 to count do
  84. copy_unit_threadvars (tables[i]{$ifndef ver3_0}^{$endif});
  85. end;
  86. end;
  87. {$endif FPC_SECTION_THREADVARS}
  88. procedure InitThreadVars(RelocProc : TRelocateThreadVarHandler);
  89. begin
  90. {$ifndef FPC_SECTION_THREADVARS}
  91. { initialize threadvars }
  92. init_all_unit_threadvars;
  93. { allocate mem for main thread threadvars }
  94. CurrentTM.AllocateThreadVars;
  95. { copy main thread threadvars }
  96. copy_all_unit_threadvars;
  97. { install threadvar handler }
  98. fpc_threadvar_relocate_proc:=RelocProc;
  99. {$endif FPC_SECTION_THREADVARS}
  100. {$ifdef FPC_HAS_FEATURE_HEAP}
  101. {$ifndef HAS_MEMORYMANAGER}
  102. {$ifndef FPC_NO_DEFAULT_HEAP}
  103. RelocateHeap;
  104. {$endif ndef FPC_NO_DEFAULT_HEAP}
  105. {$endif HAS_MEMORYMANAGER}
  106. {$endif}
  107. end;