PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
psync.h
Go to the documentation of this file.
1 /*
2  * psync.h
3  *
4  * Abstract synchronisation semaphore class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  * Copyright (c) 2005 Post Increment
10  *
11  * The contents of this file are subject to the Mozilla Public License
12  * Version 1.0 (the "License"); you may not use this file except in
13  * compliance with the License. You may obtain a copy of the License at
14  * http://www.mozilla.org/MPL/
15  *
16  * Software distributed under the License is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18  * the License for the specific language governing rights and limitations
19  * under the License.
20  *
21  * The Original Code is Portable Windows Library.
22  *
23  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24  *
25  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
26  * All Rights Reserved.
27  *
28  * Contributor(s): ______________________________________.
29  */
30 
31 #ifndef PTLIB_SYNC_H
32 #define PTLIB_SYNC_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib/contain.h>
39 #include <ptlib/object.h>
40 
41 
42 class PTimeInterval;
43 
44 
45 class PSync : public PObject
46 {
47  public:
48  PSync() { }
49 
54  virtual void Wait() = 0;
55 
61  virtual PBoolean Wait(
62  const PTimeInterval & timeout // Amount of time to wait.
63  ) = 0;
64 
66  virtual bool InstrumentedWait(const PTimeInterval & timeout, const PDebugLocation & /*location*/) { return Wait(timeout); }
67 
70  virtual void Signal() = 0;
71 
73  virtual void InstrumentedSignal(const PDebugLocation & /*location*/) { Signal(); }
75 
76  private:
77  PSync(const PSync &) : PObject() { }
78  void operator=(const PSync &) { }
79 };
80 
81 
83 class PSyncNULL : public PSync
84 {
85  public:
86  PSyncNULL() { }
87  virtual void Wait() { }
88  virtual PBoolean Wait(const PTimeInterval &) { return true; }
89  virtual void Signal() { }
90 
91  private:
92  PSyncNULL(const PSyncNULL &) : PSync() { }
93  void operator=(const PSyncNULL &) { }
94 };
95 
96 
116 {
117  public:
122  __inline explicit PWaitAndSignal(
123  const PSync & sem
124  ) : sync(const_cast<PSync &>(sem))
125  {
126  sync.Wait();
127  }
128 
129  __inline PWaitAndSignal(
130  const PSync & sem,
131  bool wait
132  ) : sync(const_cast<PSync &>(sem))
133  {
134  if (wait)
135  sync.Wait();
136  }
137 
142  __inline ~PWaitAndSignal()
143  {
144  sync.Signal();
145  }
146 
147  protected:
149 };
150 
151 #if PTRACING
152 class PInstrumentedWaitAndSignal
153 {
154  public:
159  __inline explicit PInstrumentedWaitAndSignal(
160  const PDebugLocation & location,
161  const PSync & sem
162  ) : m_location(location)
163  , sync(const_cast<PSync &>(sem))
164  {
165  sync.InstrumentedWait(PMaxTimeInterval, m_location);
166  }
167 
168  __inline PInstrumentedWaitAndSignal(
169  const PDebugLocation & location,
170  const PSync & sem,
171  bool wait
172  ) : m_location(location)
173  , sync(const_cast<PSync &>(sem))
174  {
175  if (wait)
176  sync.InstrumentedWait(PMaxTimeInterval, m_location);
177  }
178 
183  __inline ~PInstrumentedWaitAndSignal()
184  {
185  sync.InstrumentedSignal(m_location);
186  }
187 
188  protected:
189  PDebugLocation const m_location;
190  PSync & sync;
191 };
192 
193 #define P_INSTRUMENTED_WAIT_AND_SIGNAL2(var, mutex) PInstrumentedWaitAndSignal var(P_DEBUG_LOCATION, mutex)
194 #else // P_TRACING
195 #define P_INSTRUMENTED_WAIT_AND_SIGNAL2(var, mutex) PWaitAndSignal var(mutex)
196 #endif // P_TRACING
197 #define P_INSTRUMENTED_WAIT_AND_SIGNAL(mutex) P_INSTRUMENTED_WAIT_AND_SIGNAL2(lock##__LINE__,mutex)
198 
199 
200 #endif // PTLIB_SYNC_H
201 
202 
203 // End Of File ///////////////////////////////////////////////////////////////
virtual PBoolean Wait(const PTimeInterval &)
Block, for a time, until the synchronisation object is available.
Definition: psync.h:88
Information about a source file location.
Definition: object.h:333
#define PMaxTimeInterval
Definition: timeint.h:31
PSyncNULL()
Definition: psync.h:86
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:115
virtual void Wait()=0
Block until the synchronisation object is available.
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
virtual void Wait()
Block until the synchronisation object is available.
Definition: psync.h:87
virtual void Signal()
Signal that the synchronisation object is available.
Definition: psync.h:89
Definition: psync.h:45
PSync()
Definition: psync.h:48
Synchronisation without really synchronising.
Definition: psync.h:83
__inline PWaitAndSignal(const PSync &sem, bool wait)
Definition: psync.h:129
bool PBoolean
Definition: object.h:174
virtual void Signal()=0
Signal that the synchronisation object is available.
__inline ~PWaitAndSignal()
Signal the semaphore.
Definition: psync.h:142
__inline PWaitAndSignal(const PSync &sem)
Create the semaphore wait instance.
Definition: psync.h:122
virtual bool InstrumentedWait(const PTimeInterval &timeout, const PDebugLocation &)
As for Wait() but with location of call for instrumentation. Mostly used internally.
Definition: psync.h:66
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
virtual void InstrumentedSignal(const PDebugLocation &)
As for Signal() but with location of call for instrumentation. Mostly used internally.
Definition: psync.h:73
PSync & sync
Definition: psync.h:148