-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.cpp
More file actions
85 lines (74 loc) · 1.2 KB
/
control.cpp
File metadata and controls
85 lines (74 loc) · 1.2 KB
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
/* control.cpp -- General thread lock control class */
#include "control.h"
int
thread_ctl::init()
{
if (pthread_mutex_init(&mutex,NULL))
return 1;
if (pthread_cond_init(&cond,NULL))
return 2;
active=0;
return 0;
}
int
thread_ctl::destroy()
{
if (pthread_cond_destroy(&cond))
return 1;
if (pthread_cond_destroy(&cond))
return 2;
active=0;
return 0;
}
int
thread_ctl::activate()
{
if (pthread_mutex_lock(&mutex))
return 0;
active=1;
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
return 1;
}
int
thread_ctl::deactivate()
{
if (pthread_mutex_lock(&mutex))
return 0;
active=0;
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
return 1;
}
int
thread_ctl::lock()
{
return pthread_mutex_lock(&mutex);
}
int
thread_ctl::unlock()
{
return pthread_mutex_unlock(&mutex);
}
int
thread_ctl::cond_wait()
{
return pthread_cond_wait(&cond, &mutex);
}
int
thread_ctl::cond_signal()
{
return pthread_cond_signal(&cond);
}
int
thread_ctl::cond_broadcast()
{
return pthread_cond_broadcast(&cond);
}
/* Don't lock the mutex and cond. Use this only if you are assure that
you have got the lock. */
int
thread_ctl::isactive()
{
return active;
}