aboutsummaryrefslogtreecommitdiff
path: root/lib/base/elock.h
blob: 7bf25eba3e71482801d1b279608ee69157a3f83c (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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef __elock_h
#define __elock_h

#include <pthread.h>

class singleLock
{
	pthread_mutex_t &lock;
public:
	singleLock(pthread_mutex_t &m )
		:lock(m)
	{
		pthread_mutex_lock(&lock);
	}
	~singleLock()
	{
		pthread_mutex_unlock(&lock);
	}
};

class eSingleLock
{
	friend class eSingleLocker;
	pthread_mutex_t m_lock;
	eSingleLock(eSingleLock &);
public:
	eSingleLock()
	{
		pthread_mutex_init(&m_lock, 0);
	}
	~eSingleLock()
	{
		pthread_mutex_destroy(&m_lock);
	}
};

class eSingleLocker
{
	eSingleLock &m_lock;
public:
	eSingleLocker(eSingleLock &m)
		: m_lock(m)
	{
		pthread_mutex_lock(&m_lock.m_lock);
	}
	~eSingleLocker()
	{
		pthread_mutex_unlock(&m_lock.m_lock);
	}
};

class eLock
{
	pthread_mutex_t mutex;
	pthread_cond_t cond;

	int pid;
	int counter, max;
public:
	void lock(int res=100);
	void unlock(int res=100);

	eLock(int max=100);
	~eLock();
};

class eLocker
{
	eLock &lock;
	int res;
public:
	eLocker(eLock &lock, int res=100);
	~eLocker();
};

class eSemaphore
{
	int v;
	pthread_mutex_t mutex;
	pthread_cond_t cond;
public:
	eSemaphore();
	~eSemaphore();
	
	int down();
	int decrement();
	int up();
	int value();
};

#endif