-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocks.h
73 lines (55 loc) · 1.13 KB
/
locks.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* locks.h
*
* Created on: Apr 24, 2011
* Author: changokkim
*/
#ifndef LOCKS_H_
#define LOCKS_H_
#include <iostream>
#include <pthread.h>
using std::cout;
using std::endl;
namespace baseLock {
class Spinlock {
public:
Spinlock();
~Spinlock();
void lock();
void unlock();
private:
int locked_;
// Non-copyable, non-assignable
Spinlock(Spinlock&);
Spinlock& operator=(Spinlock&);
};
class Mutex {
public:
Mutex() {
//cout << "hello" << endl;
pthread_mutex_init(&m_, NULL); }
~Mutex() { pthread_mutex_destroy(&m_); }
void lock() { pthread_mutex_lock(&m_); }
void unlock() { pthread_mutex_unlock(&m_); }
private:
pthread_mutex_t m_;
// Non-copyable, non-assignable
Mutex(Mutex &);
Mutex& operator=(Mutex&);
};
class ScopedLock {
public:
explicit ScopedLock(Mutex* lock) : m_(lock) {
//cout << "here" << endl;
m_->lock(); }
~ScopedLock() {
//cout << "there" << endl;
m_->unlock(); }
private:
Mutex* m_;
// Non-copyable, non-assignable
ScopedLock(ScopedLock&);
ScopedLock& operator=(ScopedLock&);
};
} // namespace base
#endif /* LOCKS_H_ */