diff options
author | Joe Zhao <ztuowen@gmail.com> | 2014-04-14 08:14:45 +0800 |
---|---|---|
committer | Joe Zhao <ztuowen@gmail.com> | 2014-04-14 08:14:45 +0800 |
commit | cccccbf6cca94a3eaf813b4468453160e91c332b (patch) | |
tree | 23418cb73a10ae3b0688681a7f0ba9b06424583e /src/TNetLib/.svn/text-base/Semaphore.cc.svn-base | |
download | tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.gz tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.bz2 tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.zip |
First commit
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 |