Panda3D
psemaphore.I
1 // Filename: psemaphore.I
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: Semaphore::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE Semaphore::
22 Semaphore(int initial_count) :
23  _lock("Semaphore::_lock"),
24  _cvar(_lock),
25  _count(initial_count)
26 {
27  nassertv(_count >= 0);
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: Semaphore::Destructor
32 // Access: Published
33 // Description:
34 ////////////////////////////////////////////////////////////////////
35 INLINE Semaphore::
36 ~Semaphore() {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: Semaphore::Copy Constructor
41 // Access: Private
42 // Description: Do not attempt to copy semaphores.
43 ////////////////////////////////////////////////////////////////////
44 INLINE Semaphore::
45 Semaphore(const Semaphore &copy) :
46  _cvar(_lock)
47 {
48  nassertv(false);
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: Semaphore::Copy Assignment Operator
53 // Access: Private
54 // Description: Do not attempt to copy semaphores.
55 ////////////////////////////////////////////////////////////////////
56 INLINE void Semaphore::
57 operator = (const Semaphore &copy) {
58  nassertv(false);
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: Semaphore::acquire
63 // Access: Published
64 // Description: Decrements the internal count. If the count was
65 // already at zero, blocks until the count is nonzero,
66 // then decrements it.
67 ////////////////////////////////////////////////////////////////////
68 INLINE void Semaphore::
70  TAU_PROFILE("void Semaphore::acquire()", " ", TAU_USER);
71  MutexHolder holder(_lock);
72  nassertv(_count >= 0);
73  while (_count <= 0) {
74  _cvar.wait();
75  }
76  --_count;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: Semaphore::try_acquire
81 // Access: Published
82 // Description: If the semaphore can be acquired without blocking,
83 // does so and returns true. Otherwise, returns false.
84 ////////////////////////////////////////////////////////////////////
85 INLINE bool Semaphore::
87  TAU_PROFILE("void Semaphore::acquire(bool)", " ", TAU_USER);
88  MutexHolder holder(_lock);
89  nassertr(_count >= 0, false);
90  if (_count <= 0) {
91  return false;
92  }
93  --_count;
94  return true;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: Semaphore::release
99 // Access: Published
100 // Description: Increments the semaphore's internal count. This may
101 // wake up another thread blocked on acquire().
102 //
103 // Returns the count of the semaphore upon release.
104 ////////////////////////////////////////////////////////////////////
105 INLINE int Semaphore::
107  TAU_PROFILE("void Semaphore::release()", " ", TAU_USER);
108  MutexHolder holder(_lock);
109  ++_count;
110  _cvar.notify();
111  return _count;
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: Semaphore::get_count
116 // Access: Published
117 // Description: Returns the current semaphore count. Note that this
118 // call is not thread-safe (the count may change at any
119 // time).
120 ////////////////////////////////////////////////////////////////////
121 INLINE int Semaphore::
122 get_count() const {
123  TAU_PROFILE("void Semaphore::get_count()", " ", TAU_USER);
124  MutexHolder holder(_lock);
125  return _count;
126 }
int release()
Increments the semaphore&#39;s internal count.
Definition: psemaphore.I:106
A classic semaphore synchronization primitive.
Definition: psemaphore.h:34
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition: mutexHolder.h:29
void acquire()
Decrements the internal count.
Definition: psemaphore.I:69
int get_count() const
Returns the current semaphore count.
Definition: psemaphore.I:122
bool try_acquire()
If the semaphore can be acquired without blocking, does so and returns true.
Definition: psemaphore.I:86
void wait()
Waits on the condition.
void notify()
Informs one of the other threads who are currently blocked on wait() that the relevant condition has ...