bclasses.pas 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. {==============================================================================|
  2. | Project : Bauglir Library |
  3. |==============================================================================|
  4. | Content: Generic objects |
  5. |==============================================================================|
  6. | Copyright (c)2011-2012, Bronislav Klucka |
  7. | All rights reserved. |
  8. | Source code is licenced under original 4-clause BSD licence: |
  9. | http://licence.bauglir.com/bsd4.php |
  10. | |
  11. | |
  12. |==============================================================================|
  13. |==============================================================================}
  14. unit BClasses;
  15. {$IFDEF FPC}
  16. {$MODE DELPHI}
  17. {$ENDIF}
  18. {$H+}
  19. interface
  20. uses
  21. {$IFDEF UNIX}
  22. cthreads,
  23. {$ELSE UNIX}
  24. windows,
  25. {$ENDIF}
  26. Classes, SysUtils, SyncObjs;
  27. Type
  28. {:abstract(Basic library aware thread)
  29. See @BauglirInDll variable
  30. }
  31. TBThread = class(TThread)
  32. protected
  33. fSyncLock: TCriticalSection;
  34. procedure Synchronize(AMethod: TThreadMethod);
  35. public
  36. constructor Create(CreateSuspended: Boolean);
  37. destructor Destroy; override;
  38. end;
  39. var
  40. {:If @TRUE, than method passed TBThread.Synchronize will be executed directly,
  41. without synchronization, useful for libraries and cosole projects.
  42. }
  43. BauglirSynchronizeThreads: boolean = false;
  44. implementation
  45. { TBThread }
  46. constructor TBThread.Create(CreateSuspended: Boolean);
  47. begin
  48. inherited;
  49. fSyncLock := TCriticalSection.Create;
  50. end;
  51. destructor TBThread.Destroy;
  52. begin
  53. fSyncLock.Free;
  54. inherited;
  55. end;
  56. procedure TBThread.Synchronize(AMethod: TThreadMethod);
  57. begin
  58. //fSyncLock.Enter;
  59. if (BauglirSynchronizeThreads) or (GetCurrentThreadID = MainThreadID) then aMethod
  60. else inherited Synchronize(aMethod);
  61. //fSyncLock.Leave;
  62. end;
  63. end.