diff options
Diffstat (limited to 'src/TNetLib/.svn/text-base/Semaphore.cc.svn-base')
-rw-r--r-- | src/TNetLib/.svn/text-base/Semaphore.cc.svn-base | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/TNetLib/.svn/text-base/Semaphore.cc.svn-base b/src/TNetLib/.svn/text-base/Semaphore.cc.svn-base new file mode 100644 index 0000000..d149fb3 --- /dev/null +++ b/src/TNetLib/.svn/text-base/Semaphore.cc.svn-base @@ -0,0 +1,64 @@ + +#include "Semaphore.h" + +namespace TNet { + + Semaphore:: + Semaphore(int initValue) + { + mSemValue = initValue; + pthread_mutex_init(&mMutex, NULL); + pthread_cond_init(&mCond, NULL); + } + + Semaphore:: + ~Semaphore() + { + pthread_mutex_destroy(&mMutex); + pthread_cond_destroy(&mCond); + } + + int + Semaphore:: + TryWait() + { + pthread_mutex_lock(&mMutex); + if(mSemValue > 0) { + mSemValue--; + pthread_mutex_unlock(&mMutex); + return 0; + } + pthread_mutex_unlock(&mMutex); + return -1; + } + + void + Semaphore:: + Wait() + { + pthread_mutex_lock(&mMutex); + while(mSemValue <= 0) { + pthread_cond_wait(&mCond, &mMutex); + } + mSemValue--; + pthread_mutex_unlock(&mMutex); + } + + void + Semaphore:: + Post() + { + pthread_mutex_lock(&mMutex); + mSemValue++; + pthread_cond_signal(&mCond); + pthread_mutex_unlock(&mMutex); + } + + int + Semaphore:: + GetValue() + { return mSemValue; } + + + +} //namespace |