syncobjs.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. libc,
  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. end;
  90. procedure TEventObject.ResetEvent;
  91. begin
  92. While sem_trywait(FSem)=0 do
  93. ;
  94. end;
  95. procedure TEventObject.SetEvent;
  96. Var
  97. Value : Longint;
  98. begin
  99. FEventSection.Enter;
  100. Try
  101. sem_getvalue(FSem,@Value);
  102. if Value=0 then
  103. sem_post(FSem);
  104. finally
  105. FEventSection.Leave;
  106. end;
  107. end;
  108. function TEventObject.WaitFor(Timeout : Cardinal) : TWaitResult;
  109. begin
  110. If TimeOut<>Cardinal($FFFFFFFF) then
  111. result:=wrError
  112. else
  113. begin
  114. sem_wait(FSem);
  115. result:=wrSignaled;
  116. if FManualReset then
  117. begin
  118. FEventSection.Enter;
  119. Try
  120. resetevent;
  121. sem_post(FSem);
  122. Finally
  123. FEventSection.Leave;
  124. end;
  125. end;
  126. end;
  127. end;
  128. constructor TSimpleEvent.Create;
  129. begin
  130. inherited Create(nil, True, False, '');
  131. end;
  132. end.
  133. {
  134. $Log$
  135. Revision 1.1 2004-12-07 14:13:42 armin
  136. * added syncobj for netwlibc
  137. Revision 1.2 2002/08/17 02:23:35 michael
  138. + Fixed 1.1 build of syncobjs
  139. Revision 1.1 2003/06/14 19:14:31 michael
  140. + Initial implementation
  141. }