My Project
Loading...
Searching...
No Matches
Singular
dyn_modules
systhreads
thread.h
Go to the documentation of this file.
1
#ifndef _THREAD_H
2
#define _THREAD_H
3
4
#include <limits.h>
5
#include <pthread.h>
6
#include <stdint.h>
7
#include <stdlib.h>
8
#include <cstddef>
9
#include <exception>
10
11
typedef
pthread_t
Thread
;
12
13
void
ThreadError
(
const
char
*
message
);
14
15
class
ConditionVariable
;
16
17
class
Lock
{
18
private
:
19
pthread_mutex_t
mutex
;
20
friend
class
ConditionVariable
;
21
Thread
owner
;
22
int
locked
;
23
bool
recursive
;
24
void
resume_lock
(
int
l
) {
25
owner
= pthread_self();
26
locked
=
l
;
27
}
28
int
break_lock
() {
29
extern
pthread_t
no_thread
;
30
int
result
=
locked
;
31
owner
=
no_thread
;
32
locked
= 0;
33
return
result
;
34
}
35
public
:
36
Lock
(
bool
rec =
false
) {
37
extern
pthread_t
no_thread
;
38
pthread_mutex_init(&
mutex
,
NULL
);
39
locked
= 0;
40
recursive
= rec;
41
owner
=
no_thread
;
42
}
43
~Lock
() {
44
pthread_mutex_destroy(&
mutex
);
45
}
46
void
lock
() {
47
Thread
self = pthread_self();
48
if
(
owner
== self) {
49
if
(
locked
&& !
recursive
)
50
ThreadError
(
"locking mutex twice"
);
51
}
52
else
53
pthread_mutex_lock(&
mutex
);
54
owner
= self;
55
locked
++;
56
}
57
void
unlock
() {
58
extern
pthread_t
no_thread
;
59
Thread
self = pthread_self();
60
if
(
owner
!= self)
61
ThreadError
(
"unlocking unowned lock"
);
62
locked
--;
63
if
(
locked
== 0) {
64
owner
=
no_thread
;
65
pthread_mutex_unlock(&
mutex
);
66
}
67
}
68
bool
is_locked
() {
69
return
locked
!= 0 &&
owner
== pthread_self();
70
}
71
};
72
73
class
ConditionVariable
{
74
friend
class
Lock
;
75
private
:
76
pthread_cond_t
condition
;
77
Lock
*
lock
;
78
int
waiting
;
79
friend
class
Semaphore
;
80
ConditionVariable
() { }
81
public
:
82
ConditionVariable
(
Lock
*lock_init) :
waiting
(0),
lock
(lock_init) {
83
pthread_cond_init(&
condition
,
NULL
);
84
}
85
~ConditionVariable
() {
86
pthread_cond_destroy(&
condition
);
87
}
88
void
wait
() {
89
if
(!
lock
->
is_locked
())
90
ThreadError
(
"waited on condition without locked mutex"
);
91
waiting
++;
92
int
l
=
lock
->
break_lock
();
93
pthread_cond_wait(&
condition
, &
lock
->
mutex
);
94
waiting
--;
95
lock
->
resume_lock
(
l
);
96
}
97
void
signal
() {
98
if
(!
lock
->
is_locked
())
99
ThreadError
(
"signaled condition without locked mutex"
);
100
if
(
waiting
)
101
pthread_cond_signal(&
condition
);
102
}
103
void
broadcast
() {
104
if
(!
lock
->
is_locked
())
105
ThreadError
(
"signaled condition without locked mutex"
);
106
if
(
waiting
)
107
pthread_cond_broadcast(&
condition
);
108
}
109
};
110
111
class
Semaphore
{
112
private
:
113
Lock
lock
;
114
ConditionVariable
cond
;
115
unsigned
count
;
116
unsigned
waiting
;
117
public
:
118
Semaphore
() :
lock
(),
cond
(&
lock
),
count
(0),
waiting
(0) {
119
}
120
Semaphore
(
unsigned
count0) :
lock
(),
cond
(&
lock
),
count
(count0),
waiting
(0) {
121
}
122
void
wait
();
123
void
post
();
124
};
125
126
#endif
// _THREAD_H
l
int l
Definition:
cfEzgcd.cc:100
ConditionVariable
Definition:
thread.h:73
ConditionVariable::waiting
int waiting
Definition:
thread.h:78
ConditionVariable::ConditionVariable
ConditionVariable()
Definition:
thread.h:80
ConditionVariable::wait
void wait()
Definition:
thread.h:88
ConditionVariable::ConditionVariable
ConditionVariable(Lock *lock_init)
Definition:
thread.h:82
ConditionVariable::~ConditionVariable
~ConditionVariable()
Definition:
thread.h:85
ConditionVariable::condition
pthread_cond_t condition
Definition:
thread.h:76
ConditionVariable::broadcast
void broadcast()
Definition:
thread.h:103
ConditionVariable::signal
void signal()
Definition:
thread.h:97
ConditionVariable::lock
Lock * lock
Definition:
thread.h:77
Lock
Definition:
thread.h:17
Lock::is_locked
bool is_locked()
Definition:
thread.h:68
Lock::break_lock
int break_lock()
Definition:
thread.h:28
Lock::owner
Thread owner
Definition:
thread.h:21
Lock::~Lock
~Lock()
Definition:
thread.h:43
Lock::lock
void lock()
Definition:
thread.h:46
Lock::unlock
void unlock()
Definition:
thread.h:57
Lock::Lock
Lock(bool rec=false)
Definition:
thread.h:36
Lock::mutex
pthread_mutex_t mutex
Definition:
thread.h:19
Lock::locked
int locked
Definition:
thread.h:22
Lock::recursive
bool recursive
Definition:
thread.h:23
Lock::resume_lock
void resume_lock(int l)
Definition:
thread.h:24
Semaphore
Definition:
thread.h:111
Semaphore::cond
ConditionVariable cond
Definition:
thread.h:114
Semaphore::lock
Lock lock
Definition:
thread.h:113
Semaphore::waiting
unsigned waiting
Definition:
thread.h:116
Semaphore::wait
void wait()
Definition:
thread.cc:23
Semaphore::post
void post()
Definition:
thread.cc:33
Semaphore::Semaphore
Semaphore(unsigned count0)
Definition:
thread.h:120
Semaphore::count
unsigned count
Definition:
thread.h:115
Semaphore::Semaphore
Semaphore()
Definition:
thread.h:118
result
return result
Definition:
facAbsBiFact.cc:75
message
void message(int i, int *reduc, int *olddeg, kStrategy strat, int red_result)
Definition:
kutil.cc:7512
NULL
#define NULL
Definition:
omList.c:12
no_thread
pthread_t no_thread
Definition:
thread.cc:16
ThreadError
void ThreadError(const char *message)
Definition:
thread.cc:18
Thread
pthread_t Thread
Definition:
thread.h:11
Generated on Mon Feb 27 2023 10:53:50 for My Project by
doxygen 1.9.5
for
Singular