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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_THREADS_KERNEl_2_
#define DLIB_THREADS_KERNEl_2_
#ifdef DLIB_ISO_CPP_ONLY
#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code. Turn DLIB_ISO_CPP_ONLY off if you want to use it."
#endif
#include "threads_kernel_abstract.h"
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
typedef pthread_t thread_id_type;
inline thread_id_type get_thread_id (
)
{
return pthread_self();
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// mutex object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// forward declaration of signaler
class signaler;
class mutex
{
// give signaler access to hMutex
friend class signaler;
public:
mutex (
)
{
if (pthread_mutex_init(&myMutex,0))
{
throw dlib::thread_error(ECREATE_MUTEX,
"in function mutex::mutex() an error occurred making the mutex"
);
}
}
~mutex (
) { pthread_mutex_destroy(&myMutex); }
void lock (
) const { pthread_mutex_lock(&myMutex); }
void unlock (
) const { pthread_mutex_unlock(&myMutex); }
private:
mutable pthread_mutex_t myMutex;
// restricted functions
mutex(mutex&); // copy constructor
mutex& operator=(mutex&); // assignement opertor
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// signaler object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class signaler
{
public:
signaler (
const mutex& assoc_mutex
) :
associated_mutex(&assoc_mutex.myMutex),
m(assoc_mutex)
{
if (pthread_cond_init(&cond,0))
{
throw dlib::thread_error(ECREATE_SIGNALER,
"in function signaler::signaler() an error occurred making the signaler"
);
}
}
~signaler (
) { pthread_cond_destroy(&cond); }
void wait (
) const
{
pthread_cond_wait(&cond,associated_mutex);
}
bool wait_or_timeout (
unsigned long milliseconds
) const
{
timespec time_to_wait;
timeval curtime;
gettimeofday(&curtime,0);
// get the time and adjust the timespec object by the appropriate amount
time_to_wait.tv_sec = milliseconds/1000 + curtime.tv_sec;
time_to_wait.tv_nsec = curtime.tv_usec;
time_to_wait.tv_nsec *= 1000;
time_to_wait.tv_nsec += (milliseconds%1000)*1000000;
time_to_wait.tv_sec += time_to_wait.tv_nsec/1000000000;
time_to_wait.tv_nsec = time_to_wait.tv_nsec%1000000000;
if ( pthread_cond_timedwait(&cond,associated_mutex,&time_to_wait) == ETIMEDOUT)
{
return false;
}
else
{
return true;
}
}
void signal (
) const { pthread_cond_signal(&cond); }
void broadcast (
) const { pthread_cond_broadcast(&cond); }
const mutex& get_mutex (
) const { return m; }
private:
pthread_mutex_t* const associated_mutex;
mutable pthread_cond_t cond;
const mutex& m;
// restricted functions
signaler(signaler&); // copy constructor
signaler& operator=(signaler&); // assignement opertor
};
// ----------------------------------------------------------------------------------------
namespace threads_kernel_shared_helpers
{
bool spawn_thread (
void (*funct)(void*),
void* param
);
/*!
is identical to create_new_thread() but just doesn't use any thread pooling.
!*/
}
// ----------------------------------------------------------------------------------------
}
#include "threads_kernel_shared.h"
#ifdef NO_MAKEFILE
#include "threads_kernel_2.cpp"
#endif
#endif // DLIB_THREADS_KERNEl_2_