syncobjs.pp 3.7 KB

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