summaryrefslogtreecommitdiff
path: root/glib/src/thread.ccg
blob: af4695fc50d13302b1f57d1fd79b0f71b5354dfd (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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* 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 Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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)
{
  const auto slot = reinterpret_cast<sigc::slot_base*>(data);

  try
  {
    // Recreate the specific slot, and drop the reference obtained by create().
    (*static_cast<sigc::slot<void>*>(slot))();
  }
  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();
  }

  delete slot;
  return nullptr;
}

} //extern "C"

} // anonymous namespace


namespace Glib
{

// This was always meant as an internal method. It is no longer called,
// and no longer needs to be called. We are keeping it just to avoid
// breaking ABI, though hopefully nobody is using it anyway.
// TODO: Remove this when we can break ABI.
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
  const auto slot_copy = new sigc::slot<void>(slot);

  GError* error = nullptr;

  const auto thread = g_thread_try_new(nullptr,
      &call_thread_entry_slot, slot_copy, &error);

  if(error)
  {
    delete slot_copy;
    // Glib::Error::throw_exception() will probably wrap G_THREAD_ERROR in a
    // Glib::Threads::ThreadError instance, but we want a Glib::ThreadError.
    if (error->domain == G_THREAD_ERROR)
      throw Glib::ThreadError(error);
    else
      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
  const auto slot_copy = new sigc::slot<void>(slot);

  GError* error = nullptr;

  const auto 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() will probably wrap G_THREAD_ERROR in a
    // Glib::Threads::ThreadError instance, but we want a Glib::ThreadError.
    if (error->domain == G_THREAD_ERROR)
      throw Glib::ThreadError(error);
    else
      Glib::Error::throw_exception(error);
  }

  return reinterpret_cast<Thread*>(thread);
}

// static
Thread* Thread::self()
{
  return reinterpret_cast<Thread*>(g_thread_self());
}

void Thread::join()
{
  g_thread_join(&gobject_);
}

bool Thread::joinable() const
{
  return true; //An appropriate result now that this is deprecated because all threads are now joinable.
}

void Thread::set_priority(ThreadPriority priority)
{
  g_thread_set_priority(&gobject_, (GThreadPriority) priority);
}

ThreadPriority Thread::get_priority() const
{
  return THREAD_PRIORITY_NORMAL; //An appropriate result now that this is deprecated because the priority concept has been removed.
}

void thread_init(GThreadFunctions* /* vtable */)
{
  //g_thread_init() is deprecated and now does nothing,
  //so we do not even call it. That avoids a need to link to gthread-2.0,
  //which contains the empty g_thread_init() implementation.
  //g_thread_init(vtable);

  Glib::thread_init_impl();
}

bool thread_supported()
{
  //MSVC++ needs the != 0 to avoid an int -> bool cast warning.
  return (g_thread_supported() != 0);
}


// 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()) //TODO: Use a statically-allocated GMutext instead, with g_mutex_init().
{}

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_);
}

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));
}

void* StaticPrivate_get_helper(GStaticPrivate *private_key) {
  return g_static_private_get(private_key);
}

void StaticPrivate_set_helper(GStaticPrivate *private_key, gpointer data, GDestroyNotify notify) {
  return g_static_private_set(private_key, data, notify);
}

GPrivate* GPrivate_new_helper(GDestroyNotify notify) {
  return g_private_new(notify);
}


} // namespace Glib