Panda3D

Loading...
Searching...
No Matches
psemaphore.I
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file psemaphore.I
10 * @author drose
11 * @date 2008-10-13
12 */
13
14/**
15 *
16 */
17INLINE Semaphore::
18Semaphore(int initial_count) :
19 _lock("Semaphore::_lock"),
20 _cvar(_lock),
21 _count(initial_count)
22{
23 nassertv(_count >= 0);
24}
25
26/**
27 * Decrements the internal count. If the count was already at zero, blocks
28 * until the count is nonzero, then decrements it.
29 */
30INLINE void Semaphore::
31 acquire() {
32 TAU_PROFILE("void Semaphore::acquire()", " ", TAU_USER);
33 MutexHolder holder(_lock);
34 nassertv(_count >= 0);
35 while (_count <= 0) {
36 _cvar.wait();
37 }
38 --_count;
39}
40
41/**
42 * If the semaphore can be acquired without blocking, does so and returns
43 * true. Otherwise, returns false.
44 */
45INLINE bool Semaphore::
46 try_acquire() {
47 TAU_PROFILE("void Semaphore::acquire(bool)", " ", TAU_USER);
48 MutexHolder holder(_lock);
49 nassertr(_count >= 0, false);
50 if (_count <= 0) {
51 return false;
52 }
53 --_count;
54 return true;
55}
56
57/**
58 * Increments the semaphore's internal count. This may wake up another thread
59 * blocked on acquire().
60 *
61 * Returns the count of the semaphore upon release.
62 */
63INLINE int Semaphore::
64 release() {
65 TAU_PROFILE("void Semaphore::release()", " ", TAU_USER);
66 MutexHolder holder(_lock);
67 ++_count;
68 _cvar.notify();
69 return _count;
70}
71
72/**
73 * Returns the current semaphore count. Note that this call is not thread-
74 * safe (the count may change at any time).
75 */
76INLINE int Semaphore::
77 get_count() const {
78 TAU_PROFILE("void Semaphore::get_count()", " ", TAU_USER);
79 MutexHolder holder(_lock);
80 return _count;
81}
MutexHolder
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition mutexHolder.h:25
Semaphore::try_acquire
bool try_acquire()
If the semaphore can be acquired without blocking, does so and returns true.
Definition psemaphore.I:46
Semaphore::release
int release()
Increments the semaphore's internal count.
Definition psemaphore.I:64
Semaphore::get_count
int get_count() const
Returns the current semaphore count.
Definition psemaphore.I:77
Semaphore::acquire
void acquire()
Decrements the internal count.
Definition psemaphore.I:31