Panda3D
 All Classes Functions Variables Enumerations
psemaphore.h
1 // Filename: psemaphore.h
2 // Created by: drose (13Oct08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PSEMAPHORE_H
16 #define PSEMAPHORE_H
17 
18 #include "pandabase.h"
19 #include "pmutex.h"
20 #include "conditionVar.h"
21 #include "mutexHolder.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : Semaphore
25 // Description : A classic semaphore synchronization primitive.
26 //
27 // A semaphore manages an internal counter which is
28 // decremented by each acquire() call and incremented by
29 // each release() call. The counter can never go below
30 // zero; when acquire() finds that it is zero, it
31 // blocks, waiting until some other thread calls
32 // release().
33 ////////////////////////////////////////////////////////////////////
34 class EXPCL_PANDA_PIPELINE Semaphore {
35 PUBLISHED:
36  INLINE Semaphore(int initial_count = 1);
37  INLINE ~Semaphore();
38 private:
39  INLINE Semaphore(const Semaphore &copy);
40  INLINE void operator = (const Semaphore &copy);
41 
42 PUBLISHED:
43  BLOCKING INLINE void acquire();
44  BLOCKING INLINE bool try_acquire();
45  INLINE int release();
46 
47  INLINE int get_count() const;
48  void output(ostream &out) const;
49 
50 private:
51  Mutex _lock;
52  ConditionVar _cvar;
53  int _count;
54 };
55 
56 INLINE ostream &
57 operator << (ostream &out, const Semaphore &sem) {
58  sem.output(out);
59  return out;
60 }
61 
62 #include "psemaphore.I"
63 
64 #endif
A classic semaphore synchronization primitive.
Definition: psemaphore.h:34
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
A condition variable, usually used to communicate information about changing state to a thread that i...
Definition: conditionVar.h:47