classes.inc 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {**********************************************************************
  11. * Class implementations are in separate files. *
  12. **********************************************************************}
  13. type
  14. {$ifdef CPU16}
  15. TFilerFlagsInt = Byte;
  16. {$else CPU16}
  17. TFilerFlagsInt = LongInt;
  18. {$endif CPU16}
  19. var
  20. ClassList : TThreadlist;
  21. ClassAliasList : TStringList;
  22. {
  23. Include all message strings
  24. Add a language with IFDEF LANG_NAME
  25. just befor the final ELSE. This way English will always be the default.
  26. }
  27. {$IFDEF LANG_GERMAN}
  28. {$i constsg.inc}
  29. {$ELSE}
  30. {$IFDEF LANG_SPANISH}
  31. {$i constss.inc}
  32. {$ENDIF}
  33. {$ENDIF}
  34. { Utility routines }
  35. {$i util.inc}
  36. { TBits implementation }
  37. {$i bits.inc}
  38. { All streams implementations: }
  39. { Tstreams THandleStream TFileStream TResourcseStreams TStringStream }
  40. { TCustomMemoryStream TMemoryStream }
  41. {$i streams.inc}
  42. { TParser implementation}
  43. {$i parser.inc}
  44. { TCollection and TCollectionItem implementations }
  45. {$i collect.inc}
  46. { TList and TThreadList implementations }
  47. {$i lists.inc}
  48. { TStrings and TStringList implementations }
  49. {$i stringl.inc}
  50. { TThread implementation }
  51. { system independend threading code }
  52. var
  53. { event executed by SychronizeInternal to wake main thread if it sleeps in
  54. CheckSynchronize }
  55. SynchronizeTimeoutEvent: PRtlEvent;
  56. { the head of the queue containing the entries to be Synchronized - Nil if the
  57. queue is empty }
  58. ThreadQueueHead: TThread.PThreadQueueEntry;
  59. { the tail of the queue containing the entries to be Synchronized - Nil if the
  60. queue is empty }
  61. ThreadQueueTail: TThread.PThreadQueueEntry;
  62. { used for serialized access to the queue }
  63. ThreadQueueLock: TRtlCriticalSection;
  64. { usage counter for ThreadQueueLock }
  65. ThreadQueueLockCounter : longint;
  66. { this list holds all instances of external threads that need to be freed at
  67. the end of the program }
  68. ExternalThreads: TThreadList;
  69. { this list signals that the ExternalThreads list is cleared and thus the
  70. thread instances don't need to remove themselves }
  71. ExternalThreadsCleanup: Boolean = False;
  72. { this must be a global var, otherwise unwanted optimizations might happen in
  73. TThread.SpinWait() }
  74. SpinWaitDummy: LongWord;
  75. {$ifdef FPC_HAS_FEATURE_THREADING}
  76. threadvar
  77. {$else}
  78. var
  79. {$endif}
  80. { the instance of the current thread; in case of an external thread this is
  81. Nil until TThread.GetCurrentThread was called once (the RTLs need to ensure
  82. that threadvars are initialized with 0!) }
  83. CurrentThreadVar: TThread;
  84. type
  85. { this type is used if a thread is created using
  86. TThread.CreateAnonymousThread }
  87. TAnonymousThread = class(TThread)
  88. private
  89. fProc: TProcedure;
  90. protected
  91. procedure Execute; override;
  92. public
  93. { as in TThread aProc needs to be changed to TProc once closures are
  94. supported }
  95. constructor Create(aProc: TProcedure);
  96. end;
  97. procedure TAnonymousThread.Execute;
  98. begin
  99. fProc();
  100. end;
  101. constructor TAnonymousThread.Create(aProc: TProcedure);
  102. begin
  103. { an anonymous thread is created suspended and with FreeOnTerminate set }
  104. inherited Create(True);
  105. FreeOnTerminate := True;
  106. fProc := aProc;
  107. end;
  108. type
  109. { this type is used by TThread.GetCurrentThread if the thread does not yet
  110. have a value in CurrentThreadVar (Note: the main thread is also created as
  111. a TExternalThread) }
  112. TExternalThread = class(TThread)
  113. protected
  114. { dummy method to remove the warning }
  115. procedure Execute; override;
  116. public
  117. constructor Create;
  118. destructor Destroy; override;
  119. end;
  120. procedure TExternalThread.Execute;
  121. begin
  122. { empty }
  123. end;
  124. constructor TExternalThread.Create;
  125. begin
  126. FExternalThread := True;
  127. { the parameter is unimportant if FExternalThread is True }
  128. inherited Create(False);
  129. with ExternalThreads.LockList do
  130. try
  131. Add(Self);
  132. finally
  133. ExternalThreads.UnlockList;
  134. end;
  135. end;
  136. destructor TExternalThread.Destroy;
  137. begin
  138. inherited;
  139. if not ExternalThreadsCleanup then
  140. with ExternalThreads.LockList do
  141. try
  142. Extract(Self);
  143. finally
  144. ExternalThreads.UnlockList;
  145. end;
  146. end;
  147. function ThreadProc(ThreadObjPtr: Pointer): PtrInt;
  148. var
  149. FreeThread: Boolean;
  150. Thread: TThread absolute ThreadObjPtr;
  151. begin
  152. { if Suspend checks FSuspended before doing anything, make sure it }
  153. { knows we're currently not suspended (this flag may have been set }
  154. { to true if CreateSuspended was true) }
  155. // Thread.FSuspended:=false;
  156. // wait until AfterConstruction has been called, so we cannot
  157. // free ourselves before TThread.Create has finished
  158. // (since that one may check our VTM in case of $R+, and
  159. // will call the AfterConstruction method in all cases)
  160. // Thread.Suspend;
  161. try
  162. { The thread may be already terminated at this point, e.g. if it was intially
  163. suspended, or if it wasn't ever scheduled for execution for whatever reason.
  164. So bypass user code if terminated. }
  165. if not Thread.Terminated then begin
  166. CurrentThreadVar := Thread;
  167. Thread.Execute;
  168. end;
  169. except
  170. Thread.FFatalException := TObject(AcquireExceptionObject);
  171. end;
  172. FreeThread := Thread.FFreeOnTerminate;
  173. Result := Thread.FReturnValue;
  174. Thread.FFinished := True;
  175. Thread.DoTerminate;
  176. if FreeThread then
  177. Thread.Free;
  178. {$ifdef FPC_HAS_FEATURE_THREADING}
  179. EndThread(Result);
  180. {$endif}
  181. end;
  182. { system-dependent code }
  183. {$i tthread.inc}
  184. constructor TThread.Create(CreateSuspended: Boolean;
  185. const StackSize: SizeUInt);
  186. begin
  187. inherited Create;
  188. {$ifdef FPC_HAS_FEATURE_THREADING}
  189. InterlockedIncrement(ThreadQueueLockCounter);
  190. {$endif}
  191. if FExternalThread then
  192. {$ifdef FPC_HAS_FEATURE_THREADING}
  193. FThreadID := GetCurrentThreadID
  194. {$else}
  195. FThreadID := 0{GetCurrentThreadID}
  196. {$endif}
  197. else
  198. SysCreate(CreateSuspended, StackSize);
  199. end;
  200. destructor TThread.Destroy;
  201. begin
  202. if not FExternalThread then begin
  203. SysDestroy;
  204. {$ifdef FPC_HAS_FEATURE_THREADING}
  205. if FHandle <> TThreadID(0) then
  206. CloseThread(FHandle);
  207. {$endif}
  208. end;
  209. RemoveQueuedEvents(Self);
  210. DoneSynchronizeEvent;
  211. {$ifdef FPC_HAS_FEATURE_THREADING}
  212. if InterlockedDecrement(ThreadQueueLockCounter)=0 then
  213. DoneCriticalSection(ThreadQueueLock);
  214. {$endif}
  215. { set CurrentThreadVar to Nil? }
  216. inherited Destroy;
  217. end;
  218. procedure TThread.Start;
  219. begin
  220. { suspend/resume are now deprecated in Delphi (they also don't work
  221. on most platforms in FPC), so a different method was required
  222. to start a thread if it's create with fSuspended=true -> that's
  223. what this method is for. }
  224. Resume;
  225. end;
  226. function TThread.GetSuspended: Boolean;
  227. begin
  228. GetSuspended:=FSuspended;
  229. end;
  230. procedure TThread.Terminate;
  231. begin
  232. FTerminated := True;
  233. TerminatedSet;
  234. end;
  235. Procedure TThread.TerminatedSet;
  236. begin
  237. // Empty, must be overridden.
  238. end;
  239. procedure TThread.AfterConstruction;
  240. begin
  241. inherited AfterConstruction;
  242. // enable for all platforms once http://bugs.freepascal.org/view.php?id=16884
  243. // is fixed for all platforms (in case the fix for non-unix platforms also
  244. // requires this field at least)
  245. {$if defined(unix) or defined(windows) or defined(os2) or defined(hasamiga)}
  246. if not FExternalThread and not FInitialSuspended then
  247. Resume;
  248. {$endif}
  249. end;
  250. procedure ExecuteThreadQueueEntry(aEntry: TThread.PThreadQueueEntry);
  251. begin
  252. if Assigned(aEntry^.Method) then
  253. aEntry^.Method()
  254. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  255. else
  256. if Assigned(aEntry^.ThreadProc) then
  257. aEntry^.ThreadProc
  258. {$endif}
  259. end;
  260. procedure ThreadQueueAppend(aEntry: TThread.PThreadQueueEntry; aQueueIfMain: Boolean);
  261. var
  262. thd: TThread;
  263. issync: Boolean;
  264. begin
  265. { do we really need a synchronized call? }
  266. {$ifdef FPC_HAS_FEATURE_THREADING}
  267. if (GetCurrentThreadID = MainThreadID) and (not aQueueIfMain or not IsMultiThread) then
  268. {$endif}
  269. begin
  270. try
  271. ExecuteThreadQueueEntry(aEntry);
  272. finally
  273. if not Assigned(aEntry^.SyncEvent) then
  274. Dispose(aEntry);
  275. end;
  276. {$ifdef FPC_HAS_FEATURE_THREADING}
  277. end else begin
  278. { store thread and whether we're dealing with a synchronized event; the
  279. event record itself might already be freed after the ThreadQueueLock is
  280. released (in case of a Queue() call; for a Synchronize() call the record
  281. will stay valid, thus accessing SyncEvent later on (if issync is true) is
  282. okay) }
  283. thd := aEntry^.Thread;
  284. issync := Assigned(aEntry^.SyncEvent);
  285. System.EnterCriticalSection(ThreadQueueLock);
  286. try
  287. { add the entry to the thread queue }
  288. if Assigned(ThreadQueueTail) then begin
  289. ThreadQueueTail^.Next := aEntry;
  290. end else
  291. ThreadQueueHead := aEntry;
  292. ThreadQueueTail := aEntry;
  293. finally
  294. System.LeaveCriticalSection(ThreadQueueLock);
  295. end;
  296. { ensure that the main thread knows that something awaits }
  297. RtlEventSetEvent(SynchronizeTimeoutEvent);
  298. if assigned(WakeMainThread) then
  299. WakeMainThread(thd);
  300. { is this a Synchronize or Queue entry? }
  301. if issync then begin
  302. RtlEventWaitFor(aEntry^.SyncEvent);
  303. if Assigned(aEntry^.Exception) then
  304. raise aEntry^.Exception;
  305. end;
  306. {$endif def FPC_HAS_FEATURE_THREADING}
  307. end;
  308. end;
  309. procedure TThread.InitSynchronizeEvent;
  310. begin
  311. if Assigned(FSynchronizeEntry) then
  312. Exit;
  313. New(FSynchronizeEntry);
  314. FillChar(FSynchronizeEntry^, SizeOf(TThreadQueueEntry), 0);
  315. FSynchronizeEntry^.Thread := Self;
  316. FSynchronizeEntry^.ThreadID := ThreadID;
  317. {$ifdef FPC_HAS_FEATURE_THREADING}
  318. FSynchronizeEntry^.SyncEvent := RtlEventCreate;
  319. {$else}
  320. FSynchronizeEntry^.SyncEvent := nil{RtlEventCreate};
  321. {$endif}
  322. end;
  323. procedure TThread.DoneSynchronizeEvent;
  324. begin
  325. if not Assigned(FSynchronizeEntry) then
  326. Exit;
  327. {$ifdef FPC_HAS_FEATURE_THREADING}
  328. RtlEventDestroy(FSynchronizeEntry^.SyncEvent);
  329. {$endif}
  330. Dispose(FSynchronizeEntry);
  331. FSynchronizeEntry := Nil;
  332. end;
  333. class procedure TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod);
  334. var
  335. syncentry: PThreadQueueEntry;
  336. thread: TThread;
  337. begin
  338. {$ifdef FPC_HAS_FEATURE_THREADING}
  339. if Assigned(AThread) and (AThread.ThreadID = GetCurrentThreadID) then
  340. {$else}
  341. if Assigned(AThread) then
  342. {$endif}
  343. thread := AThread
  344. else if Assigned(CurrentThreadVar) then
  345. thread := CurrentThreadVar
  346. else begin
  347. thread := Nil;
  348. { use a local synchronize event }
  349. New(syncentry);
  350. FillChar(syncentry^, SizeOf(TThreadQueueEntry), 0);
  351. {$ifdef FPC_HAS_FEATURE_THREADING}
  352. syncentry^.ThreadID := GetCurrentThreadID;
  353. syncentry^.SyncEvent := RtlEventCreate;
  354. {$else}
  355. syncentry^.ThreadID := 0{GetCurrentThreadID};
  356. syncentry^.SyncEvent := nil{RtlEventCreate};
  357. {$endif}
  358. end;
  359. if Assigned(thread) then begin
  360. { the Synchronize event is instantiated on demand }
  361. thread.InitSynchronizeEvent;
  362. syncentry := thread.FSynchronizeEntry;
  363. end;
  364. syncentry^.Exception := Nil;
  365. syncentry^.Method := AMethod;
  366. try
  367. ThreadQueueAppend(syncentry, False);
  368. finally
  369. syncentry^.Method := Nil;
  370. syncentry^.Next := Nil;
  371. if not Assigned(thread) then begin
  372. { clean up again }
  373. {$ifdef FPC_HAS_FEATURE_THREADING}
  374. RtlEventDestroy(syncentry^.SyncEvent);
  375. {$endif}
  376. Dispose(syncentry);
  377. end;
  378. end;
  379. end;
  380. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  381. class procedure TThread.Synchronize(AThread: TThread; AProcedure: TThreadProcedure);
  382. var
  383. syncentry: PThreadQueueEntry;
  384. thread: TThread;
  385. begin
  386. {$ifdef FPC_HAS_FEATURE_THREADING}
  387. if Assigned(AThread) and (AThread.ThreadID = GetCurrentThreadID) then
  388. {$else}
  389. if Assigned(AThread) then
  390. {$endif}
  391. thread := AThread
  392. else if Assigned(CurrentThreadVar) then
  393. thread := CurrentThreadVar
  394. else begin
  395. thread := Nil;
  396. { use a local synchronize event }
  397. New(syncentry);
  398. FillChar(syncentry^, SizeOf(TThreadQueueEntry), 0);
  399. {$ifdef FPC_HAS_FEATURE_THREADING}
  400. syncentry^.ThreadID := GetCurrentThreadID;
  401. syncentry^.SyncEvent := RtlEventCreate;
  402. {$else}
  403. syncentry^.ThreadID := 0{GetCurrentThreadID};
  404. syncentry^.SyncEvent := nil{RtlEventCreate};
  405. {$endif}
  406. end;
  407. if Assigned(thread) then begin
  408. { the Synchronize event is instantiated on demand }
  409. thread.InitSynchronizeEvent;
  410. syncentry := thread.FSynchronizeEntry;
  411. end;
  412. syncentry^.Exception := Nil;
  413. syncentry^.ThreadProc := AProcedure;
  414. try
  415. ThreadQueueAppend(syncentry, False);
  416. finally
  417. syncentry^.ThreadProc := Nil;
  418. syncentry^.Next := Nil;
  419. if not Assigned(thread) then begin
  420. { clean up again }
  421. {$ifdef FPC_HAS_FEATURE_THREADING}
  422. RtlEventDestroy(syncentry^.SyncEvent);
  423. {$endif}
  424. Dispose(syncentry);
  425. end;
  426. end;
  427. end;
  428. {$endif}
  429. procedure TThread.Synchronize(AMethod: TThreadMethod);
  430. begin
  431. TThread.Synchronize(self,AMethod);
  432. end;
  433. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  434. procedure TThread.Synchronize(AProcedure: TThreadProcedure);
  435. begin
  436. TThread.Synchronize(self,AProcedure);
  437. end;
  438. {$endif}
  439. Function PopThreadQueueHead : TThread.PThreadQueueEntry;
  440. begin
  441. Result:=ThreadQueueHead;
  442. if (Result<>Nil) then
  443. begin
  444. {$ifdef FPC_HAS_FEATURE_THREADING}
  445. System.EnterCriticalSection(ThreadQueueLock);
  446. try
  447. {$endif}
  448. Result:=ThreadQueueHead;
  449. if Result<>Nil then
  450. ThreadQueueHead:=ThreadQueueHead^.Next;
  451. if Not Assigned(ThreadQueueHead) then
  452. ThreadQueueTail := Nil;
  453. {$ifdef FPC_HAS_FEATURE_THREADING}
  454. finally
  455. System.LeaveCriticalSection(ThreadQueueLock);
  456. end;
  457. {$endif}
  458. end;
  459. end;
  460. function CheckSynchronize(timeout : longint=0) : boolean;
  461. { assumes being called from GUI thread }
  462. var
  463. ExceptObj: TObject;
  464. tmpentry: TThread.PThreadQueueEntry;
  465. begin
  466. result:=false;
  467. { first sanity check }
  468. if Not IsMultiThread then
  469. Exit
  470. {$ifdef FPC_HAS_FEATURE_THREADING}
  471. { second sanity check }
  472. else if GetCurrentThreadID<>MainThreadID then
  473. raise EThread.CreateFmt(SCheckSynchronizeError,[GetCurrentThreadID]);
  474. if timeout>0 then
  475. RtlEventWaitFor(SynchronizeTimeoutEvent,timeout)
  476. else
  477. RtlEventResetEvent(SynchronizeTimeoutEvent);
  478. tmpentry := PopThreadQueueHead;
  479. while Assigned(tmpentry) do
  480. begin
  481. { step 2: execute the method }
  482. exceptobj := Nil;
  483. try
  484. ExecuteThreadQueueEntry(tmpentry);
  485. except
  486. exceptobj := TObject(AcquireExceptionObject);
  487. end;
  488. { step 3: error handling and cleanup }
  489. if Assigned(tmpentry^.SyncEvent) then
  490. begin
  491. { for Synchronize entries we pass back the Exception and trigger
  492. the event that Synchronize waits in }
  493. tmpentry^.Exception := exceptobj;
  494. RtlEventSetEvent(tmpentry^.SyncEvent)
  495. end
  496. else
  497. begin
  498. { for Queue entries we dispose the entry and raise the exception }
  499. Dispose(tmpentry);
  500. if Assigned(exceptobj) then
  501. raise exceptobj;
  502. end;
  503. tmpentry := PopThreadQueueHead;
  504. end
  505. {$endif};
  506. end;
  507. class function TThread.GetCurrentThread: TThread;
  508. begin
  509. { if this is the first time GetCurrentThread is called for an external thread
  510. we need to create a corresponding TExternalThread instance }
  511. Result := CurrentThreadVar;
  512. if not Assigned(Result) then begin
  513. Result := TExternalThread.Create;
  514. CurrentThreadVar := Result;
  515. end;
  516. end;
  517. class function TThread.GetIsSingleProcessor: Boolean;
  518. begin
  519. Result := FProcessorCount <= 1;
  520. end;
  521. procedure TThread.Queue(aMethod: TThreadMethod);
  522. begin
  523. Queue(Self, aMethod);
  524. end;
  525. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  526. procedure TThread.Queue(aProcedure: TThreadProcedure);
  527. begin
  528. Queue(Self, aProcedure);
  529. end;
  530. {$endif}
  531. class procedure TThread.Queue(aThread: TThread; aMethod: TThreadMethod); static;
  532. begin
  533. InternalQueue(aThread, aMethod, False);
  534. end;
  535. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  536. class procedure TThread.Queue(aThread: TThread; aProcedure: TThreadProcedure); static;
  537. begin
  538. InternalQueue(aThread, aProcedure, False);
  539. end;
  540. {$endif}
  541. class procedure TThread.InternalQueue(aThread: TThread; aMethod: TThreadMethod; aQueueIfMain: Boolean); static;
  542. var
  543. queueentry: PThreadQueueEntry;
  544. begin
  545. New(queueentry);
  546. FillChar(queueentry^, SizeOf(TThreadQueueEntry), 0);
  547. queueentry^.Thread := aThread;
  548. {$ifdef FPC_HAS_FEATURE_THREADING}
  549. queueentry^.ThreadID := GetCurrentThreadID;
  550. {$else}
  551. queueentry^.ThreadID := 0{GetCurrentThreadID};
  552. {$endif}
  553. queueentry^.Method := aMethod;
  554. { the queueentry is freed by CheckSynchronize (or by RemoveQueuedEvents) }
  555. ThreadQueueAppend(queueentry, aQueueIfMain);
  556. end;
  557. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  558. class procedure TThread.InternalQueue(aThread: TThread; aProcedure: TThreadProcedure; aQueueIfMain: Boolean); static;
  559. var
  560. queueentry: PThreadQueueEntry;
  561. begin
  562. New(queueentry);
  563. FillChar(queueentry^, SizeOf(TThreadQueueEntry), 0);
  564. queueentry^.Thread := aThread;
  565. {$ifdef FPC_HAS_FEATURE_THREADING}
  566. queueentry^.ThreadID := GetCurrentThreadID;
  567. {$else}
  568. queueentry^.ThreadID := 0{GetCurrentThreadID};
  569. {$endif}
  570. queueentry^.ThreadProc := aProcedure;
  571. { the queueentry is freed by CheckSynchronize (or by RemoveQueuedEvents) }
  572. ThreadQueueAppend(queueentry, aQueueIfMain);
  573. end;
  574. {$endif}
  575. procedure TThread.ForceQueue(aMethod: TThreadMethod);
  576. begin
  577. ForceQueue(Self, aMethod);
  578. end;
  579. class procedure TThread.ForceQueue(aThread: TThread; aMethod: TThreadMethod); static;
  580. begin
  581. InternalQueue(aThread, aMethod, True);
  582. end;
  583. class procedure TThread.RemoveQueuedEvents(aThread: TThread; aMethod: TThreadMethod);
  584. var
  585. entry, tmpentry, lastentry: PThreadQueueEntry;
  586. begin
  587. { anything to do at all? }
  588. if not Assigned(aThread) and not Assigned(aMethod) then
  589. Exit;
  590. {$ifdef FPC_HAS_FEATURE_THREADING}
  591. System.EnterCriticalSection(ThreadQueueLock);
  592. try
  593. {$endif}
  594. lastentry := Nil;
  595. entry := ThreadQueueHead;
  596. while Assigned(entry) do begin
  597. if
  598. { only entries not added by Synchronize }
  599. not Assigned(entry^.SyncEvent)
  600. { check for the thread }
  601. and (not Assigned(aThread) or (entry^.Thread = aThread) or (entry^.ThreadID = aThread.ThreadID))
  602. { check for the method }
  603. and (not Assigned(aMethod) or
  604. (
  605. (TMethod(entry^.Method).Code = TMethod(aMethod).Code) and
  606. (TMethod(entry^.Method).Data = TMethod(aMethod).Data)
  607. ))
  608. then begin
  609. { ok, we need to remove this entry }
  610. tmpentry := entry;
  611. if Assigned(lastentry) then
  612. lastentry^.Next := entry^.Next;
  613. entry := entry^.Next;
  614. if ThreadQueueHead = tmpentry then
  615. ThreadQueueHead := entry;
  616. if ThreadQueueTail = tmpentry then
  617. ThreadQueueTail := lastentry;
  618. { only dispose events added by Queue }
  619. if not Assigned(tmpentry^.SyncEvent) then
  620. Dispose(tmpentry);
  621. end else begin
  622. { leave this entry }
  623. lastentry := entry;
  624. entry := entry^.Next;
  625. end;
  626. end;
  627. {$ifdef FPC_HAS_FEATURE_THREADING}
  628. finally
  629. System.LeaveCriticalSection(ThreadQueueLock);
  630. end;
  631. {$endif}
  632. end;
  633. class procedure TThread.RemoveQueuedEvents(aMethod: TThreadMethod);
  634. begin
  635. RemoveQueuedEvents(Nil, aMethod);
  636. end;
  637. class procedure TThread.RemoveQueuedEvents(aThread: TThread);
  638. begin
  639. RemoveQueuedEvents(aThread, Nil);
  640. end;
  641. class function TThread.CheckTerminated: Boolean;
  642. begin
  643. { this method only works with threads created by TThread, so we can make a
  644. shortcut here }
  645. if not Assigned(CurrentThreadVar) then
  646. raise EThreadExternalException.Create(SThreadExternal);
  647. Result := CurrentThreadVar.FTerminated;
  648. end;
  649. class procedure TThread.SetReturnValue(aValue: Integer);
  650. begin
  651. { this method only works with threads created by TThread, so we can make a
  652. shortcut here }
  653. if not Assigned(CurrentThreadVar) then
  654. raise EThreadExternalException.Create(SThreadExternal);
  655. CurrentThreadVar.FReturnValue := aValue;
  656. end;
  657. class function TThread.CreateAnonymousThread(aProc: TProcedure): TThread;
  658. begin
  659. if not Assigned(aProc) then
  660. raise Exception.Create(SNoProcGiven);
  661. Result := TAnonymousThread.Create(aProc);
  662. end;
  663. class procedure TThread.NameThreadForDebugging(aThreadName: UnicodeString; aThreadID: TThreadID);
  664. begin
  665. {$ifdef FPC_HAS_FEATURE_THREADING}
  666. SetThreadDebugName(aThreadID, aThreadName);
  667. {$endif}
  668. end;
  669. class procedure TThread.NameThreadForDebugging(aThreadName: AnsiString; aThreadID: TThreadID);
  670. begin
  671. {$ifdef FPC_HAS_FEATURE_THREADING}
  672. SetThreadDebugName(aThreadID, aThreadName);
  673. {$endif}
  674. end;
  675. class procedure TThread.Yield;
  676. begin
  677. {$ifdef FPC_HAS_FEATURE_THREADING}
  678. ThreadSwitch;
  679. {$endif}
  680. end;
  681. class procedure TThread.Sleep(aMilliseconds: Cardinal);
  682. begin
  683. SysUtils.Sleep(aMilliseconds);
  684. end;
  685. class procedure TThread.SpinWait(aIterations: LongWord);
  686. var
  687. i: LongWord;
  688. begin
  689. { yes, it's just a simple busy wait to burn some cpu cycles... and as the job
  690. of this loop is to burn CPU cycles we switch off any optimizations that
  691. could interfere with this (e.g. loop unrolling) }
  692. { Do *NOT* do $PUSH, $OPTIMIZATIONS OFF, <code>, $POP because optimization is
  693. not a local switch, which means $PUSH/POP doesn't affect it, so that turns
  694. off *ALL* optimizations for code below this point. Thanks to this we shipped
  695. large parts of the classes unit with optimizations off between 2012-12-27
  696. and 2014-06-06.
  697. Instead, use a global var for the spinlock, because that is always handled
  698. as volatile, so the access won't be optimized away by the compiler. (KB) }
  699. for i:=1 to aIterations do
  700. begin
  701. Inc(SpinWaitDummy); // SpinWaitDummy *MUST* be global
  702. end;
  703. end;
  704. {$ifndef HAS_TTHREAD_GETSYSTEMTIMES}
  705. class procedure TThread.GetSystemTimes(out aSystemTimes: TSystemTimes);
  706. begin
  707. { by default we just return a zeroed out record }
  708. FillChar(aSystemTimes, SizeOf(aSystemTimes), 0);
  709. end;
  710. {$endif}
  711. class function TThread.GetTickCount: LongWord;
  712. begin
  713. Result := SysUtils.GetTickCount;
  714. end;
  715. class function TThread.GetTickCount64: QWord;
  716. begin
  717. Result := SysUtils.GetTickCount64;
  718. end;
  719. { TSimpleThread allows objects to create a threading method without defining
  720. a new thread class }
  721. Type
  722. TSimpleThread = class(TThread)
  723. private
  724. FExecuteMethod: TThreadExecuteHandler;
  725. protected
  726. procedure Execute; override;
  727. public
  728. constructor Create(ExecuteMethod: TThreadExecuteHandler; AOnterminate : TNotifyEvent);
  729. end;
  730. TSimpleStatusThread = class(TThread)
  731. private
  732. FExecuteMethod: TThreadExecuteStatusHandler;
  733. FStatus : String;
  734. FOnStatus : TThreadStatusNotifyEvent;
  735. protected
  736. procedure Execute; override;
  737. Procedure DoStatus;
  738. Procedure SetStatus(Const AStatus : String);
  739. public
  740. constructor Create(ExecuteMethod: TThreadExecuteStatusHandler; AOnStatus : TThreadStatusNotifyEvent; AOnterminate : TNotifyEvent);
  741. end;
  742. TSimpleProcThread = class(TThread)
  743. private
  744. FExecuteMethod: TThreadExecuteCallBack;
  745. FCallOnTerminate : TNotifyCallBack;
  746. FData : Pointer;
  747. protected
  748. Procedure TerminateCallBack(Sender : TObject);
  749. procedure Execute; override;
  750. public
  751. constructor Create(ExecuteMethod: TThreadExecuteCallBack; AData : Pointer; AOnterminate : TNotifyCallBack);
  752. end;
  753. TSimpleStatusProcThread = class(TThread)
  754. private
  755. FExecuteMethod: TThreadExecuteStatusCallBack;
  756. FCallOnTerminate : TNotifyCallBack;
  757. FStatus : String;
  758. FOnStatus : TThreadStatusNotifyCallBack;
  759. FData : Pointer;
  760. protected
  761. procedure Execute; override;
  762. Procedure DoStatus;
  763. Procedure SetStatus(Const AStatus : String);
  764. Procedure TerminateCallBack(Sender : TObject);
  765. public
  766. constructor Create(ExecuteMethod: TThreadExecuteStatusCallBack; AData : Pointer; AOnStatus : TThreadStatusNotifyCallBack; AOnterminate : TNotifyCallBack);
  767. end;
  768. { TSimpleThread }
  769. constructor TSimpleThread.Create(ExecuteMethod: TThreadExecuteHandler; AOnTerminate: TNotifyEvent);
  770. begin
  771. FExecuteMethod := ExecuteMethod;
  772. OnTerminate := AOnTerminate;
  773. inherited Create(False);
  774. end;
  775. procedure TSimpleThread.Execute;
  776. begin
  777. FreeOnTerminate := True;
  778. FExecuteMethod;
  779. end;
  780. { TSimpleStatusThread }
  781. constructor TSimpleStatusThread.Create(ExecuteMethod: TThreadExecuteStatusHandler;AOnStatus : TThreadStatusNotifyEvent; AOnTerminate: TNotifyEvent);
  782. begin
  783. FExecuteMethod := ExecuteMethod;
  784. OnTerminate := AOnTerminate;
  785. FOnStatus:=AOnStatus;
  786. FStatus:='';
  787. inherited Create(False);
  788. end;
  789. procedure TSimpleStatusThread.Execute;
  790. begin
  791. FreeOnTerminate := True;
  792. FExecuteMethod(@SetStatus);
  793. end;
  794. procedure TSimpleStatusThread.SetStatus(Const AStatus : String);
  795. begin
  796. If (AStatus=FStatus) then
  797. exit;
  798. FStatus:=AStatus;
  799. If Assigned(FOnStatus) then
  800. Synchronize(@DoStatus);
  801. end;
  802. procedure TSimpleStatusThread.DoStatus;
  803. begin
  804. FOnStatus(Self,FStatus);
  805. end;
  806. { TSimpleProcThread }
  807. constructor TSimpleProcThread.Create(ExecuteMethod: TThreadExecuteCallBack; AData : Pointer; AOnTerminate: TNotifyCallBack);
  808. begin
  809. FExecuteMethod := ExecuteMethod;
  810. FCallOnTerminate := AOnTerminate;
  811. FData:=AData;
  812. If Assigned(FCallOnTerminate) then
  813. OnTerminate:=@TerminateCallBack;
  814. inherited Create(False);
  815. end;
  816. procedure TSimpleProcThread.Execute;
  817. begin
  818. FreeOnTerminate := True;
  819. FExecuteMethod(FData);
  820. end;
  821. procedure TSimpleProcThread.TerminateCallBack(Sender : TObject);
  822. begin
  823. if Assigned(FCallOnTerminate) then
  824. FCallOnTerminate(Sender,FData);
  825. end;
  826. { TSimpleStatusProcThread }
  827. constructor TSimpleStatusProcThread.Create(ExecuteMethod: TThreadExecuteStatusCallback; AData : Pointer; AOnStatus : TThreadStatusNotifyCallBack; AOnTerminate: TNotifyCallBack);
  828. begin
  829. FExecuteMethod := ExecuteMethod;
  830. FCallOnTerminate := AOnTerminate;
  831. FData:=AData;
  832. If Assigned(FCallOnTerminate) then
  833. OnTerminate:=@TerminateCallBack;
  834. FOnStatus:=AOnStatus;
  835. FStatus:='';
  836. inherited Create(False);
  837. end;
  838. procedure TSimpleStatusProcThread.Execute;
  839. begin
  840. FreeOnTerminate := True;
  841. FExecuteMethod(FData,@SetStatus);
  842. end;
  843. procedure TSimpleStatusProcThread.SetStatus(Const AStatus : String);
  844. begin
  845. If (AStatus=FStatus) then
  846. exit;
  847. FStatus:=AStatus;
  848. If Assigned(FOnStatus) then
  849. Synchronize(@DoStatus);
  850. end;
  851. procedure TSimpleStatusProcThread.DoStatus;
  852. begin
  853. FOnStatus(Self,FData,FStatus);
  854. end;
  855. procedure TSimpleStatusProcThread.TerminateCallBack(Sender : TObject);
  856. begin
  857. if Assigned(FCallOnTerminate) then
  858. FCallOnTerminate(Sender,FData);
  859. end;
  860. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteHandler; AOnTerminate : TNotifyEvent = Nil) : TThread;
  861. begin
  862. Result:=TSimpleThread.Create(AMethod,AOnTerminate);
  863. end;
  864. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteCallback; AData : Pointer; AOnTerminate : TNotifyCallback = Nil) : TThread;
  865. begin
  866. Result:=TSimpleProcThread.Create(AMethod,AData,AOnTerminate);
  867. end;
  868. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteStatusHandler; AOnStatus : TThreadStatusNotifyEvent; AOnTerminate : TNotifyEvent = Nil) : TThread;
  869. begin
  870. If Not Assigned(AOnStatus) then
  871. Raise EThread.Create(SErrStatusCallBackRequired);
  872. Result:=TSimpleStatusThread.Create(AMethod,AOnStatus,AOnTerminate);
  873. end;
  874. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteStatusCallback; AOnStatus : TThreadStatusNotifyCallback;AData : Pointer = Nil; AOnTerminate : TNotifyCallBack = Nil) : TThread;
  875. begin
  876. If Not Assigned(AOnStatus) then
  877. Raise EThread.Create(SErrStatusCallBackRequired);
  878. Result:=TSimpleStatusProcThread.Create(AMethod,AData,AOnStatus,AOnTerminate);
  879. end;
  880. { TPersistent implementation }
  881. {$i persist.inc }
  882. {$i sllist.inc}
  883. {$i resref.inc}
  884. { TComponent implementation }
  885. {$i compon.inc}
  886. { TBasicAction implementation }
  887. {$i action.inc}
  888. { TDataModule implementation }
  889. {$i dm.inc}
  890. { Class and component registration routines }
  891. {$I cregist.inc}
  892. { Interface related stuff }
  893. {$I intf.inc}
  894. {**********************************************************************
  895. * Miscellaneous procedures and functions *
  896. **********************************************************************}
  897. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  898. var
  899. b, c : pchar;
  900. procedure SkipWhitespace;
  901. begin
  902. while (c^ in Whitespace) do
  903. inc (c);
  904. end;
  905. procedure AddString;
  906. var
  907. l : integer;
  908. s : string;
  909. begin
  910. l := c-b;
  911. if (l > 0) or AddEmptyStrings then
  912. begin
  913. if assigned(Strings) then
  914. begin
  915. setlength(s, l);
  916. if l>0 then
  917. move (b^, s[1],l*SizeOf(char));
  918. Strings.Add (s);
  919. end;
  920. inc (result);
  921. end;
  922. end;
  923. var
  924. quoted : char;
  925. begin
  926. result := 0;
  927. c := Content;
  928. Quoted := #0;
  929. Separators := Separators + [#13, #10] - ['''','"'];
  930. SkipWhitespace;
  931. b := c;
  932. while (c^ <> #0) do
  933. begin
  934. if (c^ = Quoted) then
  935. begin
  936. if ((c+1)^ = Quoted) then
  937. inc (c)
  938. else
  939. Quoted := #0
  940. end
  941. else if (Quoted = #0) and (c^ in ['''','"']) then
  942. Quoted := c^;
  943. if (Quoted = #0) and (c^ in Separators) then
  944. begin
  945. AddString;
  946. inc (c);
  947. SkipWhitespace;
  948. b := c;
  949. end
  950. else
  951. inc (c);
  952. end;
  953. if (c <> b) then
  954. AddString;
  955. end;
  956. { Point and rectangle constructors }
  957. function Point(AX, AY: Integer): TPoint;
  958. begin
  959. with Result do
  960. begin
  961. X := AX;
  962. Y := AY;
  963. end;
  964. end;
  965. function SmallPoint(AX, AY: SmallInt): TSmallPoint;
  966. begin
  967. with Result do
  968. begin
  969. X := AX;
  970. Y := AY;
  971. end;
  972. end;
  973. function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
  974. begin
  975. with Result do
  976. begin
  977. Left := ALeft;
  978. Top := ATop;
  979. Right := ARight;
  980. Bottom := ABottom;
  981. end;
  982. end;
  983. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  984. begin
  985. with Result do
  986. begin
  987. Left := ALeft;
  988. Top := ATop;
  989. Right := ALeft + AWidth;
  990. Bottom := ATop + AHeight;
  991. end;
  992. end;
  993. function PointsEqual(const P1, P2: TPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  994. begin
  995. { lazy, but should work }
  996. result:=QWord(P1)=QWord(P2);
  997. end;
  998. function PointsEqual(const P1, P2: TSmallPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  999. begin
  1000. { lazy, but should work }
  1001. result:=DWord(P1)=DWord(P2);
  1002. end;
  1003. function InvalidPoint(X, Y: Integer): Boolean;
  1004. begin
  1005. result:=(X=-1) and (Y=-1);
  1006. end;
  1007. function InvalidPoint(const At: TPoint): Boolean;
  1008. begin
  1009. result:=(At.x=-1) and (At.y=-1);
  1010. end;
  1011. function InvalidPoint(const At: TSmallPoint): Boolean;
  1012. begin
  1013. result:=(At.x=-1) and (At.y=-1);
  1014. end;
  1015. { Object filing routines }
  1016. var
  1017. IntConstList: TThreadList;
  1018. type
  1019. TIntConst = class
  1020. IntegerType: PTypeInfo; // The integer type RTTI pointer
  1021. IdentToIntFn: TIdentToInt; // Identifier to Integer conversion
  1022. IntToIdentFn: TIntToIdent; // Integer to Identifier conversion
  1023. constructor Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1024. AIntToIdent: TIntToIdent);
  1025. end;
  1026. constructor TIntConst.Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1027. AIntToIdent: TIntToIdent);
  1028. begin
  1029. IntegerType := AIntegerType;
  1030. IdentToIntFn := AIdentToInt;
  1031. IntToIdentFn := AIntToIdent;
  1032. end;
  1033. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt;
  1034. IntToIdentFn: TIntToIdent);
  1035. begin
  1036. IntConstList.Add(TIntConst.Create(IntegerType, IdentToIntFn, IntToIdentFn));
  1037. end;
  1038. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  1039. var
  1040. i: Integer;
  1041. begin
  1042. with IntConstList.LockList do
  1043. try
  1044. for i := 0 to Count - 1 do
  1045. if TIntConst(Items[i]).IntegerType = AIntegerType then
  1046. exit(TIntConst(Items[i]).IntToIdentFn);
  1047. Result := nil;
  1048. finally
  1049. IntConstList.UnlockList;
  1050. end;
  1051. end;
  1052. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  1053. var
  1054. i: Integer;
  1055. begin
  1056. with IntConstList.LockList do
  1057. try
  1058. for i := 0 to Count - 1 do
  1059. with TIntConst(Items[I]) do
  1060. if TIntConst(Items[I]).IntegerType = AIntegerType then
  1061. exit(IdentToIntFn);
  1062. Result := nil;
  1063. finally
  1064. IntConstList.UnlockList;
  1065. end;
  1066. end;
  1067. function IdentToInt(const Ident: String; out Int: LongInt;
  1068. const Map: array of TIdentMapEntry): Boolean;
  1069. var
  1070. i: Integer;
  1071. begin
  1072. for i := Low(Map) to High(Map) do
  1073. if CompareText(Map[i].Name, Ident) = 0 then
  1074. begin
  1075. Int := Map[i].Value;
  1076. exit(True);
  1077. end;
  1078. Result := False;
  1079. end;
  1080. function IntToIdent(Int: LongInt; var Ident: String;
  1081. const Map: array of TIdentMapEntry): Boolean;
  1082. var
  1083. i: Integer;
  1084. begin
  1085. for i := Low(Map) to High(Map) do
  1086. if Map[i].Value = Int then
  1087. begin
  1088. Ident := Map[i].Name;
  1089. exit(True);
  1090. end;
  1091. Result := False;
  1092. end;
  1093. function GlobalIdentToInt(const Ident: String; var Int: LongInt):boolean;
  1094. var
  1095. i : Integer;
  1096. begin
  1097. with IntConstList.LockList do
  1098. try
  1099. for i := 0 to Count - 1 do
  1100. if TIntConst(Items[I]).IdentToIntFn(Ident, Int) then
  1101. Exit(True);
  1102. Result := false;
  1103. finally
  1104. IntConstList.UnlockList;
  1105. end;
  1106. end;
  1107. { TPropFixup }
  1108. // Tainted. TPropFixup is being removed.
  1109. Type
  1110. TInitHandler = Class(TObject)
  1111. AHandler : TInitComponentHandler;
  1112. AClass : TComponentClass;
  1113. end;
  1114. {$ifndef i8086}
  1115. type
  1116. TCodePtrList = TList;
  1117. {$endif i8086}
  1118. Var
  1119. InitHandlerList : TList;
  1120. FindGlobalComponentList : TCodePtrList;
  1121. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1122. begin
  1123. if not(assigned(FindGlobalComponentList)) then
  1124. FindGlobalComponentList:=TCodePtrList.Create;
  1125. if FindGlobalComponentList.IndexOf(CodePointer(AFindGlobalComponent))<0 then
  1126. FindGlobalComponentList.Add(CodePointer(AFindGlobalComponent));
  1127. end;
  1128. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1129. begin
  1130. if assigned(FindGlobalComponentList) then
  1131. FindGlobalComponentList.Remove(CodePointer(AFindGlobalComponent));
  1132. end;
  1133. function FindGlobalComponent(const Name: string): TComponent;
  1134. var
  1135. i : sizeint;
  1136. begin
  1137. FindGlobalComponent:=nil;
  1138. if assigned(FindGlobalComponentList) then
  1139. begin
  1140. for i:=FindGlobalComponentList.Count-1 downto 0 do
  1141. begin
  1142. FindGlobalComponent:=TFindGlobalComponent(FindGlobalComponentList[i])(name);
  1143. if assigned(FindGlobalComponent) then
  1144. break;
  1145. end;
  1146. end;
  1147. end;
  1148. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  1149. Var
  1150. I : Integer;
  1151. H: TInitHandler;
  1152. begin
  1153. If (InitHandlerList=Nil) then
  1154. InitHandlerList:=TList.Create;
  1155. H:=TInitHandler.Create;
  1156. H.Aclass:=ComponentClass;
  1157. H.AHandler:=Handler;
  1158. try
  1159. With InitHandlerList do
  1160. begin
  1161. I:=0;
  1162. While (I<Count) and not H.AClass.InheritsFrom(TInitHandler(Items[I]).AClass) do
  1163. Inc(I);
  1164. { override? }
  1165. if (I<Count) and (TInitHandler(Items[I]).AClass=H.AClass) then
  1166. begin
  1167. TInitHandler(Items[I]).AHandler:=Handler;
  1168. H.Free;
  1169. end
  1170. else
  1171. InitHandlerList.Insert(I,H);
  1172. end;
  1173. except
  1174. H.Free;
  1175. raise;
  1176. end;
  1177. end;
  1178. { all targets should at least include the sysres.inc dummy in the system unit to compile this }
  1179. function CreateComponentfromRes(const res : string;Inst : THandle;var Component : TComponent) : Boolean;
  1180. var
  1181. ResStream : TResourceStream;
  1182. begin
  1183. result:=true;
  1184. if Inst=0 then
  1185. Inst:=HInstance;
  1186. try
  1187. ResStream:=TResourceStream.Create(Inst,res,RT_RCDATA);
  1188. try
  1189. Component:=ResStream.ReadComponent(Component);
  1190. finally
  1191. ResStream.Free;
  1192. end;
  1193. except
  1194. on EResNotFound do
  1195. result:=false;
  1196. end;
  1197. end;
  1198. function DefaultInitHandler(Instance: TComponent; RootAncestor: TClass): Boolean;
  1199. function doinit(_class : TClass) : boolean;
  1200. begin
  1201. result:=false;
  1202. if (_class.ClassType=TComponent) or (_class.ClassType=RootAncestor) then
  1203. exit;
  1204. result:=doinit(_class.ClassParent);
  1205. result:=CreateComponentfromRes(_class.ClassName,0,Instance) or result;
  1206. end;
  1207. begin
  1208. {$ifdef FPC_HAS_FEATURE_THREADING}
  1209. GlobalNameSpace.BeginWrite;
  1210. try
  1211. {$endif}
  1212. result:=doinit(Instance.ClassType);
  1213. {$ifdef FPC_HAS_FEATURE_THREADING}
  1214. finally
  1215. GlobalNameSpace.EndWrite;
  1216. end;
  1217. {$endif}
  1218. end;
  1219. function InitInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
  1220. Var
  1221. I : Integer;
  1222. begin
  1223. I:=0;
  1224. if not Assigned(InitHandlerList) then begin
  1225. Result := True;
  1226. Exit;
  1227. end;
  1228. Result:=False;
  1229. With InitHandlerList do
  1230. begin
  1231. I:=0;
  1232. // Instance is the normally the lowest one, so that one should be used when searching.
  1233. While Not result and (I<Count) do
  1234. begin
  1235. If (Instance.InheritsFrom(TInitHandler(Items[i]).AClass)) then
  1236. Result:=TInitHandler(Items[i]).AHandler(Instance,RootAncestor);
  1237. Inc(I);
  1238. end;
  1239. end;
  1240. end;
  1241. function InitComponentRes(const ResName: String; Instance: TComponent): Boolean;
  1242. begin
  1243. Result:=ReadComponentRes(ResName,Instance)=Instance;
  1244. end;
  1245. function SysReadComponentRes(HInstance : THandle; const ResName: String; Instance: TComponent): TComponent;
  1246. Var
  1247. H : TFPResourceHandle;
  1248. begin
  1249. { Windows unit also has a FindResource function, use the one from
  1250. system unit here. }
  1251. H:=system.FindResource(HInstance,ResName,RT_RCDATA);
  1252. if (PtrInt(H)=0) then
  1253. Result:=Nil
  1254. else
  1255. With TResourceStream.Create(HInstance,ResName,RT_RCDATA) do
  1256. try
  1257. Result:=ReadComponent(Instance);
  1258. Finally
  1259. Free;
  1260. end;
  1261. end;
  1262. function ReadComponentRes(const ResName: String; Instance: TComponent): TComponent;
  1263. begin
  1264. Result:=SysReadComponentRes(Hinstance,Resname,Instance);
  1265. end;
  1266. function ReadComponentResEx(HInstance: THandle; const ResName: String): TComponent;
  1267. begin
  1268. Result:=SysReadComponentRes(Hinstance,ResName,Nil);
  1269. end;
  1270. function ReadComponentResFile(const FileName: String; Instance: TComponent): TComponent;
  1271. var
  1272. FileStream: TStream;
  1273. begin
  1274. FileStream := TFileStream.Create(FileName, fmOpenRead {!!!:or fmShareDenyWrite});
  1275. try
  1276. Result := FileStream.ReadComponentRes(Instance);
  1277. finally
  1278. FileStream.Free;
  1279. end;
  1280. end;
  1281. procedure WriteComponentResFile(const FileName: String; Instance: TComponent);
  1282. var
  1283. FileStream: TStream;
  1284. begin
  1285. FileStream := TFileStream.Create(FileName, fmCreate);
  1286. try
  1287. FileStream.WriteComponentRes(Instance.ClassName, Instance);
  1288. finally
  1289. FileStream.Free;
  1290. end;
  1291. end;
  1292. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  1293. Function GetNextName : String; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1294. Var
  1295. P : Integer;
  1296. CM : Boolean;
  1297. begin
  1298. P:=Pos('.',APath);
  1299. CM:=False;
  1300. If (P=0) then
  1301. begin
  1302. If CStyle then
  1303. begin
  1304. P:=Pos('->',APath);
  1305. CM:=P<>0;
  1306. end;
  1307. If (P=0) Then
  1308. P:=Length(APath)+1;
  1309. end;
  1310. Result:=Copy(APath,1,P-1);
  1311. Delete(APath,1,P+Ord(CM));
  1312. end;
  1313. Var
  1314. C : TComponent;
  1315. S : String;
  1316. begin
  1317. If (APath='') then
  1318. Result:=Nil
  1319. else
  1320. begin
  1321. Result:=Root;
  1322. While (APath<>'') And (Result<>Nil) do
  1323. begin
  1324. C:=Result;
  1325. S:=Uppercase(GetNextName);
  1326. Result:=C.FindComponent(S);
  1327. If (Result=Nil) And (S='OWNER') then
  1328. Result:=C;
  1329. end;
  1330. end;
  1331. end;
  1332. {$ifdef FPC_HAS_FEATURE_THREADING}
  1333. threadvar
  1334. {$else}
  1335. var
  1336. {$endif}
  1337. GlobalLoaded, GlobalLists: TFpList;
  1338. procedure BeginGlobalLoading;
  1339. begin
  1340. if not Assigned(GlobalLists) then
  1341. GlobalLists := TFpList.Create;
  1342. GlobalLists.Add(GlobalLoaded);
  1343. GlobalLoaded := TFpList.Create;
  1344. end;
  1345. { Notify all global components that they have been loaded completely }
  1346. procedure NotifyGlobalLoading;
  1347. var
  1348. i: Integer;
  1349. begin
  1350. for i := 0 to GlobalLoaded.Count - 1 do
  1351. TComponent(GlobalLoaded[i]).Loaded;
  1352. end;
  1353. procedure EndGlobalLoading;
  1354. begin
  1355. { Free the memory occupied by BeginGlobalLoading }
  1356. GlobalLoaded.Free;
  1357. GlobalLoaded := TFpList(GlobalLists.Last);
  1358. GlobalLists.Delete(GlobalLists.Count - 1);
  1359. if GlobalLists.Count = 0 then
  1360. begin
  1361. GlobalLists.Free;
  1362. GlobalLists := nil;
  1363. end;
  1364. end;
  1365. function CollectionsEqual(C1, C2: TCollection): Boolean;
  1366. begin
  1367. // !!!: Implement this
  1368. CollectionsEqual:=false;
  1369. end;
  1370. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  1371. procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
  1372. var
  1373. w : twriter;
  1374. begin
  1375. w:=twriter.create(s,4096);
  1376. try
  1377. w.root:=o;
  1378. w.flookuproot:=o;
  1379. w.writecollection(c);
  1380. finally
  1381. w.free;
  1382. end;
  1383. end;
  1384. var
  1385. s1,s2 : tmemorystream;
  1386. begin
  1387. result:=false;
  1388. if (c1.classtype<>c2.classtype) or
  1389. (c1.count<>c2.count) then
  1390. exit;
  1391. if c1.count = 0 then
  1392. begin
  1393. result:= true;
  1394. exit;
  1395. end;
  1396. s1:=tmemorystream.create;
  1397. try
  1398. s2:=tmemorystream.create;
  1399. try
  1400. stream_collection(s1,c1,owner1);
  1401. stream_collection(s2,c2,owner2);
  1402. result:=(s1.size=s2.size) and (CompareChar(s1.memory^,s2.memory^,s1.size)=0);
  1403. finally
  1404. s2.free;
  1405. end;
  1406. finally
  1407. s1.free;
  1408. end;
  1409. end;
  1410. { Object conversion routines }
  1411. type
  1412. CharToOrdFuncty = Function(var charpo: Pointer): Cardinal;
  1413. function CharToOrd(var P: Pointer): Cardinal;
  1414. begin
  1415. result:= ord(pchar(P)^);
  1416. inc(pchar(P));
  1417. end;
  1418. function WideCharToOrd(var P: Pointer): Cardinal;
  1419. begin
  1420. result:= ord(pwidechar(P)^);
  1421. inc(pwidechar(P));
  1422. end;
  1423. function Utf8ToOrd(var P:Pointer): Cardinal;
  1424. begin
  1425. // Should also check for illegal utf8 combinations
  1426. Result := Ord(PChar(P)^);
  1427. Inc(P);
  1428. if (Result and $80) <> 0 then
  1429. if (Ord(Result) and %11100000) = %11000000 then begin
  1430. Result := ((Result and %00011111) shl 6)
  1431. or (ord(PChar(P)^) and %00111111);
  1432. Inc(P);
  1433. end else if (Ord(Result) and %11110000) = %11100000 then begin
  1434. Result := ((Result and %00011111) shl 12)
  1435. or ((ord(PChar(P)^) and %00111111) shl 6)
  1436. or (ord((PChar(P)+1)^) and %00111111);
  1437. Inc(P,2);
  1438. end else begin
  1439. Result := ((ord(Result) and %00011111) shl 18)
  1440. or ((ord(PChar(P)^) and %00111111) shl 12)
  1441. or ((ord((PChar(P)+1)^) and %00111111) shl 6)
  1442. or (ord((PChar(P)+2)^) and %00111111);
  1443. Inc(P,3);
  1444. end;
  1445. end;
  1446. procedure ObjectBinaryToText(Input, Output: TStream; Encoding: TObjectTextEncoding);
  1447. procedure OutStr(s: String);
  1448. begin
  1449. if Length(s) > 0 then
  1450. Output.Write(s[1], Length(s));
  1451. end;
  1452. procedure OutLn(s: String);
  1453. begin
  1454. OutStr(s + LineEnding);
  1455. end;
  1456. procedure Outchars(P, LastP : Pointer; CharToOrdFunc: CharToOrdFuncty;
  1457. UseBytes: boolean = false);
  1458. var
  1459. res, NewStr: String;
  1460. w: Cardinal;
  1461. InString, NewInString: Boolean;
  1462. begin
  1463. if p = nil then begin
  1464. res:= '''''';
  1465. end
  1466. else
  1467. begin
  1468. res := '';
  1469. InString := False;
  1470. while P < LastP do
  1471. begin
  1472. NewInString := InString;
  1473. w := CharToOrdfunc(P);
  1474. if w = ord('''') then
  1475. begin //quote char
  1476. if not InString then
  1477. NewInString := True;
  1478. NewStr := '''''';
  1479. end
  1480. else if (Ord(w) >= 32) and ((Ord(w) < 127) or (UseBytes and (Ord(w)<256))) then
  1481. begin //printable ascii or bytes
  1482. if not InString then
  1483. NewInString := True;
  1484. NewStr := char(w);
  1485. end
  1486. else
  1487. begin //ascii control chars, non ascii
  1488. if InString then
  1489. NewInString := False;
  1490. NewStr := '#' + IntToStr(w);
  1491. end;
  1492. if NewInString <> InString then
  1493. begin
  1494. NewStr := '''' + NewStr;
  1495. InString := NewInString;
  1496. end;
  1497. res := res + NewStr;
  1498. end;
  1499. if InString then
  1500. res := res + '''';
  1501. end;
  1502. OutStr(res);
  1503. end;
  1504. procedure OutString(s: String);
  1505. begin
  1506. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd,Encoding=oteLFM);
  1507. end;
  1508. procedure OutWString(W: WideString);
  1509. begin
  1510. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1511. end;
  1512. procedure OutUString(W: UnicodeString);
  1513. begin
  1514. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1515. end;
  1516. procedure OutUtf8Str(s: String);
  1517. begin
  1518. if Encoding=oteLFM then
  1519. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd)
  1520. else
  1521. OutChars(Pointer(S),PChar(S)+Length(S),@Utf8ToOrd);
  1522. end;
  1523. function ReadWord : word; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1524. begin
  1525. Result:=Input.ReadWord;
  1526. Result:=LEtoN(Result);
  1527. end;
  1528. function ReadDWord : longword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1529. begin
  1530. Result:=Input.ReadDWord;
  1531. Result:=LEtoN(Result);
  1532. end;
  1533. function ReadQWord : qword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1534. begin
  1535. Input.ReadBuffer(Result,sizeof(Result));
  1536. Result:=LEtoN(Result);
  1537. end;
  1538. {$ifndef FPUNONE}
  1539. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1540. function ExtendedToDouble(e : pointer) : double;
  1541. var mant : qword;
  1542. exp : smallint;
  1543. sign : boolean;
  1544. d : qword;
  1545. begin
  1546. move(pbyte(e)[0],mant,8); //mantissa : bytes 0..7
  1547. move(pbyte(e)[8],exp,2); //exponent and sign: bytes 8..9
  1548. mant:=LEtoN(mant);
  1549. exp:=LetoN(word(exp));
  1550. sign:=(exp and $8000)<>0;
  1551. if sign then exp:=exp and $7FFF;
  1552. case exp of
  1553. 0 : mant:=0; //if denormalized, value is too small for double,
  1554. //so it's always zero
  1555. $7FFF : exp:=2047 //either infinity or NaN
  1556. else
  1557. begin
  1558. dec(exp,16383-1023);
  1559. if (exp>=-51) and (exp<=0) then //can be denormalized
  1560. begin
  1561. mant:=mant shr (-exp);
  1562. exp:=0;
  1563. end
  1564. else
  1565. if (exp<-51) or (exp>2046) then //exponent too large.
  1566. begin
  1567. Result:=0;
  1568. exit;
  1569. end
  1570. else //normalized value
  1571. mant:=mant shl 1; //hide most significant bit
  1572. end;
  1573. end;
  1574. d:=word(exp);
  1575. d:=d shl 52;
  1576. mant:=mant shr 12;
  1577. d:=d or mant;
  1578. if sign then d:=d or $8000000000000000;
  1579. Result:=pdouble(@d)^;
  1580. end;
  1581. {$ENDIF}
  1582. {$endif}
  1583. function ReadInt(ValueType: TValueType): Int64;
  1584. begin
  1585. case ValueType of
  1586. vaInt8: Result := ShortInt(Input.ReadByte);
  1587. vaInt16: Result := SmallInt(ReadWord);
  1588. vaInt32: Result := LongInt(ReadDWord);
  1589. vaInt64: Result := Int64(ReadQWord);
  1590. end;
  1591. end;
  1592. function ReadInt: Int64;
  1593. begin
  1594. Result := ReadInt(TValueType(Input.ReadByte));
  1595. end;
  1596. {$ifndef FPUNONE}
  1597. function ReadExtended : extended;
  1598. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1599. var ext : array[0..9] of byte;
  1600. {$ENDIF}
  1601. begin
  1602. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1603. Input.ReadBuffer(ext[0],10);
  1604. Result:=ExtendedToDouble(@(ext[0]));
  1605. {$ELSE}
  1606. Input.ReadBuffer(Result,sizeof(Result));
  1607. {$ENDIF}
  1608. end;
  1609. {$endif}
  1610. function ReadSStr: String;
  1611. var
  1612. len: Byte;
  1613. begin
  1614. len := Input.ReadByte;
  1615. SetLength(Result, len);
  1616. if (len > 0) then
  1617. Input.ReadBuffer(Result[1], len);
  1618. end;
  1619. function ReadLStr: String;
  1620. var
  1621. len: DWord;
  1622. begin
  1623. len := ReadDWord;
  1624. SetLength(Result, len);
  1625. if (len > 0) then
  1626. Input.ReadBuffer(Result[1], len);
  1627. end;
  1628. function ReadWStr: WideString;
  1629. var
  1630. len: DWord;
  1631. {$IFDEF ENDIAN_BIG}
  1632. i : integer;
  1633. {$ENDIF}
  1634. begin
  1635. len := ReadDWord;
  1636. SetLength(Result, len);
  1637. if (len > 0) then
  1638. begin
  1639. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1640. {$IFDEF ENDIAN_BIG}
  1641. for i:=1 to len do
  1642. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1643. {$ENDIF}
  1644. end;
  1645. end;
  1646. function ReadUStr: UnicodeString;
  1647. var
  1648. len: DWord;
  1649. {$IFDEF ENDIAN_BIG}
  1650. i : integer;
  1651. {$ENDIF}
  1652. begin
  1653. len := ReadDWord;
  1654. SetLength(Result, len);
  1655. if (len > 0) then
  1656. begin
  1657. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1658. {$IFDEF ENDIAN_BIG}
  1659. for i:=1 to len do
  1660. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1661. {$ENDIF}
  1662. end;
  1663. end;
  1664. procedure ReadPropList(indent: String);
  1665. procedure ProcessValue(ValueType: TValueType; Indent: String);
  1666. procedure ProcessBinary;
  1667. var
  1668. ToDo, DoNow, i: LongInt;
  1669. lbuf: array[0..31] of Byte;
  1670. s: String;
  1671. begin
  1672. ToDo := ReadDWord;
  1673. OutLn('{');
  1674. while ToDo > 0 do begin
  1675. DoNow := ToDo;
  1676. if DoNow > 32 then DoNow := 32;
  1677. Dec(ToDo, DoNow);
  1678. s := Indent + ' ';
  1679. Input.ReadBuffer(lbuf, DoNow);
  1680. for i := 0 to DoNow - 1 do
  1681. s := s + IntToHex(lbuf[i], 2);
  1682. OutLn(s);
  1683. end;
  1684. OutLn(indent + '}');
  1685. end;
  1686. var
  1687. s: String;
  1688. { len: LongInt; }
  1689. IsFirst: Boolean;
  1690. {$ifndef FPUNONE}
  1691. ext: Extended;
  1692. {$endif}
  1693. begin
  1694. case ValueType of
  1695. vaList: begin
  1696. OutStr('(');
  1697. IsFirst := True;
  1698. while True do begin
  1699. ValueType := TValueType(Input.ReadByte);
  1700. if ValueType = vaNull then break;
  1701. if IsFirst then begin
  1702. OutLn('');
  1703. IsFirst := False;
  1704. end;
  1705. OutStr(Indent + ' ');
  1706. ProcessValue(ValueType, Indent + ' ');
  1707. end;
  1708. OutLn(Indent + ')');
  1709. end;
  1710. vaInt8: OutLn(IntToStr(ShortInt(Input.ReadByte)));
  1711. vaInt16: OutLn( IntToStr(SmallInt(ReadWord)));
  1712. vaInt32: OutLn(IntToStr(LongInt(ReadDWord)));
  1713. vaInt64: OutLn(IntToStr(Int64(ReadQWord)));
  1714. {$ifndef FPUNONE}
  1715. vaExtended: begin
  1716. ext:=ReadExtended;
  1717. Str(ext,S);// Do not use localized strings.
  1718. OutLn(S);
  1719. end;
  1720. {$endif}
  1721. vaString: begin
  1722. OutString(ReadSStr);
  1723. OutLn('');
  1724. end;
  1725. vaIdent: OutLn(ReadSStr);
  1726. vaFalse: OutLn('False');
  1727. vaTrue: OutLn('True');
  1728. vaBinary: ProcessBinary;
  1729. vaSet: begin
  1730. OutStr('[');
  1731. IsFirst := True;
  1732. while True do begin
  1733. s := ReadSStr;
  1734. if Length(s) = 0 then break;
  1735. if not IsFirst then OutStr(', ');
  1736. IsFirst := False;
  1737. OutStr(s);
  1738. end;
  1739. OutLn(']');
  1740. end;
  1741. vaLString:
  1742. begin
  1743. OutString(ReadLStr);
  1744. OutLn('');
  1745. end;
  1746. vaWString:
  1747. begin
  1748. OutWString(ReadWStr);
  1749. OutLn('');
  1750. end;
  1751. vaUString:
  1752. begin
  1753. OutWString(ReadWStr);
  1754. OutLn('');
  1755. end;
  1756. vaNil:
  1757. OutLn('nil');
  1758. vaCollection: begin
  1759. OutStr('<');
  1760. while Input.ReadByte <> 0 do begin
  1761. OutLn(Indent);
  1762. Input.Seek(-1, soFromCurrent);
  1763. OutStr(indent + ' item');
  1764. ValueType := TValueType(Input.ReadByte);
  1765. if ValueType <> vaList then
  1766. OutStr('[' + IntToStr(ReadInt(ValueType)) + ']');
  1767. OutLn('');
  1768. ReadPropList(indent + ' ');
  1769. OutStr(indent + ' end');
  1770. end;
  1771. OutLn('>');
  1772. end;
  1773. {vaSingle: begin OutLn('!!Single!!'); exit end;
  1774. vaCurrency: begin OutLn('!!Currency!!'); exit end;
  1775. vaDate: begin OutLn('!!Date!!'); exit end;}
  1776. vaUTF8String: begin
  1777. OutUtf8Str(ReadLStr);
  1778. OutLn('');
  1779. end;
  1780. else
  1781. Raise EReadError.CreateFmt(SErrInvalidPropertyType,[Ord(ValueType)]);
  1782. end;
  1783. end;
  1784. begin
  1785. while Input.ReadByte <> 0 do begin
  1786. Input.Seek(-1, soFromCurrent);
  1787. OutStr(indent + ReadSStr + ' = ');
  1788. ProcessValue(TValueType(Input.ReadByte), Indent);
  1789. end;
  1790. end;
  1791. procedure ReadObject(indent: String);
  1792. var
  1793. b: Byte;
  1794. ObjClassName, ObjName: String;
  1795. ChildPos: LongInt;
  1796. begin
  1797. // Check for FilerFlags
  1798. b := Input.ReadByte;
  1799. if (b and $f0) = $f0 then begin
  1800. if (b and 2) <> 0 then ChildPos := ReadInt;
  1801. end else begin
  1802. b := 0;
  1803. Input.Seek(-1, soFromCurrent);
  1804. end;
  1805. ObjClassName := ReadSStr;
  1806. ObjName := ReadSStr;
  1807. OutStr(Indent);
  1808. if (b and 1) <> 0 then OutStr('inherited')
  1809. else
  1810. if (b and 4) <> 0 then OutStr('inline')
  1811. else OutStr('object');
  1812. OutStr(' ');
  1813. if ObjName <> '' then
  1814. OutStr(ObjName + ': ');
  1815. OutStr(ObjClassName);
  1816. if (b and 2) <> 0 then OutStr('[' + IntToStr(ChildPos) + ']');
  1817. OutLn('');
  1818. ReadPropList(indent + ' ');
  1819. while Input.ReadByte <> 0 do begin
  1820. Input.Seek(-1, soFromCurrent);
  1821. ReadObject(indent + ' ');
  1822. end;
  1823. OutLn(indent + 'end');
  1824. end;
  1825. type
  1826. PLongWord = ^LongWord;
  1827. const
  1828. signature: PChar = 'TPF0';
  1829. begin
  1830. if Input.ReadDWord <> PLongWord(Pointer(signature))^ then
  1831. raise EReadError.Create('Illegal stream image' {###SInvalidImage});
  1832. ReadObject('');
  1833. end;
  1834. procedure ObjectBinaryToText(Input, Output: TStream);
  1835. begin
  1836. ObjectBinaryToText(Input,Output,oteDFM);
  1837. end;
  1838. procedure ObjectTextToBinary(Input, Output: TStream);
  1839. var
  1840. parser: TParser;
  1841. procedure WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1842. begin
  1843. w:=NtoLE(w);
  1844. Output.WriteWord(w);
  1845. end;
  1846. procedure WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1847. begin
  1848. lw:=NtoLE(lw);
  1849. Output.WriteDWord(lw);
  1850. end;
  1851. procedure WriteQWord(qw : qword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1852. begin
  1853. qw:=NtoLE(qw);
  1854. Output.WriteBuffer(qw,sizeof(qword));
  1855. end;
  1856. {$ifndef FPUNONE}
  1857. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1858. procedure DoubleToExtended(d : double; e : pointer);
  1859. var mant : qword;
  1860. exp : smallint;
  1861. sign : boolean;
  1862. begin
  1863. mant:=(qword(d) and $000FFFFFFFFFFFFF) shl 12;
  1864. exp :=(qword(d) shr 52) and $7FF;
  1865. sign:=(qword(d) and $8000000000000000)<>0;
  1866. case exp of
  1867. 0 : begin
  1868. if mant<>0 then //denormalized value: hidden bit is 0. normalize it
  1869. begin
  1870. exp:=16383-1022;
  1871. while (mant and $8000000000000000)=0 do
  1872. begin
  1873. dec(exp);
  1874. mant:=mant shl 1;
  1875. end;
  1876. dec(exp); //don't shift, most significant bit is not hidden in extended
  1877. end;
  1878. end;
  1879. 2047 : exp:=$7FFF //either infinity or NaN
  1880. else
  1881. begin
  1882. inc(exp,16383-1023);
  1883. mant:=(mant shr 1) or $8000000000000000; //unhide hidden bit
  1884. end;
  1885. end;
  1886. if sign then exp:=exp or $8000;
  1887. mant:=NtoLE(mant);
  1888. exp:=NtoLE(word(exp));
  1889. move(mant,pbyte(e)[0],8); //mantissa : bytes 0..7
  1890. move(exp,pbyte(e)[8],2); //exponent and sign: bytes 8..9
  1891. end;
  1892. {$ENDIF}
  1893. procedure WriteExtended(e : extended);
  1894. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1895. var ext : array[0..9] of byte;
  1896. {$ENDIF}
  1897. begin
  1898. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1899. DoubleToExtended(e,@(ext[0]));
  1900. Output.WriteBuffer(ext[0],10);
  1901. {$ELSE}
  1902. Output.WriteBuffer(e,sizeof(e));
  1903. {$ENDIF}
  1904. end;
  1905. {$endif}
  1906. procedure WriteString(s: String);
  1907. var size : byte;
  1908. begin
  1909. if length(s)>255 then size:=255
  1910. else size:=length(s);
  1911. Output.WriteByte(size);
  1912. if Length(s) > 0 then
  1913. Output.WriteBuffer(s[1], size);
  1914. end;
  1915. procedure WriteLString(Const s: String);
  1916. begin
  1917. WriteDWord(Length(s));
  1918. if Length(s) > 0 then
  1919. Output.WriteBuffer(s[1], Length(s));
  1920. end;
  1921. procedure WriteWString(Const s: WideString);
  1922. var len : longword;
  1923. {$IFDEF ENDIAN_BIG}
  1924. i : integer;
  1925. ws : widestring;
  1926. {$ENDIF}
  1927. begin
  1928. len:=Length(s);
  1929. WriteDWord(len);
  1930. if len > 0 then
  1931. begin
  1932. {$IFDEF ENDIAN_BIG}
  1933. setlength(ws,len);
  1934. for i:=1 to len do
  1935. ws[i]:=widechar(SwapEndian(word(s[i])));
  1936. Output.WriteBuffer(ws[1], len*sizeof(widechar));
  1937. {$ELSE}
  1938. Output.WriteBuffer(s[1], len*sizeof(widechar));
  1939. {$ENDIF}
  1940. end;
  1941. end;
  1942. procedure WriteInteger(value: Int64);
  1943. begin
  1944. if (value >= -128) and (value <= 127) then begin
  1945. Output.WriteByte(Ord(vaInt8));
  1946. Output.WriteByte(byte(value));
  1947. end else if (value >= -32768) and (value <= 32767) then begin
  1948. Output.WriteByte(Ord(vaInt16));
  1949. WriteWord(word(value));
  1950. end else if (value >= -2147483648) and (value <= 2147483647) then begin
  1951. Output.WriteByte(Ord(vaInt32));
  1952. WriteDWord(longword(value));
  1953. end else begin
  1954. Output.WriteByte(ord(vaInt64));
  1955. WriteQWord(qword(value));
  1956. end;
  1957. end;
  1958. procedure ProcessWideString(const left : widestring);
  1959. var ws : widestring;
  1960. begin
  1961. ws:=left+parser.TokenWideString;
  1962. while parser.NextToken = '+' do
  1963. begin
  1964. parser.NextToken; // Get next string fragment
  1965. if not (parser.Token in [toString,toWString]) then
  1966. parser.CheckToken(toWString);
  1967. ws:=ws+parser.TokenWideString;
  1968. end;
  1969. Output.WriteByte(Ord(vaWstring));
  1970. WriteWString(ws);
  1971. end;
  1972. procedure ProcessProperty; forward;
  1973. procedure ProcessValue;
  1974. var
  1975. {$ifndef FPUNONE}
  1976. flt: Extended;
  1977. {$endif}
  1978. s: String;
  1979. stream: TMemoryStream;
  1980. begin
  1981. case parser.Token of
  1982. toInteger:
  1983. begin
  1984. WriteInteger(parser.TokenInt);
  1985. parser.NextToken;
  1986. end;
  1987. {$ifndef FPUNONE}
  1988. toFloat:
  1989. begin
  1990. Output.WriteByte(Ord(vaExtended));
  1991. flt := Parser.TokenFloat;
  1992. WriteExtended(flt);
  1993. parser.NextToken;
  1994. end;
  1995. {$endif}
  1996. toString:
  1997. begin
  1998. s := parser.TokenString;
  1999. while parser.NextToken = '+' do
  2000. begin
  2001. parser.NextToken; // Get next string fragment
  2002. case parser.Token of
  2003. toString : s:=s+parser.TokenString;
  2004. toWString : begin
  2005. ProcessWideString(WideString(s));
  2006. exit;
  2007. end
  2008. else parser.CheckToken(toString);
  2009. end;
  2010. end;
  2011. if (length(S)>255) then
  2012. begin
  2013. Output.WriteByte(Ord(vaLString));
  2014. WriteLString(S);
  2015. end
  2016. else
  2017. begin
  2018. Output.WriteByte(Ord(vaString));
  2019. WriteString(s);
  2020. end;
  2021. end;
  2022. toWString:
  2023. ProcessWideString('');
  2024. toSymbol:
  2025. begin
  2026. if CompareText(parser.TokenString, 'True') = 0 then
  2027. Output.WriteByte(Ord(vaTrue))
  2028. else if CompareText(parser.TokenString, 'False') = 0 then
  2029. Output.WriteByte(Ord(vaFalse))
  2030. else if CompareText(parser.TokenString, 'nil') = 0 then
  2031. Output.WriteByte(Ord(vaNil))
  2032. else
  2033. begin
  2034. Output.WriteByte(Ord(vaIdent));
  2035. WriteString(parser.TokenComponentIdent);
  2036. end;
  2037. Parser.NextToken;
  2038. end;
  2039. // Set
  2040. '[':
  2041. begin
  2042. parser.NextToken;
  2043. Output.WriteByte(Ord(vaSet));
  2044. if parser.Token <> ']' then
  2045. while True do
  2046. begin
  2047. parser.CheckToken(toSymbol);
  2048. WriteString(parser.TokenString);
  2049. parser.NextToken;
  2050. if parser.Token = ']' then
  2051. break;
  2052. parser.CheckToken(',');
  2053. parser.NextToken;
  2054. end;
  2055. Output.WriteByte(0);
  2056. parser.NextToken;
  2057. end;
  2058. // List
  2059. '(':
  2060. begin
  2061. parser.NextToken;
  2062. Output.WriteByte(Ord(vaList));
  2063. while parser.Token <> ')' do
  2064. ProcessValue;
  2065. Output.WriteByte(0);
  2066. parser.NextToken;
  2067. end;
  2068. // Collection
  2069. '<':
  2070. begin
  2071. parser.NextToken;
  2072. Output.WriteByte(Ord(vaCollection));
  2073. while parser.Token <> '>' do
  2074. begin
  2075. parser.CheckTokenSymbol('item');
  2076. parser.NextToken;
  2077. // ConvertOrder
  2078. Output.WriteByte(Ord(vaList));
  2079. while not parser.TokenSymbolIs('end') do
  2080. ProcessProperty;
  2081. parser.NextToken; // Skip 'end'
  2082. Output.WriteByte(0);
  2083. end;
  2084. Output.WriteByte(0);
  2085. parser.NextToken;
  2086. end;
  2087. // Binary data
  2088. '{':
  2089. begin
  2090. Output.WriteByte(Ord(vaBinary));
  2091. stream := TMemoryStream.Create;
  2092. try
  2093. parser.HexToBinary(stream);
  2094. WriteDWord(stream.Size);
  2095. Output.WriteBuffer(Stream.Memory^, stream.Size);
  2096. finally
  2097. stream.Free;
  2098. end;
  2099. parser.NextToken;
  2100. end;
  2101. else
  2102. parser.Error(SInvalidProperty);
  2103. end;
  2104. end;
  2105. procedure ProcessProperty;
  2106. var
  2107. name: String;
  2108. begin
  2109. // Get name of property
  2110. parser.CheckToken(toSymbol);
  2111. name := parser.TokenString;
  2112. while True do begin
  2113. parser.NextToken;
  2114. if parser.Token <> '.' then break;
  2115. parser.NextToken;
  2116. parser.CheckToken(toSymbol);
  2117. name := name + '.' + parser.TokenString;
  2118. end;
  2119. WriteString(name);
  2120. parser.CheckToken('=');
  2121. parser.NextToken;
  2122. ProcessValue;
  2123. end;
  2124. procedure ProcessObject;
  2125. var
  2126. Flags: Byte;
  2127. ObjectName, ObjectType: String;
  2128. ChildPos: Integer;
  2129. begin
  2130. if parser.TokenSymbolIs('OBJECT') then
  2131. Flags :=0 { IsInherited := False }
  2132. else begin
  2133. if parser.TokenSymbolIs('INHERITED') then
  2134. Flags := 1 { IsInherited := True; }
  2135. else begin
  2136. parser.CheckTokenSymbol('INLINE');
  2137. Flags := 4;
  2138. end;
  2139. end;
  2140. parser.NextToken;
  2141. parser.CheckToken(toSymbol);
  2142. ObjectName := '';
  2143. ObjectType := parser.TokenString;
  2144. parser.NextToken;
  2145. if parser.Token = ':' then begin
  2146. parser.NextToken;
  2147. parser.CheckToken(toSymbol);
  2148. ObjectName := ObjectType;
  2149. ObjectType := parser.TokenString;
  2150. parser.NextToken;
  2151. if parser.Token = '[' then begin
  2152. parser.NextToken;
  2153. ChildPos := parser.TokenInt;
  2154. parser.NextToken;
  2155. parser.CheckToken(']');
  2156. parser.NextToken;
  2157. Flags := Flags or 2;
  2158. end;
  2159. end;
  2160. if Flags <> 0 then begin
  2161. Output.WriteByte($f0 or Flags);
  2162. if (Flags and 2) <> 0 then
  2163. WriteInteger(ChildPos);
  2164. end;
  2165. WriteString(ObjectType);
  2166. WriteString(ObjectName);
  2167. // Convert property list
  2168. while not (parser.TokenSymbolIs('END') or
  2169. parser.TokenSymbolIs('OBJECT') or
  2170. parser.TokenSymbolIs('INHERITED') or
  2171. parser.TokenSymbolIs('INLINE')) do
  2172. ProcessProperty;
  2173. Output.WriteByte(0); // Terminate property list
  2174. // Convert child objects
  2175. while not parser.TokenSymbolIs('END') do ProcessObject;
  2176. parser.NextToken; // Skip end token
  2177. Output.WriteByte(0); // Terminate property list
  2178. end;
  2179. const
  2180. signature: PChar = 'TPF0';
  2181. begin
  2182. parser := TParser.Create(Input);
  2183. try
  2184. Output.WriteBuffer(signature[0], 4);
  2185. ProcessObject;
  2186. finally
  2187. parser.Free;
  2188. end;
  2189. end;
  2190. procedure ObjectResourceToText(Input, Output: TStream);
  2191. begin
  2192. Input.ReadResHeader;
  2193. ObjectBinaryToText(Input, Output);
  2194. end;
  2195. procedure ObjectTextToResource(Input, Output: TStream);
  2196. var
  2197. StartPos, FixupInfo: LongInt;
  2198. parser: TParser;
  2199. name: String;
  2200. begin
  2201. // Get form type name
  2202. StartPos := Input.Position;
  2203. parser := TParser.Create(Input);
  2204. try
  2205. if not parser.TokenSymbolIs('OBJECT') then parser.CheckTokenSymbol('INHERITED');
  2206. parser.NextToken;
  2207. parser.CheckToken(toSymbol);
  2208. parser.NextToken;
  2209. parser.CheckToken(':');
  2210. parser.NextToken;
  2211. parser.CheckToken(toSymbol);
  2212. name := parser.TokenString;
  2213. finally
  2214. parser.Free;
  2215. Input.Position := StartPos;
  2216. end;
  2217. name := UpperCase(name);
  2218. Output.WriteResourceHeader(name,FixupInfo); // Write resource header
  2219. ObjectTextToBinary(Input, Output); // Convert the stuff!
  2220. Output.FixupResourceHeader(FixupInfo); // Insert real resource data size
  2221. end;
  2222. { Utility routines }
  2223. Function IfThen(AValue: Boolean; const ATrue: TStringList; const AFalse: TStringList = nil): TStringList; overload;
  2224. begin
  2225. if avalue then
  2226. result:=atrue
  2227. else
  2228. result:=afalse;
  2229. end;
  2230. function LineStart(Buffer, BufPos: PChar): PChar;
  2231. begin
  2232. Result := BufPos;
  2233. while Result > Buffer do begin
  2234. Dec(Result);
  2235. if Result[0] = #10 then break;
  2236. end;
  2237. end;
  2238. procedure CommonInit;
  2239. begin
  2240. {$ifdef FPC_HAS_FEATURE_THREADING}
  2241. SynchronizeTimeoutEvent:=RtlEventCreate;
  2242. InterlockedIncrement(ThreadQueueLockCounter);
  2243. InitCriticalSection(ThreadQueueLock);
  2244. MainThreadID:=GetCurrentThreadID;
  2245. {$else}
  2246. MainThreadID:=0{GetCurrentThreadID};
  2247. {$endif}
  2248. ExternalThreads := TThreadList.Create;
  2249. {$ifdef FPC_HAS_FEATURE_THREADING}
  2250. InitCriticalsection(ResolveSection);
  2251. TThread.FProcessorCount := CPUCount;
  2252. {$else}
  2253. TThread.FProcessorCount := 1{CPUCount};
  2254. {$endif}
  2255. InitHandlerList:=Nil;
  2256. FindGlobalComponentList:=nil;
  2257. IntConstList := TThreadList.Create;
  2258. ClassList := TThreadList.Create;
  2259. ClassAliasList := nil;
  2260. { on unix this maps to a simple rw synchornizer }
  2261. GlobalNameSpace := TMultiReadExclusiveWriteSynchronizer.Create;
  2262. RegisterInitComponentHandler(TComponent,@DefaultInitHandler);
  2263. end;
  2264. procedure CommonCleanup;
  2265. var
  2266. i: Integer;
  2267. tmpentry: TThread.PThreadQueueEntry;
  2268. begin
  2269. {$ifdef FPC_HAS_FEATURE_THREADING}
  2270. GlobalNameSpace.BeginWrite;
  2271. {$endif}
  2272. with IntConstList.LockList do
  2273. try
  2274. for i := 0 to Count - 1 do
  2275. TIntConst(Items[I]).Free;
  2276. finally
  2277. IntConstList.UnlockList;
  2278. end;
  2279. IntConstList.Free;
  2280. ClassList.Free;
  2281. ClassAliasList.Free;
  2282. RemoveFixupReferences(nil, '');
  2283. {$ifdef FPC_HAS_FEATURE_THREADING}
  2284. DoneCriticalsection(ResolveSection);
  2285. {$endif}
  2286. GlobalLists.Free;
  2287. ComponentPages.Free;
  2288. FreeAndNil(NeedResolving);
  2289. {$ifdef FPC_HAS_FEATURE_THREADING}
  2290. GlobalNameSpace.EndWrite;
  2291. {$endif}
  2292. { GlobalNameSpace is an interface so this is enough }
  2293. GlobalNameSpace:=nil;
  2294. if (InitHandlerList<>Nil) then
  2295. for i := 0 to InitHandlerList.Count - 1 do
  2296. TInitHandler(InitHandlerList.Items[I]).Free;
  2297. InitHandlerList.Free;
  2298. InitHandlerList:=Nil;
  2299. FindGlobalComponentList.Free;
  2300. FindGlobalComponentList:=nil;
  2301. ExternalThreadsCleanup:=True;
  2302. with ExternalThreads.LockList do
  2303. try
  2304. for i := 0 to Count - 1 do
  2305. TThread(Items[i]).Free;
  2306. finally
  2307. ExternalThreads.UnlockList;
  2308. end;
  2309. FreeAndNil(ExternalThreads);
  2310. {$ifdef FPC_HAS_FEATURE_THREADING}
  2311. RtlEventDestroy(SynchronizeTimeoutEvent);
  2312. try
  2313. System.EnterCriticalSection(ThreadQueueLock);
  2314. {$endif}
  2315. { clean up the queue, but keep in mind that the entries used for Synchronize
  2316. are owned by the corresponding TThread }
  2317. while Assigned(ThreadQueueHead) do begin
  2318. tmpentry := ThreadQueueHead;
  2319. ThreadQueueHead := tmpentry^.Next;
  2320. if not Assigned(tmpentry^.SyncEvent) then
  2321. Dispose(tmpentry);
  2322. end;
  2323. { We also need to reset ThreadQueueTail }
  2324. ThreadQueueTail := nil;
  2325. {$ifdef FPC_HAS_FEATURE_THREADING}
  2326. finally
  2327. System.LeaveCriticalSection(ThreadQueueLock);
  2328. end;
  2329. if InterlockedDecrement(ThreadQueueLockCounter)=0 then
  2330. DoneCriticalSection(ThreadQueueLock);
  2331. {$endif}
  2332. end;
  2333. { TFiler implementation }
  2334. {$i filer.inc}
  2335. { TReader implementation }
  2336. {$i reader.inc}
  2337. { TWriter implementations }
  2338. {$i writer.inc}
  2339. {$i twriter.inc}