syncobjs.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1998 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$h+}
  14. unit syncobjs;
  15. interface
  16. uses
  17. pthreads,
  18. {$ifdef ver1_0}
  19. Linux,
  20. {$else}
  21. unix,
  22. {$endif}
  23. sysutils;
  24. type
  25. PSecurityAttributes = Pointer;
  26. TEventHandle = THandle;
  27. TRTLCriticalSection = TPthreadMutex;
  28. {$I syncobh.inc}
  29. implementation
  30. { ---------------------------------------------------------------------
  31. Some wrappers around PThreads.
  32. ---------------------------------------------------------------------}
  33. function InitializeCriticalSection(var lpCriticalSection: TRTLCriticalSection): Integer;
  34. var
  35. MAttr : TMutexAttribute;
  36. begin
  37. Result:=pthread_mutexattr_init(@MAttr);
  38. if Result=0 then
  39. try
  40. Result:=pthread_mutexattr_settype(@MAttr, PTHREAD_MUTEX_RECURSIVE);
  41. if Result=0 then
  42. Result:=pthread_mutex_init(@lpCriticalSection,@MAttr);
  43. finally
  44. pthread_mutexattr_destroy(@MAttr);
  45. end;
  46. end;
  47. function EnterCriticalSection(var lpCriticalSection: TRTLCriticalSection) : Integer;
  48. begin
  49. Result:=pthread_mutex_lock(@lpCriticalSection);
  50. end;
  51. function LeaveCriticalSection (var lpCriticalSection: TRTLCriticalSection) : Integer;
  52. begin
  53. Result:=pthread_mutex_unlock(@lpCriticalSection);
  54. end;
  55. function DeleteCriticalSection(var lpCriticalSection: TRTLCriticalSection) : Integer;
  56. begin
  57. Result:=pthread_mutex_destroy(@lpCriticalSection);
  58. end;
  59. { ---------------------------------------------------------------------
  60. Real syncobjs implementation
  61. ---------------------------------------------------------------------}
  62. {$I syncob.inc}
  63. procedure TCriticalSection.Acquire;
  64. begin
  65. EnterCriticalSection(CriticalSection);
  66. end;
  67. procedure TCriticalSection.Release;
  68. begin
  69. LeaveCriticalSection(CriticalSection);
  70. end;
  71. constructor TCriticalSection.Create;
  72. begin
  73. Inherited Create;
  74. InitializeCriticalSection(CriticalSection);
  75. end;
  76. destructor TCriticalSection.Destroy;
  77. begin
  78. DeleteCriticalSection(CriticalSection);
  79. end;
  80. destructor THandleObject.destroy;
  81. begin
  82. end;
  83. constructor TEventObject.Create(EventAttributes : PSecurityAttributes;
  84. AManualReset,InitialState : Boolean;const Name : string);
  85. begin
  86. FManualReset:=AManualReset;
  87. FSem:=New(PSemaphore);
  88. FEventSection:=TCriticalSection.Create;
  89. sem_init(FSem,ord(False),Ord(InitialState));
  90. end;
  91. destructor TEventObject.destroy;
  92. begin
  93. sem_destroy(FSem);
  94. end;
  95. procedure TEventObject.ResetEvent;
  96. begin
  97. While sem_trywait(FSem)=0 do
  98. ;
  99. end;
  100. procedure TEventObject.SetEvent;
  101. Var
  102. Value : Longint;
  103. begin
  104. FEventSection.Enter;
  105. Try
  106. sem_getvalue(FSem,@Value);
  107. if Value=0 then
  108. sem_post(FSem);
  109. finally
  110. FEventSection.Leave;
  111. end;
  112. end;
  113. function TEventObject.WaitFor(Timeout : Cardinal) : TWaitResult;
  114. begin
  115. If TimeOut<>Cardinal($FFFFFFFF) then
  116. result:=wrError
  117. else
  118. begin
  119. sem_wait(FSem);
  120. result:=wrSignaled;
  121. if FManualReset then
  122. begin
  123. FEventSection.Enter;
  124. Try
  125. resetevent;
  126. sem_post(FSem);
  127. Finally
  128. FEventSection.Leave;
  129. end;
  130. end;
  131. end;
  132. end;
  133. constructor TSimpleEvent.Create;
  134. begin
  135. inherited Create(nil, True, False, '');
  136. end;
  137. end.
  138. {
  139. $Log$
  140. Revision 1.2 2002-08-17 02:23:35 michael
  141. + Fixed 1.1 build of syncobjs
  142. Revision 1.1 2003/06/14 19:14:31 michael
  143. + Initial implementation
  144. }