Panda3D
|
00001 // Filename: psemaphore.h 00002 // Created by: drose (13Oct08) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef PSEMAPHORE_H 00016 #define PSEMAPHORE_H 00017 00018 #include "pandabase.h" 00019 #include "pmutex.h" 00020 #include "conditionVar.h" 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Class : Semaphore 00024 // Description : A classic semaphore synchronization primitive. 00025 // 00026 // A semaphore manages an internal counter which is 00027 // decremented by each acquire() call and incremented by 00028 // each release() call. The counter can never go below 00029 // zero; when acquire() finds that it is zero, it 00030 // blocks, waiting until some other thread calls 00031 // release(). 00032 //////////////////////////////////////////////////////////////////// 00033 class EXPCL_PANDA_PIPELINE Semaphore { 00034 PUBLISHED: 00035 INLINE Semaphore(int initial_count = 1); 00036 INLINE ~Semaphore(); 00037 private: 00038 INLINE Semaphore(const Semaphore ©); 00039 INLINE void operator = (const Semaphore ©); 00040 00041 PUBLISHED: 00042 BLOCKING INLINE void acquire(); 00043 BLOCKING INLINE bool try_acquire(); 00044 INLINE int release(); 00045 00046 INLINE int get_count() const; 00047 void output(ostream &out) const; 00048 00049 private: 00050 Mutex _lock; 00051 ConditionVar _cvar; 00052 int _count; 00053 }; 00054 00055 INLINE ostream & 00056 operator << (ostream &out, const Semaphore &sem) { 00057 sem.output(out); 00058 return out; 00059 } 00060 00061 #include "psemaphore.I" 00062 00063 #endif