Panda3D
psemaphore.h
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.h
10  * @author drose
11  * @date 2008-10-13
12  */
13 
14 #ifndef PSEMAPHORE_H
15 #define PSEMAPHORE_H
16 
17 #include "pandabase.h"
18 #include "pmutex.h"
19 #include "conditionVar.h"
20 #include "mutexHolder.h"
21 
22 /**
23  * A classic semaphore synchronization primitive.
24  *
25  * A semaphore manages an internal counter which is decremented by each
26  * acquire() call and incremented by each release() call. The counter can
27  * never go below zero; when acquire() finds that it is zero, it blocks,
28  * waiting until some other thread calls release().
29  */
30 class EXPCL_PANDA_PIPELINE Semaphore {
31 PUBLISHED:
32  INLINE explicit Semaphore(int initial_count = 1);
33  Semaphore(const Semaphore &copy) = delete;
34  ~Semaphore() = default;
35 
36  Semaphore &operator = (const Semaphore &copy) = delete;
37 
38 PUBLISHED:
39  BLOCKING INLINE void acquire();
40  BLOCKING INLINE bool try_acquire();
41  INLINE int release();
42 
43  INLINE int get_count() const;
44  void output(std::ostream &out) const;
45 
46 private:
47  Mutex _lock;
48  ConditionVar _cvar;
49  int _count;
50 };
51 
52 INLINE std::ostream &
53 operator << (std::ostream &out, const Semaphore &sem) {
54  sem.output(out);
55  return out;
56 }
57 
58 #include "psemaphore.I"
59 
60 #endif
A condition variable, usually used to communicate information about changing state to a thread that i...
Definition: conditionVar.h:43
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:40
A classic semaphore synchronization primitive.
Definition: psemaphore.h:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.