blob: 4ec956af267fba4a9220a6d5a0919891056bfaad (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
 |