Panda3D
|
00001 // Filename: psemaphore.I 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 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: Semaphore::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE Semaphore:: 00022 Semaphore(int initial_count) : 00023 _lock("Semaphore::_lock"), 00024 _cvar(_lock), 00025 _count(initial_count) 00026 { 00027 nassertv(_count >= 0); 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: Semaphore::Destructor 00032 // Access: Published 00033 // Description: 00034 //////////////////////////////////////////////////////////////////// 00035 INLINE Semaphore:: 00036 ~Semaphore() { 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: Semaphore::Copy Constructor 00041 // Access: Private 00042 // Description: Do not attempt to copy semaphores. 00043 //////////////////////////////////////////////////////////////////// 00044 INLINE Semaphore:: 00045 Semaphore(const Semaphore ©) : 00046 _cvar(_lock) 00047 { 00048 nassertv(false); 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: Semaphore::Copy Assignment Operator 00053 // Access: Private 00054 // Description: Do not attempt to copy semaphores. 00055 //////////////////////////////////////////////////////////////////// 00056 INLINE void Semaphore:: 00057 operator = (const Semaphore ©) { 00058 nassertv(false); 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: Semaphore::acquire 00063 // Access: Published 00064 // Description: Decrements the internal count. If the count was 00065 // already at zero, blocks until the count is nonzero, 00066 // then decrements it. 00067 //////////////////////////////////////////////////////////////////// 00068 INLINE void Semaphore:: 00069 acquire() { 00070 TAU_PROFILE("void Semaphore::acquire()", " ", TAU_USER); 00071 MutexHolder holder(_lock); 00072 nassertv(_count >= 0); 00073 while (_count <= 0) { 00074 _cvar.wait(); 00075 } 00076 --_count; 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: Semaphore::try_acquire 00081 // Access: Published 00082 // Description: If the semaphore can be acquired without blocking, 00083 // does so and returns true. Otherwise, returns false. 00084 //////////////////////////////////////////////////////////////////// 00085 INLINE bool Semaphore:: 00086 try_acquire() { 00087 TAU_PROFILE("void Semaphore::acquire(bool)", " ", TAU_USER); 00088 MutexHolder holder(_lock); 00089 nassertr(_count >= 0, false); 00090 if (_count <= 0) { 00091 return false; 00092 } 00093 --_count; 00094 return true; 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: Semaphore::release 00099 // Access: Published 00100 // Description: Increments the semaphore's internal count. This may 00101 // wake up another thread blocked on acquire(). 00102 // 00103 // Returns the count of the semaphore upon release. 00104 //////////////////////////////////////////////////////////////////// 00105 INLINE int Semaphore:: 00106 release() { 00107 TAU_PROFILE("void Semaphore::release()", " ", TAU_USER); 00108 MutexHolder holder(_lock); 00109 ++_count; 00110 _cvar.notify(); 00111 return _count; 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: Semaphore::get_count 00116 // Access: Published 00117 // Description: Returns the current semaphore count. Note that this 00118 // call is not thread-safe (the count may change at any 00119 // time). 00120 //////////////////////////////////////////////////////////////////// 00121 INLINE int Semaphore:: 00122 get_count() const { 00123 TAU_PROFILE("void Semaphore::get_count()", " ", TAU_USER); 00124 MutexHolder holder(_lock); 00125 return _count; 00126 }