syncobjs.pp 3.5 KB

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