summaryrefslogtreecommitdiff
path: root/src/TNetLib/.svn/text-base/Semaphore.cc.svn-base
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
committerJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
commitcccccbf6cca94a3eaf813b4468453160e91c332b (patch)
tree23418cb73a10ae3b0688681a7f0ba9b06424583e /src/TNetLib/.svn/text-base/Semaphore.cc.svn-base
downloadtnet-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-base64
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