syncobjs.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. end;
  89. procedure TEventObject.ResetEvent;
  90. begin
  91. While sem_trywait(FSem)=0 do
  92. ;
  93. end;
  94. procedure TEventObject.SetEvent;
  95. Var
  96. Value : Longint;
  97. begin
  98. FEventSection.Enter;
  99. Try
  100. sem_getvalue(FSem,@Value);
  101. if Value=0 then
  102. sem_post(FSem);
  103. finally
  104. FEventSection.Leave;
  105. end;
  106. end;
  107. function TEventObject.WaitFor(Timeout : Cardinal) : TWaitResult;
  108. begin
  109. If TimeOut<>Cardinal($FFFFFFFF) then
  110. result:=wrError
  111. else
  112. begin
  113. sem_wait(FSem);
  114. result:=wrSignaled;
  115. if FManualReset then
  116. begin
  117. FEventSection.Enter;
  118. Try
  119. resetevent;
  120. sem_post(FSem);
  121. Finally
  122. FEventSection.Leave;
  123. end;
  124. end;
  125. end;
  126. end;
  127. constructor TSimpleEvent.Create;
  128. begin
  129. inherited Create(nil, True, False, '');
  130. end;
  131. end.