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/Mutex.cc | |
download | tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.gz tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.bz2 tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.zip |
First commit
Diffstat (limited to 'src/TNetLib/Mutex.cc')
-rw-r--r-- | src/TNetLib/Mutex.cc | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/TNetLib/Mutex.cc b/src/TNetLib/Mutex.cc new file mode 100644 index 0000000..4ec956a --- /dev/null +++ b/src/TNetLib/Mutex.cc @@ -0,0 +1,48 @@ + +#include <pthread.h> +#include <cerrno> + +#include "Error.h" +#include "Mutex.h" + +namespace TNet { + + +Mutex::Mutex() { + if(0 != pthread_mutex_init(&mutex_,NULL)) + KALDI_ERR << "Cannot initialize mutex"; +} + + +Mutex::~Mutex() { + if(0 != pthread_mutex_destroy(&mutex_)) + KALDI_ERR << "Cannot destroy mutex"; +} + + +void Mutex::Lock() { + if(0 != pthread_mutex_lock(&mutex_)) + KALDI_ERR << "Error on locking mutex"; +} + + +bool Mutex::TryLock() { + int ret = pthread_mutex_lock(&mutex_); + switch (ret) { + case 0: return true; + case EBUSY: return false; + default: KALDI_ERR << "Error on try-locking mutex"; + } + return 0;//make compiler not complain +} + + +void Mutex::Unlock() { + if(0 != pthread_mutex_unlock(&mutex_)) + KALDI_ERR << "Error on unlocking mutex"; +} + + + +}//namespace TNet + |