PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
semaphor.h
Go to the documentation of this file.
1 /*
2  * semaphor.h
3  *
4  * Thread synchronisation semaphore class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_SEMAPHORE_H
31 #define PTLIB_SEMAPHORE_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib/psync.h>
38 #include <limits.h>
39 #include <ptlib/atomic.h>
40 
74 class PSemaphore : public PSync
75 {
76  PCLASSINFO(PSemaphore, PSync);
77 
78  public:
86  unsigned initial = 0,
87  unsigned maximum = UINT_MAX
88  ) { Reset(initial, maximum); }
89 
95  const PString & name,
96  unsigned initial = 0,
97  unsigned maximum = UINT_MAX
98  ) : m_name(name) { Reset(initial, maximum); }
99 
102  PSemaphore(const PSemaphore & sem)
103  : PSync()
104  , m_name(sem.m_name)
105  { Reset(sem.m_initial, sem.m_maximum); }
106 
110  ~PSemaphore();
112 
118  virtual void Wait();
119 
126  virtual PBoolean Wait(
127  const PTimeInterval & timeout // Amount of time to wait for semaphore.
128  );
129 
134  virtual void Signal();
135 
140  virtual void Reset(
141  unsigned initial = 0,
142  unsigned maximum = UINT_MAX
143  );
144 
147  unsigned GetInitial() const { return m_initial; }
148 
151  unsigned GetMaximum() const { return m_maximum; }
153 
154 
155  protected:
157  unsigned m_maximum;
158  unsigned m_initial;
159 
160 
161 // Include platform dependent part of class
162 #ifdef _WIN32
163 #include "msos/ptlib/semaphor.h"
164 #else
165 #include "unix/ptlib/semaphor.h"
166 #endif
167 };
168 
169 
170 #endif // PTLIB_SEMAPHORE_H
171 
172 
173 // End Of File ///////////////////////////////////////////////////////////////
This class defines a thread synchronisation object.
Definition: semaphor.h:74
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
unsigned m_initial
Definition: semaphor.h:158
Definition: psync.h:45
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
virtual void Wait()
If the semaphore count is &gt; 0, decrement the semaphore and return.
~PSemaphore()
Destroy the semaphore.
unsigned GetMaximum() const
Get the initial value semaphore was creted with.
Definition: semaphor.h:151
PString m_name
Definition: semaphor.h:156
bool PBoolean
Definition: object.h:174
PSemaphore(unsigned initial=0, unsigned maximum=UINT_MAX)
Create a new semaphore with maximum count and initial value specified.
Definition: semaphor.h:85
The character string class.
Definition: pstring.h:108
virtual void Reset(unsigned initial=0, unsigned maximum=UINT_MAX)
Reset the semaphore to the specified inital and maximum values.
unsigned GetInitial() const
Get the initial value semaphore was creted with.
Definition: semaphor.h:147
unsigned m_maximum
Definition: semaphor.h:157
PSemaphore(const PSemaphore &sem)
Create a new semaphore with the same initial and maximum values as the original.
Definition: semaphor.h:102
PSemaphore(const PString &name, unsigned initial=0, unsigned maximum=UINT_MAX)
Create a new system global semaphore with maximum count and initial value specified.
Definition: semaphor.h:94