syncobjs.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1998 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. unit syncobjs;
  14. interface
  15. uses
  16. sysutils;
  17. type
  18. PSecurityAttributes = Pointer;
  19. TEventHandle = Pointer;
  20. const
  21. INFINITE = Cardinal(-1);
  22. type
  23. ESyncObjectException = Class(Exception);
  24. ELockException = Class(ESyncObjectException);
  25. ELockRecursionException = Class(ESyncObjectException);
  26. TWaitResult = (wrSignaled, wrTimeout, wrAbandoned, wrError);
  27. TSynchroObject = class(TObject)
  28. procedure Acquire;virtual;
  29. procedure Release;virtual;
  30. end;
  31. TCriticalSection = class(TSynchroObject)
  32. private
  33. CriticalSection : TRTLCriticalSection;
  34. public
  35. procedure Acquire;override;
  36. procedure Release;override;
  37. procedure Enter;
  38. function TryEnter:boolean;
  39. procedure Leave;
  40. constructor Create;
  41. destructor Destroy;override;
  42. end;
  43. THandleObject = class(TSynchroObject)
  44. protected
  45. FHandle : TEventHandle;
  46. FLastError : Integer;
  47. public
  48. destructor destroy;override;
  49. property Handle : TEventHandle read FHandle;
  50. property LastError : Integer read FLastError;
  51. end;
  52. TEventObject = class(THandleObject)
  53. private
  54. FManualReset: Boolean;
  55. public
  56. constructor Create(EventAttributes : PSecurityAttributes;
  57. AManualReset,InitialState : Boolean;const Name : string);
  58. destructor destroy; override;
  59. procedure ResetEvent;
  60. procedure SetEvent;
  61. function WaitFor(Timeout : Cardinal) : TWaitResult;
  62. Property ManualReset : Boolean read FManualReset;
  63. end;
  64. TEvent = TEventObject;
  65. TSimpleEvent = class(TEventObject)
  66. constructor Create;
  67. end;
  68. implementation
  69. Resourcestring
  70. SErrEventCreateFailed = 'Failed to create OS basic event with name "%s"';
  71. { ---------------------------------------------------------------------
  72. Real syncobjs implementation
  73. ---------------------------------------------------------------------}
  74. {$IFDEF OS2}
  75. type
  76. TBasicEventState = record
  77. FHandle: THandle;
  78. FLastError: longint;
  79. end;
  80. PLocalEventRec = ^TBasicEventState;
  81. {$ENDIF OS2}
  82. procedure TSynchroObject.Acquire;
  83. begin
  84. end;
  85. procedure TSynchroObject.Release;
  86. begin
  87. end;
  88. procedure TCriticalSection.Enter;
  89. begin
  90. Acquire;
  91. end;
  92. procedure TCriticalSection.Leave;
  93. begin
  94. Release;
  95. end;
  96. function TCriticalSection.TryEnter:boolean;
  97. begin
  98. result:=TryEnterCriticalSection(CriticalSection)<>0;
  99. end;
  100. procedure TCriticalSection.Acquire;
  101. begin
  102. EnterCriticalSection(CriticalSection);
  103. end;
  104. procedure TCriticalSection.Release;
  105. begin
  106. LeaveCriticalSection(CriticalSection);
  107. end;
  108. constructor TCriticalSection.Create;
  109. begin
  110. Inherited Create;
  111. InitCriticalSection(CriticalSection);
  112. end;
  113. destructor TCriticalSection.Destroy;
  114. begin
  115. DoneCriticalSection(CriticalSection);
  116. end;
  117. destructor THandleObject.destroy;
  118. begin
  119. end;
  120. constructor TEventObject.Create(EventAttributes : PSecurityAttributes;
  121. AManualReset,InitialState : Boolean;const Name : string);
  122. begin
  123. FHandle := BasicEventCreate(EventAttributes, AManualReset, InitialState, Name);
  124. if (FHandle=Nil) then
  125. Raise ESyncObjectException.CreateFmt(SErrEventCreateFailed,[Name]);
  126. FManualReset:=AManualReset;
  127. end;
  128. destructor TEventObject.destroy;
  129. begin
  130. BasicEventDestroy(Handle);
  131. end;
  132. procedure TEventObject.ResetEvent;
  133. begin
  134. BasicEventResetEvent(Handle);
  135. end;
  136. procedure TEventObject.SetEvent;
  137. begin
  138. BasicEventSetEvent(Handle);
  139. end;
  140. function TEventObject.WaitFor(Timeout : Cardinal) : TWaitResult;
  141. begin
  142. Result := TWaitResult(basiceventWaitFor(Timeout, Handle));
  143. if Result = wrError then
  144. {$IFDEF OS2}
  145. FLastError := PLocalEventRec (Handle)^.FLastError;
  146. {$ELSE OS2}
  147. FLastError := GetLastOSError;
  148. {$ENDIF OS2}
  149. end;
  150. constructor TSimpleEvent.Create;
  151. begin
  152. inherited Create(nil, True, False, '');
  153. end;
  154. end.