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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
// -*- c++ -*-
/* $Id: thread.ccg,v 1.9 2006/05/12 08:08:44 murrayc Exp $ */
/* Copyright (C) 2002 The gtkmm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <glibmm/exceptionhandler.h>
#include <glib.h>
namespace
{
extern "C"
{
static void* call_thread_entry_slot(void* data)
{
sigc::slot_base *const slot = reinterpret_cast<sigc::slot_base*>(data);
#ifdef GLIBMM_EXCEPTIONS_ENABLED
try
{
#endif //GLIBMM_EXCEPTIONS_ENABLED
// Recreate the specific slot, and drop the reference obtained by create().
(*static_cast<sigc::slot<void>*>(slot))();
#ifdef GLIBMM_EXCEPTIONS_ENABLED
}
catch(Glib::Thread::Exit&)
{
// Just exit from the thread. The Thread::Exit exception
// is our sane C++ replacement of g_thread_exit().
}
catch(...)
{
Glib::exception_handlers_invoke();
}
#endif //GLIBMM_EXCEPTIONS_ENABLED
delete slot;
return 0;
}
} //extern "C"
} // anonymous namespace
namespace Glib
{
// internal
void thread_init_impl()
{
// Make sure the exception map is initialized before creating any thread.
Glib::Error::register_init();
}
/**** Glib::Thread *********************************************************/
// static
Thread* Thread::create(const sigc::slot<void>& slot, bool joinable)
{
// Make a copy of slot on the heap
sigc::slot_base *const slot_copy = new sigc::slot<void>(slot);
GError* error = 0;
GThread *const thread = g_thread_create(
&call_thread_entry_slot, slot_copy, joinable, &error);
if(error)
{
delete slot_copy;
Glib::Error::throw_exception(error);
}
return reinterpret_cast<Thread*>(thread);
}
// static
Thread* Thread::create(const sigc::slot<void>& slot, unsigned long stack_size,
bool joinable, bool bound, ThreadPriority priority)
{
// Make a copy of slot on the heap
sigc::slot_base *const slot_copy = new sigc::slot<void>(slot);
GError* error = 0;
GThread *const thread = g_thread_create_full(
&call_thread_entry_slot, slot_copy, stack_size, joinable,
bound, (GThreadPriority) priority, &error);
if(error)
{
delete slot_copy;
Glib::Error::throw_exception(error);
}
return reinterpret_cast<Thread*>(thread);
}
// static
Thread* Thread::self()
{
return reinterpret_cast<Thread*>(g_thread_self());
}
bool Thread::joinable() const
{
return gobject_.joinable;
}
void Thread::join()
{
g_thread_join(&gobject_);
}
void Thread::set_priority(ThreadPriority priority)
{
g_thread_set_priority(&gobject_, (GThreadPriority) priority);
}
ThreadPriority Thread::get_priority() const
{
return (ThreadPriority) gobject_.priority;
}
// static
void Thread::yield()
{
g_thread_yield();
}
Thread* wrap(GThread* gobject)
{
return reinterpret_cast<Thread*>(gobject);
}
/**** Glib::StaticMutex ****************************************************/
void StaticMutex::lock()
{
g_static_mutex_lock(&gobject_);
}
bool StaticMutex::trylock()
{
return g_static_mutex_trylock(&gobject_);
}
void StaticMutex::unlock()
{
g_static_mutex_unlock(&gobject_);
}
StaticMutex::operator Mutex&()
{
// If GStaticMutex is implemented as struct (e.g. on Linux), its first struct
// member (runtime_mutex) is a GMutex pointer. If the gthread implementation
// is native (i.e. the vtable pointer passed to g_thread_init() was 0), then
// the runtime_mutex pointer is unused, and the rest of the GStaticMutex
// struct resembles the mutex data.
//
// On Win32, GStaticMutex is just a typedef to struct _GMutex*. Either way,
// the first sizeof(GMutex*) bytes of GStaticMutex always resemble a GMutex
// pointer. The gthread implementation relies on that, and we'll also do so.
GMutex*& runtime_mutex = reinterpret_cast<GMutex*&>(gobject_);
// Fortunately, it cannot hurt if we set this to the GMutex pointer returned
// by g_static_mutex_get_mutex(). Either we just overwrite it with the same
// value, or it was unused anyway. Doing that allows casting the pointer
// location to a Glib::Mutex reference (its only data member is a GMutex*).
runtime_mutex = g_static_mutex_get_mutex(&gobject_);
return reinterpret_cast<Mutex&>(runtime_mutex);
}
/**** Glib::Mutex **********************************************************/
Mutex::Mutex()
:
gobject_ (g_mutex_new())
{}
Mutex::~Mutex()
{
g_mutex_free(gobject_);
}
void Mutex::lock()
{
g_mutex_lock(gobject_);
}
bool Mutex::trylock()
{
return g_mutex_trylock(gobject_);
}
void Mutex::unlock()
{
g_mutex_unlock(gobject_);
}
/**** Glib::StaticRecMutex *************************************************/
void StaticRecMutex::lock()
{
g_static_rec_mutex_lock(&gobject_);
}
bool StaticRecMutex::trylock()
{
return g_static_rec_mutex_trylock(&gobject_);
}
void StaticRecMutex::unlock()
{
g_static_rec_mutex_unlock(&gobject_);
}
void StaticRecMutex::lock_full(unsigned int depth)
{
g_static_rec_mutex_lock_full(&gobject_, depth);
}
unsigned int StaticRecMutex::unlock_full()
{
return g_static_rec_mutex_unlock_full(&gobject_);
}
StaticRecMutex::operator RecMutex&()
{
return static_cast<RecMutex&>(*this);
}
/**** Glib::RecMutex *******************************************************/
RecMutex::RecMutex()
{
g_static_rec_mutex_init(&gobject_);
// GLib doesn't have GRecMutex, only GStaticRecMutex. Force initialization
// of the mutex now, to mimic the behaviour of a (hypothetical) GRecMutex.
g_static_mutex_get_mutex(&gobject_.mutex);
}
RecMutex::~RecMutex()
{
g_static_rec_mutex_free(&gobject_);
}
/**** Glib::StaticRWLock ***************************************************/
void StaticRWLock::reader_lock()
{
g_static_rw_lock_reader_lock(&gobject_);
}
bool StaticRWLock::reader_trylock()
{
return g_static_rw_lock_reader_trylock(&gobject_);
}
void StaticRWLock::reader_unlock()
{
g_static_rw_lock_reader_unlock(&gobject_);
}
void StaticRWLock::writer_lock()
{
g_static_rw_lock_writer_lock(&gobject_);
}
bool StaticRWLock::writer_trylock()
{
return g_static_rw_lock_writer_trylock(&gobject_);
}
void StaticRWLock::writer_unlock()
{
g_static_rw_lock_writer_unlock(&gobject_);
}
StaticRWLock::operator RWLock&()
{
return static_cast<RWLock&>(*this);
}
/**** Glib::RWLock *********************************************************/
RWLock::RWLock()
{
g_static_rw_lock_init(&gobject_);
// GLib doesn't have GRWLock, only GStaticRWLock. Force initialization
// of the mutex and the condition variables now, to mimic the behaviour
// of a (hypothetical) GRWLock.
if(g_static_mutex_get_mutex(&gobject_.mutex))
{
gobject_.read_cond = g_cond_new();
gobject_.write_cond = g_cond_new();
}
}
RWLock::~RWLock()
{
g_static_rw_lock_free(&gobject_);
}
/**** Glib::Cond ***********************************************************/
Cond::Cond()
:
gobject_ (g_cond_new())
{}
Cond::~Cond()
{
g_cond_free(gobject_);
}
void Cond::signal()
{
g_cond_signal(gobject_);
}
void Cond::broadcast()
{
g_cond_broadcast(gobject_);
}
void Cond::wait(Mutex& mutex)
{
g_cond_wait(gobject_, mutex.gobj());
}
bool Cond::timed_wait(Mutex& mutex, const Glib::TimeVal& abs_time)
{
return g_cond_timed_wait(gobject_, mutex.gobj(), const_cast<Glib::TimeVal*>(&abs_time));
}
} // namespace Glib
|