syncobjs.pp 3.5 KB

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