syncobjs.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. pthreads,
  17. {$ifdef ver1_0}
  18. Linux,
  19. {$else}
  20. unix,
  21. {$endif}
  22. sysutils;
  23. type
  24. PSecurityAttributes = Pointer;
  25. TEventHandle = THandle;
  26. TRTLCriticalSection = TPthreadMutex;
  27. {$I syncobh.inc}
  28. implementation
  29. { ---------------------------------------------------------------------
  30. Some wrappers around PThreads.
  31. ---------------------------------------------------------------------}
  32. function InitializeCriticalSection(var lpCriticalSection: TRTLCriticalSection): Integer;
  33. var
  34. MAttr : TMutexAttribute;
  35. begin
  36. Result:=pthread_mutexattr_init(@MAttr);
  37. if Result=0 then
  38. try
  39. Result:=pthread_mutexattr_settype(@MAttr, PTHREAD_MUTEX_RECURSIVE);
  40. if Result=0 then
  41. Result:=pthread_mutex_init(@lpCriticalSection,@MAttr);
  42. finally
  43. pthread_mutexattr_destroy(@MAttr);
  44. end;
  45. end;
  46. function EnterCriticalSection(var lpCriticalSection: TRTLCriticalSection) : Integer;
  47. begin
  48. Result:=pthread_mutex_lock(@lpCriticalSection);
  49. end;
  50. function LeaveCriticalSection (var lpCriticalSection: TRTLCriticalSection) : Integer;
  51. begin
  52. Result:=pthread_mutex_unlock(@lpCriticalSection);
  53. end;
  54. function DeleteCriticalSection(var lpCriticalSection: TRTLCriticalSection) : Integer;
  55. begin
  56. Result:=pthread_mutex_destroy(@lpCriticalSection);
  57. end;
  58. { ---------------------------------------------------------------------
  59. Real syncobjs implementation
  60. ---------------------------------------------------------------------}
  61. {$I syncob.inc}
  62. procedure TCriticalSection.Acquire;
  63. begin
  64. EnterCriticalSection(CriticalSection);
  65. end;
  66. procedure TCriticalSection.Release;
  67. begin
  68. LeaveCriticalSection(CriticalSection);
  69. end;
  70. constructor TCriticalSection.Create;
  71. begin
  72. Inherited Create;
  73. InitializeCriticalSection(CriticalSection);
  74. end;
  75. destructor TCriticalSection.Destroy;
  76. begin
  77. DeleteCriticalSection(CriticalSection);
  78. end;
  79. destructor THandleObject.destroy;
  80. begin
  81. end;
  82. constructor TEventObject.Create(EventAttributes : PSecurityAttributes;
  83. AManualReset,InitialState : Boolean;const Name : string);
  84. begin
  85. FManualReset:=AManualReset;
  86. FSem:=New(PSemaphore);
  87. FEventSection:=TCriticalSection.Create;
  88. sem_init(FSem,ord(False),Ord(InitialState));
  89. end;
  90. destructor TEventObject.destroy;
  91. begin
  92. sem_destroy(FSem);
  93. end;
  94. procedure TEventObject.ResetEvent;
  95. begin
  96. While sem_trywait(FSem)=0 do
  97. ;
  98. end;
  99. procedure TEventObject.SetEvent;
  100. Var
  101. Value : Longint;
  102. begin
  103. FEventSection.Enter;
  104. Try
  105. sem_getvalue(FSem,@Value);
  106. if Value=0 then
  107. sem_post(FSem);
  108. finally
  109. FEventSection.Leave;
  110. end;
  111. end;
  112. function TEventObject.WaitFor(Timeout : Cardinal) : TWaitResult;
  113. begin
  114. If TimeOut<>Cardinal($FFFFFFFF) then
  115. result:=wrError
  116. else
  117. begin
  118. sem_wait(FSem);
  119. result:=wrSignaled;
  120. if FManualReset then
  121. begin
  122. FEventSection.Enter;
  123. Try
  124. resetevent;
  125. sem_post(FSem);
  126. Finally
  127. FEventSection.Leave;
  128. end;
  129. end;
  130. end;
  131. end;
  132. constructor TSimpleEvent.Create;
  133. begin
  134. inherited Create(nil, True, False, '');
  135. end;
  136. end.