c++-gtk-utils
mutex.h
Go to the documentation of this file.
1 /* Copyright (C) 2005 to 2013 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_MUTEX_H
40 #define CGU_MUTEX_H
41 
42 #include <exception>
43 #include <pthread.h>
44 #include <time.h>
45 
46 #include <glib.h>
48 
49 /**
50  * @file mutex.h
51  * @brief Provides wrapper classes for pthread mutexes and condition
52  * variables, and scoped locking classes for exception safe mutex
53  * locking.
54  * @note If the system supports monotonic clocks (and this library is
55  * not being cross-compiled onto a different architecture), then a
56  * system monotonic clock will be used in
57  * Cgu::Thread::Cond::timed_wait() and
58  * Cgu::Thread::Cond::get_abs_time(). This can be tested at run time
59  * with Cgu::Thread::Cond::have_monotonic_clock().
60  */
61 
62 namespace Cgu {
63 
64 namespace Thread {
65 
66 struct CondError: public std::exception {
67  virtual const char* what() const throw() {return "Thread::CondError";}
68 };
69 
70 /*
71  * Since version 1.2.0, which automatically checks for monotonic
72  * clocks in its configure script, this exception is no longer thrown.
73  * We keep the class just for source compatibility purposes.
74  */
75 #ifndef DOXYGEN_PARSING
76 struct CondSetClockError: public std::exception {
77  virtual const char* what() const throw() {return "Thread::CondSetClockError";}
78 };
79 #endif
80 
81 struct MutexError: public std::exception {
82  virtual const char* what() const throw() {return "Thread::MutexError";}
83 };
84 
85 struct RecMutexError: public std::exception {
86  virtual const char* what() const throw() {return "Thread::RecMutexError";}
87 };
88 
89 class Cond;
90 
91 /**
92  * @class Mutex mutex.h c++-gtk-utils/mutex.h
93  * @brief A wrapper class for pthread mutexes.
94  * @sa Thread::Thread Thread::Mutex::Lock Thread::Mutex::TrackLock Thread::Cond Thread::RecMutex
95  *
96  * This class can be used interchangeably with threads started with
97  * GThread and by this library, as both glib and this library use
98  * pthreads underneath on POSIX and other unix-like OSes. Mutex
99  * objects can be constructed statically as well as dynamically and
100  * there is no need to call g_thread_init() before they are
101  * constructed, even if glib < 2.32 is used. (If created as a static
102  * object in global scope, it will not be possible to catch
103  * Thread::MutexError thrown by its constructor, but if a static
104  * global mutex throws there is nothing that could be done anyway
105  * except abort, and it would show that the pthreads installation is
106  * seriously defective.)
107  */
108 
109 class Mutex {
110  pthread_mutex_t pthr_mutex;
111 
112  // mutexes cannot be copied
113  Mutex(const Mutex&);
114  Mutex& operator=(const Mutex&);
115 public:
116  class Lock;
117  class TrackLock;
118  friend class Cond;
119 
120 /**
121  * Locks the mutex and acquires ownership. Blocks if already locked
122  * until it becomes free. It is not a cancellation point. It does
123  * not throw. It is thread safe.
124  * @return 0 if successful, otherwise the pthread mutex error number.
125  * @note With this library implementation, the only pthread error
126  * number which could be returned by this method is EDEADLK, which it
127  * would do if the default pthread mutex behaviour happens to return
128  * that error rather than deadlock in the case of recursive locking.
129  * Most default implementations do not do this and hence the return
130  * value is usually not worth checking for except during debugging.
131  */
132  int lock() {return pthread_mutex_lock(&pthr_mutex);}
133 
134 /**
135  * Tries to lock the mutex and acquire ownership, but returns
136  * immediately if it is already locked with value EBUSY. It is not a
137  * cancellation point. It does not throw. It is thread safe.
138  * @return 0 if successful, otherwise EBUSY.
139  * @note With this library implementation, the only pthread error
140  * number which could be returned by this method is EBUSY.
141  */
142  int trylock() {return pthread_mutex_trylock(&pthr_mutex);}
143 
144 /**
145  * Unlocks a locked mutex owned by the calling thread and relinquishes
146  * ownership. It is not a cancellation point. It does not throw. It
147  * must be called by the thread which owns the mutex.
148  * @return 0 if successful, otherwise the pthread mutex error number.
149  * @note With this library implementation, the only pthread error
150  * number which could be returned by this method is EPERM because the
151  * calling thread does not own the mutex (however POSIX does not
152  * require that return value in that case and hence the return value
153  * is usually not worth checking for except during debugging).
154  */
155  int unlock() {return pthread_mutex_unlock(&pthr_mutex);}
156 
157 /**
158  * Initialises the pthread mutex. It is not a cancellation point.
159  * @exception Cgu::Thread::MutexError Throws this exception if
160  * initialization of the mutex fails. (It is often not worth checking
161  * for this, as it means either memory is exhausted or pthread has run
162  * out of other resources to create new mutexes.)
163  */
164  Mutex() {if (pthread_mutex_init(&pthr_mutex, 0)) throw MutexError();}
165 
166 /**
167  * Destroys the pthread mutex. It is not a cancellation point. It
168  * does not throw. Destroying a mutex which is currently locked or
169  * associated with an active condition variable wait results in
170  * undefined behavior.
171  */
172  ~Mutex() {pthread_mutex_destroy(&pthr_mutex);}
173 
174 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
176 #endif
177 };
178 
179 // used as a second argument to Lock::Lock() and TrackLock::TrackLock
180 // in cases where the mutex has already been locked (say by Mutex::trylock())
181 enum Locked {locked};
182 // used as a second argument to TrackLock::TrackLock() in cases where
183 // locking of the mutex is to be deferred
185 
186 /**
187  * @class Mutex::Lock mutex.h c++-gtk-utils/mutex.h
188  * @brief A scoped locking class for exception safe Mutex locking.
189  * @sa Thread::Mutex Thread::Mutex::TrackLock Thread::Thread Thread::Cond
190  */
191 
192 class Mutex::Lock {
193  Mutex& mutex;
194 
195  // locks cannot be copied
196  Lock(const Mutex::Lock&);
197  Mutex::Lock& operator=(const Mutex::Lock&);
198 public:
199  friend class Cond;
200 
201 /**
202  * Calls Mutex::lock(), and so locks the mutex and reacquires
203  * ownership. It blocks if the mutex is already locked until the
204  * mutex becomes free. This method should normally only be called if
205  * a previous call has been made to Mutex::Lock::unlock() (that is,
206  * where the thread owning the Mutex::Lock object has temporarily
207  * allowed another thread to take the mutex concerned). It is not a
208  * cancellation point. It does not throw.
209  * @return 0 if successful, otherwise the pthread mutex error number.
210  * @note With this library implementation, the only pthread error
211  * number which could be returned by this method is EDEADLK, which it
212  * would do if the default pthread mutex behaviour happens to return
213  * that error rather than deadlock in the case of recursive locking.
214  * Most default implementations do not do this and hence the return
215  * value is usually not worth checking for except during debugging.
216  * @sa Mutex::TrackLock.
217  */
218  int lock() {return mutex.lock();}
219 
220 /**
221  * Calls Mutex::trylock(), and so tries to lock the mutex and
222  * reacquire ownership, but returns immediately if it is already
223  * locked with value EBUSY. This method should normally only be
224  * called if a previous call has been made to Mutex::Lock::unlock()
225  * (that is, where the thread owning the Mutex::Lock object has
226  * temporarily allowed another thread to take the mutex concerned).
227  * It is not a cancellation point. It does not throw.
228  * @return 0 if successful, otherwise EBUSY.
229  * @note With this library implementation, the only pthread error
230  * number which could be returned by this method is EBUSY.
231  * @sa Mutex::TrackLock.
232  */
233  int trylock() {return mutex.trylock();}
234 
235 /**
236  * Calls Mutex::unlock(), and so unlocks a locked mutex owned by the
237  * calling thread and relinquishes ownership (so temporarily allowing
238  * another thread to take the mutex). This method should normally
239  * only be called if it is to be followed by a call to
240  * Mutex::Lock::lock() or a successful call to Mutex::Lock::trylock()
241  * before the Mutex::Lock object concerned goes out of scope
242  * (otherwise Mutex::Lock's destructor will attempt to unlock an
243  * already unlocked mutex or a mutex of which another thread has by
244  * then taken ownership - Mutex::Lock objects do not maintain state).
245  * See Mutex::TrackLock::unlock() for a safe version of this method.
246  * It is not a cancellation point. It does not throw.
247  * @return 0 if successful, otherwise the pthread mutex error number.
248  * @note With this library implementation, the only pthread error
249  * number which could be returned by this method is EPERM because the
250  * calling thread does not own the mutex (however POSIX does not
251  * require that return value in that case and hence the return value
252  * is usually not worth checking for except during debugging).
253  * @sa Mutex::TrackLock.
254  */
255  int unlock() {return mutex.unlock();}
256 
257 /**
258  * This constructor locks the mutex passed to it. It is not a
259  * cancellation point. It does not throw.
260  * @param mutex_ The mutex to be locked.
261  */
262  Lock(Mutex& mutex_): mutex(mutex_) {mutex.lock();}
263 
264 /**
265  * This constructor takes an already locked mutex (say as a result of
266  * Mutex::trylock()), and takes ownership of it. It is not a
267  * cancellation point. It does not throw.
268  * @param mutex_ The mutex to be managed by this object.
269  * @param tag Pass the Cgu::Thread::locked enum tag to this parameter.
270  */
271  Lock(Mutex& mutex_, Locked tag): mutex(mutex_) {}
272 
273 /**
274  * The destructor unlocks the owned mutex. It is not a cancellation
275  * point. It does not throw.
276  */
277  ~Lock() {mutex.unlock();}
278 
279 /* Only has effect if --with-glib-memory-slices-compat or
280  * --with-glib-memory-slices-no-compat option picked */
282 };
283 
284 /**
285  * @class Mutex::TrackLock mutex.h c++-gtk-utils/mutex.h
286  * @brief A scoped locking class for exception safe Mutex locking
287  * which tracks the status of its mutex.
288  * @sa Thread::Mutex Thread::Mutex::Lock Thread::Thread Thread::Cond
289  *
290  * This class is similar to a Mutex::Lock object, except that it
291  * tracks whether the mutex it manages is locked by the thread
292  * creating the Mutex::TrackLock object (provided that, while the
293  * Mutex::TrackLock object exists, the thread creating it only
294  * accesses the mutex through that object). This enables
295  * Mutex::TrackLock::unlock() to be used without it being followed
296  * later by a call to Mutex::TrackLock::lock() or a successful call to
297  * Mutex::TrackLock::trylock(), and also permits locking to be
298  * deferred until after construction of the lock object. Note that
299  * only one thread may call the methods of any one Mutex::TrackLock
300  * object, including causing its destructor to be invoked.
301  */
302 
304  Mutex& mutex;
305  bool owner;
306 
307  // locks cannot be copied
308  TrackLock(const Mutex::TrackLock&);
309  Mutex::TrackLock& operator=(const Mutex::TrackLock&);
310 public:
311  friend class Cond;
312 
313 /**
314  * Calls Mutex::lock(), and so locks the mutex and acquires ownership.
315  * It blocks if the mutex is already locked until the mutex becomes
316  * free. This method should normally only be called if a previous
317  * call has been made to Mutex::TrackLock::unlock() or this
318  * Mutex::TrackLock object has been constructed with the Thread::defer
319  * enum tag. It is not a cancellation point. It does not throw.
320  * @return 0 if successful, otherwise the pthread mutex error number.
321  * @note With this library implementation, the only pthread error
322  * number which could be returned by this method is EDEADLK, which it
323  * would do if the default pthread mutex behaviour happens to return
324  * that error rather than deadlock in the case of recursive locking.
325  * Most default implementations do not do this and hence the return
326  * value is usually not worth checking for except during debugging.
327  *
328  * Since 1.2.1
329  */
330  int lock() {int ret = mutex.lock(); if (!owner) owner = !ret; return ret;}
331 
332 /**
333  * Calls Mutex::trylock(), and so tries to lock the mutex and acquire
334  * ownership, but returns immediately if it is already locked with
335  * value EBUSY. This method should normally only be called if a
336  * previous call has been made to Mutex::TrackLock::unlock() or this
337  * Mutex::TrackLock object has been constructed with the Thread::defer
338  * enum tag. It is not a cancellation point. It does not throw.
339  * @return 0 if successful, otherwise EBUSY.
340  * @note With this library implementation, the only pthread error
341  * number which could be returned by this method is EBUSY.
342  *
343  * Since 1.2.1
344  */
345  int trylock() {int ret = mutex.trylock(); if (!owner) owner = !ret; return ret;}
346 
347 /**
348  * Calls Mutex::unlock(), and so unlocks a locked mutex owned by the
349  * calling thread. It will cause is_owner() to return false unless a
350  * subsequent call is made to lock() or a subsequent successful call
351  * is made to trylock(). It is not a cancellation point. It does not
352  * throw.
353  * @return 0 if successful, otherwise the pthread mutex error number.
354  * @note With this library implementation, the only pthread error
355  * number which could be returned by this method is EPERM because the
356  * calling thread does not own the mutex (however POSIX does not
357  * require that return value in that case and hence the return value
358  * is usually not worth checking for except during debugging).
359  *
360  * Since 1.2.1
361  */
362  int unlock() {int ret = mutex.unlock(); if (owner) owner = ret; return ret;}
363 
364 /**
365  * Indicates whether the mutex managed by this Mutex::TrackLock object
366  * is locked, and so owned, by it. It does not throw.
367  * @return true if the mutex is locked by this object, otherwise
368  * false.
369  *
370  * Since 1.2.1
371  */
372  bool is_owner() const {return owner;}
373 
374 /**
375  * This constructor locks the mutex passed to it. It is not a
376  * cancellation point. It does not throw.
377  * @param mutex_ The mutex to be locked.
378  *
379  * Since 1.2.1
380  */
381  TrackLock(Mutex& mutex_): mutex(mutex_), owner(true) {mutex.lock();}
382 
383 /**
384  * This constructor takes an already locked mutex (say as a result of
385  * Mutex::trylock()), and takes ownership of it. It is not a
386  * cancellation point. It does not throw.
387  * @param mutex_ The mutex to be managed by this object.
388  * @param tag Pass the Cgu::Thread::locked enum tag to this parameter.
389  *
390  * Since 1.2.1
391  */
392  TrackLock(Mutex& mutex_, Locked tag): mutex(mutex_), owner(true) {}
393 
394 /**
395  * This constructor defers locking of the mutex (and so taking
396  * ownership of it) until an explicit call to lock() or trylock() is
397  * made. It is not a cancellation point. It does not throw.
398  * @param mutex_ The mutex to be managed by this object.
399  * @param tag Pass the Cgu::Thread::defer enum tag to this parameter.
400  *
401  * Since 1.2.1
402  */
403  TrackLock(Mutex& mutex_, DeferLock tag): mutex(mutex_), owner(false) {}
404 
405 /**
406  * The destructor unlocks the managed mutex if it is locked. It is
407  * not a cancellation point. It does not throw.
408  *
409  * Since 1.2.1
410  */
411  ~TrackLock() {if (owner) mutex.unlock();}
412 
413 /* Only has effect if --with-glib-memory-slices-compat or
414  * --with-glib-memory-slices-no-compat option picked */
416 };
417 
418 /**
419  * @class Cond mutex.h c++-gtk-utils/mutex.h
420  * @brief A wrapper class for pthread condition variables.
421  * @sa Thread::Thread Thread::Mutex Thread::Mutex::Lock Thread::Mutex::TrackLock
422  */
423 
424 class Cond {
425  pthread_cond_t cond;
426 
427  // Cond cannot be copied
428  Cond(const Cond&);
429  Cond& operator=(const Cond&);
430 public:
431 
432 /**
433  * Unblock at least one thread waiting on this condition variable.
434  * Can be called by any thread. It is not a cancellation point. Does
435  * not throw.
436  * @return 0 if successful, otherwise the pthread error number.
437  * @note With this library implementation, no pthread error should
438  * arise so there is no need to check the return value.
439  */
440  int signal() {return pthread_cond_signal(&cond);}
441 
442 /**
443  * Unblocks all threads waiting on this condition variable, which
444  * acquire the mutex in an order determined by the scheduling policy.
445  * Can be called by any thread. It is not a cancellation point. Does
446  * not throw.
447  * @return 0 if successful, otherwise the pthread error number.
448  * @note With this library implementation, no pthread error should
449  * arise so there is no need to check the return value.
450  */
451  int broadcast() {return pthread_cond_broadcast(&cond);}
452 
453 /**
454  * Waits on this condition variable until awoken. It must be called
455  * by the thread which owns the mutex. Re-acquires the mutex when
456  * awoken. It is a cancellation point. This method is cancellation
457  * safe even if the stack does not unwind on cancellation (but if the
458  * stack does not unwind some other way of destroying this object on
459  * cancellation is required, such as by having it allocated on
460  * freestore and deleted in a cancellation clean-up handler). This
461  * method does not throw.
462  * @param mutex The locked mutex associated with the wait which is
463  * re-acquired on being awoken.
464  * @return 0 after being awoken on waiting, otherwise the pthread
465  * error number.
466  * @note 1. pthread condition variables can, as a matter of design,
467  * awake spontaneously (and Cond::signal() may awaken more than one
468  * thread). Therefore the relevant condition should be tested in a
469  * while loop and not in an if block. 0 will be returned on a
470  * spontaneous awakening.
471  * @note 2. With this library implementation, the only pthread error
472  * numbers which could be returned are EINVAL (if the mutex argument
473  * is not a valid mutex) or EPERM (if the thread calling this method
474  * does not own the mutex).
475  */
476  int wait(Mutex& mutex) {return pthread_cond_wait(&cond, &mutex.pthr_mutex);}
477 
478 /**
479  * Does the same as Cond::wait(Mutex&), except that as a convenience
480  * it will take a Mutex::Lock object handling the Mutex object as an
481  * alternative to passing the Mutex object itself.
482  */
483  int wait(Mutex::Lock& lock) {return wait(lock.mutex);}
484 
485 /**
486  * Does the same as Cond::wait(Mutex&), except that as a convenience
487  * it will take a Mutex::TrackLock object handling the Mutex object as
488  * an alternative to passing the Mutex object itself.
489  *
490  * Since 1.2.1
491  */
492  int wait(Mutex::TrackLock& lock) {return wait(lock.mutex);}
493 
494 /**
495  * Waits on this condition variable until awoken (in which case it
496  * re-acquires the mutex), or until the timeout expires (in which case
497  * it re-acquires the mutex and returns with ETIMEDOUT). It must be
498  * called by the thread which owns the mutex. Re-acquires the mutex
499  * when awoken or timing out. It is a cancellation point. This
500  * method is cancellation safe even if the stack does not unwind on
501  * cancellation (but if the stack does not unwind some other way of
502  * destroying this object on cancellation is required, such as by
503  * having it allocated on freestore and deleted in a cancellation
504  * clean-up handler). This method does not throw.
505  * @param mutex The locked mutex associated with the wait which is
506  * re-acquired on being awoken or timing out.
507  * @param abs_time The time at which the wait will unblock if not
508  * previously awoken. A suitable value can be obtained by calling
509  * the get_abs_time() function.
510  * @return 0 after being awoken on waiting, otherwise ETIMEDOUT or
511  * other pthread error number.
512  * @note 1. With this library implementation, apart from ETIMEDOUT,
513  * the only pthread error numbers which could be returned are EINVAL
514  * (if the mutex argument is not a valid mutex or the abs_time
515  * argument does not comprise a valid timespec struct) or EPERM (if
516  * the thread calling this method does not own the mutex).
517  * @note 2. pthread condition variables can, as a matter of design,
518  * awake spontaneously (and Cond::signal() may awaken more than one
519  * thread). Therefore the relevant condition should be tested in a
520  * while loop and not in an if block. 0 will be returned on a
521  * spontaneous awakening.
522  * @note 3. If the system supports monotonic clocks (and this library
523  * is not being cross-compiled onto a different architecture), then
524  * condition variables will use a monotonic clock in
525  * Cond::timed_wait() and Cond::get_abs_time(). This can be tested at
526  * run time with Cond::have_monotonic_clock().
527  */
528  int timed_wait(Mutex& mutex, const timespec& abs_time) {
529  return pthread_cond_timedwait(&cond, &mutex.pthr_mutex, &abs_time);
530  }
531 
532 /**
533  * Does the same as Cond::timed_wait(Mutex&, const timespec&), except
534  * that as a convenience it will take a Mutex::Lock object handling
535  * the Mutex object as an alternative to passing the Mutex object
536  * itself.
537  */
539  const timespec& abs_time) {return timed_wait(lock.mutex, abs_time);}
540 
541 /**
542  * Does the same as Cond::timed_wait(Mutex&, const timespec&), except
543  * that as a convenience it will take a Mutex::TrackLock object
544  * handling the Mutex object as an alternative to passing the Mutex
545  * object itself.
546  *
547  * Since 1.2.1
548  */
550  const timespec& abs_time) {return timed_wait(lock.mutex, abs_time);}
551 
552 /**
553  * This is a utility function that inserts into a timespec structure
554  * the current time plus a given number of milliseconds ahead, which
555  * can be applied to a call to Cond::timed_wait(). It does not throw.
556  * It is thread-safe.
557  * @param ts A timespec object into which the result of current time +
558  * millisec will be placed.
559  * @param millisec The number of milliseconds ahead of current time to
560  * which the timespec object will be set.
561  * @note If the system supports monotonic clocks (and this library is
562  * not being cross-compiled onto a different architecture), then
563  * condition variables will use a system monotonic clock in this
564  * method and Cond::timed_wait(). This can be tested at run time with
565  * Cond::have_monotonic_clock().
566  */
567  static void get_abs_time(timespec& ts, unsigned int millisec);
568 
569 /**
570  * Indicates whether the library has been compiled with support for
571  * monotonic clocks in Cond::timed_wait(). Most recent linux and BSD
572  * distributions will support them, and this function would normally
573  * return true unless the library has been cross-compiled from one
574  * platform to a different platform. This function can be tested at
575  * program initialization, and if they are not supported a warning can
576  * be given to the user about the deficiences of using the system
577  * clock for timed events. It does not throw. It is thread safe.
578  * @return true if the library has been compiled with support for
579  * monotonic clocks in Cond::timed_wait(), otherwise false.
580  *
581  * Since 1.2.0
582  */
583  static bool have_monotonic_clock();
584 
585 /**
586  * Initialises the pthread condition variable. It is not a
587  * cancellation point.
588  * @exception Cgu::Thread::CondError Throws this exception if
589  * initialization of the condition variable fails. (It is often not
590  * worth checking for CondError, as it means either memory is
591  * exhausted or pthread has run out of other resources to create new
592  * condition variables.)
593  * @note If the system supports monotonic clocks (and this library is
594  * not being cross-compiled onto a different architecture), then
595  * condition variables will use a system monotonic clock in
596  * Cond::timed_wait() and Cond::get_abs_time(). This can be tested at
597  * run time by calling Cond::have_monotonic_clock().
598  */
599  Cond();
600 
601 /**
602  * Destroys the pthread condition variable. It is not a cancellation
603  * point. The destructor does not throw. Destroying a condition
604  * variable on which another thread is currently blocked results in
605  * undefined behavior.
606  */
607  ~Cond(void) {pthread_cond_destroy(&cond);}
608 
609 /* Only has effect if --with-glib-memory-slices-compat or
610  * --with-glib-memory-slices-no-compat option picked */
612 };
613 
614 /**
615  * @class RecMutex mutex.h c++-gtk-utils/mutex.h
616  * @brief A wrapper class for pthread mutexes which provides a
617  * recursive mutex.
618  * @sa Thread::Thread Thread::RecMutex::Lock Thread::RecMutex::TrackLock Thread::Mutex
619  *
620  * This class can be used interchangeably with threads started with
621  * GThread and by this library, as both glib and this library use
622  * pthreads underneath on POSIX and other unix-like OSes. RecMutex
623  * objects can be constructed statically as well as dynamically and
624  * there is no need to call g_thread_init() before they are
625  * constructed, even if glib < 2.32 is used. (If created as a static
626  * object in global scope, it will not be possible to catch
627  * Thread::MutexError or Thread::RecMutexError thrown by its
628  * constructor, but if a static global mutex throws there is nothing
629  * that could be done anyway except abort.)
630  *
631  * See the comments below on the test_support() method of this class,
632  * about how the system's support for native recursive mutexes can be
633  * tested at runtime (from version 1.2.0) and when a user program is
634  * compiled (from version 1.2.1). If glib >= 2.32 is installed, it
635  * can be assumed that native recursive mutexes are available, as glib
636  * >= 2.32 also uses them.
637  */
638 
639 class RecMutex {
640  pthread_mutex_t pthr_mutex;
641 
642  // mutexes cannot be copied
643  RecMutex(const RecMutex&);
644  RecMutex& operator=(const RecMutex&);
645 public:
646  class Lock;
647  class TrackLock;
648 
649 /**
650  * Locks the mutex and acquires ownership. Blocks if already locked
651  * until it becomes free, unless the calling thread already holds the
652  * lock, in which case it increments the lock count and returns
653  * immediately. It is not a cancellation point. It does not throw.
654  * It is thread safe.
655  * @return 0 if successful, otherwise the pthread mutex error number.
656  * @note With this library implementation, the only pthread error
657  * number which could be returned by this method is EAGAIN, which it
658  * would do if the maximum recursive lock count for this mutex has
659  * been reached. Usually this number is at or around INT_MAX and
660  * hence the return value is usually not worth checking for except
661  * during debugging.
662  */
663  int lock() {return pthread_mutex_lock(&pthr_mutex);}
664 
665 /**
666  * Tries to lock the mutex and acquire ownership, but returns
667  * immediately if it is already locked with value EBUSY unless the
668  * calling thread already holds the lock, in which case it returns
669  * normally and increments the lock count. It is not a cancellation
670  * point. It does not throw. It is thread safe.
671  * @return 0 if successful, otherwise EBUSY or other pthread mutex
672  * error number.
673  * @note With this library implementation, apart from EBUSY, the only
674  * other pthread error number which could be returned by this method
675  * is EAGAIN, which it would do if the maximum recursive lock count
676  * for this mutex has been reached. Usually this number is at or
677  * around INT_MAX and hence an EAGAIN error return value is usually
678  * not worth checking for except during debugging.
679  */
680  int trylock() {return pthread_mutex_trylock(&pthr_mutex);}
681 
682 /**
683  * Unlocks a locked mutex owned by the calling thread and either
684  * relinquishes ownership (if the mutex has not been recursively
685  * locked) or decrements the lock count (if it has). It is not a
686  * cancellation point. It does not throw. It must be called by the
687  * thread which owns the mutex.
688  * @return 0 if successful, otherwise the pthread mutex error number.
689  * @note With this library implementation, the only pthread error
690  * number which could be returned by this method is EPERM because the
691  * calling thread does not own the mutex (however POSIX does not
692  * require that return value in that case and hence the return value
693  * is usually not worth checking for except during debugging).
694  */
695  int unlock() {return pthread_mutex_unlock(&pthr_mutex);}
696 
697 /**
698  * Indicates whether the system supports recursive mutexes. This
699  * method does not throw. It is thread safe.
700  * @return 0 if the system supports recursive mutexes, -1 if it does
701  * not support recursive mutexes and 1 if pthread has run out of
702  * sufficient resources to test this at run time (in which case any
703  * attempt to create mutexes or start threads is likely to fail).
704  * Practically all recent linux and BSD distributions will support
705  * them, and this function would normally return 0. If it does not,
706  * it is still possible to use GStaticRecMutex objects (for which
707  * purpose see Cgu::Thread::GrecmutexLock).
708  *
709  * @note This method exists as from version 1.2.0, but from version
710  * 1.2.1, the header file <c++-gtk-utils/cgu_config.h> defines the
711  * symbol CGU_HAVE_RECURSIVE_MUTEX if native recursive mutexes were
712  * found to be supported when this library was compiled. Programs
713  * using this library can therefore test for that symbol with the
714  * pre-processor for conditional compilation purposes from version
715  * 1.2.1 onwards (so that the program can, for example, be compiled to
716  * use GStaticRecMutex if that symbol is not defined). However, if
717  * the library was cross-compiled from one architecture to another,
718  * that symbol may not be defined even though the target architecture
719  * does, in fact, support them at program runtime. In other words, if
720  * CGU_HAVE_RECURSIVE_MUTEX is defined then this method will always
721  * return 0; but in the event of cross-compilation of the library this
722  * method (which provides a separate runtime test) might return 0,
723  * correctly showing support, even when CGU_HAVE_RECURSIVE_MUTEX is
724  * not defined.
725  *
726  * @note If glib >= 2.32 is installed, it can be assumed that native
727  * recursive mutexes are available, as glib >= 2.32 also uses them.
728  *
729  * Since 1.2.0
730  */
731  static int test_support();
732 
733 /**
734  * Initialises the pthread mutex. It is not a cancellation point.
735  * @exception Cgu::Thread::RecMutexError Throws this exception if the
736  * system does not support recursive mutexes. (If this has been
737  * checked beforehand, say by calling test_support(), there should be
738  * no need to check for this exception.)
739  * @exception Cgu::Thread::MutexError Throws this exception if
740  * initialization of the mutex fails, except because the system does
741  * not support recursive mutexes. (It is often not worth checking for
742  * MutexError, as it means either memory is exhausted or pthread has
743  * run out of other resources to create new mutexes.)
744  */
745  RecMutex();
746 
747 /**
748  * Destroys the pthread mutex. It is not a cancellation point. It
749  * does not throw. Destroying a mutex which is currently locked
750  * results in undefined behavior.
751  */
752  ~RecMutex() {pthread_mutex_destroy(&pthr_mutex);}
753 
754 /* Only has effect if --with-glib-memory-slices-compat or
755  * --with-glib-memory-slices-no-compat option picked */
757 };
758 
759 /**
760  * @class RecMutex::Lock mutex.h c++-gtk-utils/mutex.h
761  * @brief A scoped locking class for exception safe RecMutex locking.
762  * @sa Thread::RecMutex Thread::RecMutex::TrackLock Thread::Thread
763  */
764 
766  RecMutex& mutex;
767 
768  // locks cannot be copied
769  Lock(const RecMutex::Lock&);
770  RecMutex::Lock& operator=(const RecMutex::Lock&);
771 public:
772 
773 /**
774  * This calls RecMutex::lock(), and so locks the mutex and reacquires
775  * ownership. It blocks if the mutex is already locked until the
776  * mutex becomes free, unless the calling thread already holds the
777  * lock, in which case it increments the lock count and returns
778  * immediately. This method should normally only be called if a
779  * previous call has been made to RecMutex::Lock::unlock() (that is,
780  * where the thread owning the RecMutex::Lock object has temporarily
781  * allowed another thread to take the mutex concerned if it is not
782  * recursively locked). It is not a cancellation point. It does not
783  * throw.
784  * @return 0 if successful, otherwise the pthread mutex error number.
785  * @note With this library implementation, the only pthread error
786  * number which could be returned by this method is EAGAIN, which it
787  * would do if the maximum recursive lock count for the particular
788  * mutex in question has been reached. Usually this number is at or
789  * around INT_MAX and hence the return value is usually not worth
790  * checking for except during debugging.
791  * @sa RecMutex::TrackLock.
792  */
793  int lock() {return mutex.lock();}
794 
795 /**
796  * This calls RecMutex::trylock(), and so tries to lock the mutex and
797  * reacquire ownership, but returns immediately if it is already
798  * locked with value EBUSY unless the calling thread already holds the
799  * lock, in which case it returns normally and increments the lock
800  * count. This method should normally only be called if a previous
801  * call has been made to RecMutex::Lock::unlock() (that is, where the
802  * thread owning the RecMutex::Lock object has temporarily allowed
803  * another thread to take the mutex concerned if it is not recursively
804  * locked). It is not a cancellation point. It does not throw.
805  * @return 0 if successful, otherwise EBUSY or other pthread mutex
806  * error number.
807  * @note With this library implementation, apart from EBUSY, the only
808  * other pthread error number which could be returned by this method
809  * is EAGAIN, which it would do if the maximum recursive lock count
810  * for the particular mutex in question has been reached. Usually
811  * this number is at or around INT_MAX and hence an EAGAIN error
812  * return value is usually not worth checking for except during
813  * debugging.
814  * @sa RecMutex::TrackLock.
815  */
816  int trylock() {return mutex.trylock();}
817 
818 /**
819  * This calls RecMutex::unlock() and so unlocks a locked mutex owned
820  * by the calling thread, so temporarily allowing another thread to
821  * take the mutex if the mutex has not been recursively locked, or if
822  * it has been recursively locked decrements the lock count, so
823  * temporarily relinquishing ownership. This method should normally
824  * only be called if it is to be followed by a call to
825  * RecMutex::Lock::lock() or a successful call to
826  * RecMutex::Lock::trylock() before the RecMutex::Lock object
827  * concerned goes out of scope (otherwise RecMutex::Lock's destructor
828  * will attempt to decrement the lock count of a mutex which already
829  * has a lock count of 0 or which another thread has by then taken
830  * ownership or leave the lock count in an unbalanced condition -
831  * RecMutex::Lock objects do not maintain state). See
832  * RecMutex::TrackLock::unlock() for a safe version of this method.
833  * It is not a cancellation point. It does not throw.
834  * @return 0 if successful, otherwise the pthread mutex error number.
835  * @note With this library implementation, the only pthread error
836  * number which could be returned by this method is EPERM because the
837  * calling thread does not own the particular mutex in question
838  * (however POSIX does not require that return value in that case and
839  * hence the return value is usually not worth checking for except
840  * during debugging).
841  * @sa RecMutex::TrackLock.
842  */
843  int unlock() {return mutex.unlock();}
844 
845 /**
846  * This constructor locks the mutex passed to it. See
847  * RecMutex::lock() for a description of the outcomes. It is not a
848  * cancellation point.
849  * @param mutex_ The mutex to be locked.
850  * @exception Cgu::Thread::RecMutexError Throws this exception if
851  * initialization of the mutex fails because the maximum recursive
852  * lock count for the particular mutex in question has been reached.
853  * Usually this number is at or around INT_MAX so it is not usually
854  * useful to check for it except during debugging.
855  */
856  Lock(RecMutex& mutex_): mutex(mutex_) {if (mutex.lock()) throw RecMutexError();}
857 
858 /**
859  * This constructor takes an already locked mutex (say as a result of
860  * RecMutex::trylock()), and takes ownership of it. It is not a
861  * cancellation point. It does not throw.
862  * @param mutex_ The mutex to be managed by this object.
863  * @param tag Pass the Cgu::Thread::locked enum tag to this parameter.
864  */
865  Lock(RecMutex& mutex_, Locked tag): mutex(mutex_) {}
866 
867 /**
868  * The destructor unlocks the owned mutex. See RecMutex::unlock() for
869  * a description of the outcomes. It is not a cancellation point. It
870  * does not throw.
871  */
872  ~Lock() {mutex.unlock();}
873 
874 /* Only has effect if --with-glib-memory-slices-compat or
875  * --with-glib-memory-slices-no-compat option picked */
877 };
878 
879 /**
880  * @class RecMutex::TrackLock mutex.h c++-gtk-utils/mutex.h
881  * @brief A scoped locking class for exception safe RecMutex locking
882  * which tracks the status of its mutex.
883  * @sa Thread::RecMutex Thread::RecMutex::Lock Thread::Thread
884  *
885  * This class is similar to a RecMutex::Lock object, except that it
886  * tracks whether the mutex it manages is locked by the thread
887  * creating the RecMutex::TrackLock object with respect to the
888  * particular locking operation to be governed by the object (provided
889  * that, while the RecMutex::TrackLock object exists, the thread
890  * creating it only accesses the mutex with respect that particular
891  * operation through that object). This enables
892  * RecMutex::TrackLock::unlock() to be used without it being followed
893  * later by a call to RecMutex::TrackLock::lock() or a successful call
894  * to RecMutex::TrackLock::trylock(), and also permits locking to be
895  * deferred until after construction of the lock object. Note that
896  * only one thread may call the methods of any one RecMutex::TrackLock
897  * object, including causing its destructor to be invoked.
898  */
899 
901  RecMutex& mutex;
902  bool owner;
903 
904  // locks cannot be copied
906  RecMutex::TrackLock& operator=(const RecMutex::TrackLock&);
907 public:
908 
909 /**
910  * This calls RecMutex::lock(), and so locks the mutex and acquires
911  * ownership. It blocks if the mutex is already locked until the
912  * mutex becomes free, unless the calling thread already holds the
913  * lock, in which case it increments the lock count and returns
914  * immediately. This method should normally only be called if a
915  * previous call has been made to RecMutex::TrackLock::unlock() or
916  * this RecMutex::TrackLock object has been constructed with the
917  * Thread::defer enum tag. It is not a cancellation point. It does
918  * not throw.
919  * @return 0 if successful, otherwise the pthread mutex error number.
920  * @note With this library implementation, the only pthread error
921  * number which could be returned by this method is EAGAIN, which it
922  * would do if the maximum recursive lock count for the particular
923  * mutex in question has been reached. Usually this number is at or
924  * around INT_MAX and hence the return value is usually not worth
925  * checking for except during debugging.
926  *
927  * Since 1.2.1
928  */
929  int lock() {int ret = mutex.lock(); if (!owner) owner = !ret; return ret;}
930 
931 /**
932  * This calls RecMutex::trylock(), and so tries to lock the mutex and
933  * acquire ownership, but returns immediately if it is already locked
934  * with value EBUSY unless the calling thread already holds the lock,
935  * in which case it returns normally and increments the lock count.
936  * This method should normally only be called if a previous call has
937  * been made to RecMutex::TrackLock::unlock() or this
938  * RecMutex::TrackLock object has been constructed with the
939  * Thread::defer enum tag. It is not a cancellation point. It does
940  * not throw.
941  * @return 0 if successful, otherwise EBUSY or other pthread mutex
942  * error number.
943  * @note With this library implementation, apart from EBUSY, the only
944  * other pthread error number which could be returned by this method
945  * is EAGAIN, which it would do if the maximum recursive lock count
946  * for the particular mutex in question has been reached. Usually
947  * this number is at or around INT_MAX and hence an EAGAIN error
948  * return value is usually not worth checking for except during
949  * debugging.
950  *
951  * Since 1.2.1
952  */
953  int trylock() {int ret = mutex.trylock(); if (!owner) owner = !ret; return ret;}
954 
955 /**
956  * This calls RecMutex::unlock(), and so unlocks a locked mutex owned
957  * by the calling thread, or decrements the lock count (if it has been
958  * recursively locked). It will cause is_owner() to return false
959  * unless a subsequent call is made to lock() or a subsequent
960  * successful call is made to trylock(). It is not a cancellation
961  * point. It does not throw.
962  * @return 0 if successful, otherwise the pthread mutex error number.
963  * @note With this library implementation, the only pthread error
964  * number which could be returned by this method is EPERM because the
965  * calling thread does not own the particular mutex in question
966  * (however POSIX does not require that return value in that case and
967  * hence the return value is usually not worth checking for except
968  * during debugging).
969  *
970  * Since 1.2.1
971  */
972  int unlock() {int ret = mutex.unlock(); if (owner) owner = ret; return ret;}
973 
974 /**
975  * Indicates whether the mutex managed by this Mutex::TrackLock object
976  * is locked by it (whether originally or recursively) and so owned by
977  * it. It does not throw.
978  * @return true if the mutex is owned by this object, otherwise false.
979  *
980  * Since 1.2.1
981  */
982  bool is_owner() const {return owner;}
983 
984 /**
985  * This constructor locks the mutex passed to it. See
986  * RecMutex::lock() for a description of the outcomes. It is not a
987  * cancellation point. It does not throw.
988  * @param mutex_ The mutex to be locked.
989  * @exception Cgu::Thread::RecMutexError Throws this exception if
990  * initialization of the mutex fails because the maximum recursive
991  * lock count for the particular mutex in question has been reached.
992  * Usually this number is at or around INT_MAX so it is not usually
993  * useful to check for it except during debugging.
994  *
995  * Since 1.2.1
996  */
997  TrackLock(RecMutex& mutex_): mutex(mutex_), owner(true) {if (mutex.lock()) throw RecMutexError();}
998 
999 /**
1000  * This constructor takes an already locked mutex (say as a result of
1001  * RecMutex::trylock()), and takes ownership of it. It is not a
1002  * cancellation point. It does not throw.
1003  * @param mutex_ The mutex to be managed by this object.
1004  * @param tag Pass the Cgu::Thread::locked enum tag to this parameter.
1005  *
1006  * Since 1.2.1
1007  */
1008  TrackLock(RecMutex& mutex_, Locked tag): mutex(mutex_), owner(true) {}
1009 
1010 /**
1011  * This constructor defers locking of the mutex (and so taking
1012  * ownership of it) until an explicit call to lock() or trylock() is
1013  * made. It is not a cancellation point. It does not throw.
1014  * @param mutex_ The mutex to be managed by this object.
1015  * @param tag Pass the Cgu::Thread::defer enum tag to this parameter.
1016  *
1017  * Since 1.2.1
1018  */
1019  TrackLock(RecMutex& mutex_, DeferLock tag): mutex(mutex_), owner(false) {}
1020 
1021 /**
1022  * The destructor unlocks the managed mutex if it is owned by this
1023  * RecMutex::TrackLock object. See RecMutex::unlock() for a
1024  * description of the outcomes if it is so owned. It is not a
1025  * cancellation point. It does not throw.
1026  *
1027  * Since 1.2.1
1028  */
1029  ~TrackLock() {if (owner) mutex.unlock();}
1030 
1031 /* Only has effect if --with-glib-memory-slices-compat or
1032  * --with-glib-memory-slices-no-compat option picked */
1034 };
1035 
1036 // we don't want glib's warnings about GStaticRecMutex
1037 #ifdef CGU_USE_DIAGNOSTIC_PRAGMAS
1038 #pragma GCC diagnostic push
1039 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1040 #endif
1041 
1042 /**
1043  * @class GrecmutexLock mutex.h c++-gtk-utils/mutex.h
1044  * @brief A scoped locking class for exception safe locking of
1045  * GStaticRecMutex objects.
1046  * @details This class is mainly intended for use where the native
1047  * pthreads implementation does not support recursive mutexes so
1048  * Cgu::Thread::RecMutex and Cgu::Thread::RecMutex::Lock cannot be
1049  * used.
1050  *
1051  * It should be noted that this class is for use with GStaticRecMutex
1052  * objects, and not the GRecMutex objects available in glib from glib
1053  * version 2.32. If glib >= 2.32 is installed, it can be assumed that
1054  * native recursive mutexes and so Cgu::Thread::RecMutex are
1055  * available, as glib >= 2.32 also uses native recursive mutexes.
1056  */
1058  GStaticRecMutex& mutex;
1059  GrecmutexLock(const GrecmutexLock&);
1060  GrecmutexLock& operator=(const GrecmutexLock&);
1061 public:
1062 /**
1063  * This method provides access to the GStaticRecMutex object locked by
1064  * this GrecmutexLock object. It does not throw. It is thread safe.
1065  * @return A pointer to the GStaticRecMutex object.
1066  *
1067  * Since 1.2.0
1068  */
1069  GStaticRecMutex* get() const {return &mutex;}
1070 /**
1071  * This constructor locks the mutex and acquires ownership, and blocks
1072  * if it is already locked until it becomes free, unless the
1073  * constructing thread already holds the lock, in which case it
1074  * increments the lock count and returns immediately. It is not a
1075  * cancellation point. It does not throw.
1076  * @param mutex_ The mutex to be locked.
1077  *
1078  * Since 1.2.0
1079  */
1080  // this is not inline, so we can apply GLIB_VERSION_MIN_REQUIRED
1081  GrecmutexLock(GStaticRecMutex& mutex_);
1082 
1083 /**
1084  * This constructor takes an already locked mutex (say as a result of
1085  * g_static_rec_mutex_trylock()), and takes ownership of it. It is not a
1086  * cancellation point. It does not throw.
1087  * @param mutex_ The mutex to be managed by this object.
1088  * @param tag Pass the Cgu::Thread::locked enum tag to this parameter.
1089  *
1090  * Since 1.2.0
1091  */
1092  GrecmutexLock(GStaticRecMutex& mutex_, Locked tag): mutex(mutex_) {}
1093 
1094 /**
1095  * The destructor unlocks the owned mutex, and either relinquishes
1096  * ownership (if the mutex has not been recursively locked) or
1097  * decrements the lock count (if it has). It is not a cancellation
1098  * point. It does not throw.
1099  *
1100  * Since 1.2.0
1101  */
1102  // this is not inline, so we can apply GLIB_VERSION_MIN_REQUIRED
1103  ~GrecmutexLock();
1104 
1105 /* Only has effect if --with-glib-memory-slices-compat or
1106  * --with-glib-memory-slices-no-compat option picked */
1108 };
1109 
1110 #ifdef CGU_USE_DIAGNOSTIC_PRAGMAS
1111 #pragma GCC diagnostic pop
1112 #endif
1113 
1114 } // namespace Thread
1115 
1116 } // namespace Cgu
1117 
1118 #endif
Cgu::Thread::Cond::get_abs_time
static void get_abs_time(timespec &ts, unsigned int millisec)
Cgu::Thread::Cond::timed_wait
int timed_wait(Mutex &mutex, const timespec &abs_time)
Definition: mutex.h:528
Cgu::Thread::Mutex::TrackLock::unlock
int unlock()
Definition: mutex.h:362
Cgu::Thread::RecMutex::Lock::unlock
int unlock()
Definition: mutex.h:843
Cgu
Definition: application.h:45
Cgu::Thread::RecMutex::TrackLock::~TrackLock
~TrackLock()
Definition: mutex.h:1029
Cgu::Thread::Cond::wait
int wait(Mutex::Lock &lock)
Definition: mutex.h:483
Cgu::Thread::Mutex::TrackLock::trylock
int trylock()
Definition: mutex.h:345
Cgu::Thread::Cond::Cond
Cond()
Cgu::Thread::locked
@ locked
Definition: mutex.h:181
Cgu::Thread::Mutex::Lock::Lock
Lock(Mutex &mutex_, Locked tag)
Definition: mutex.h:271
Cgu::Thread::Cond::have_monotonic_clock
static bool have_monotonic_clock()
Cgu::Thread::RecMutex
A wrapper class for pthread mutexes which provides a recursive mutex.
Definition: mutex.h:639
Cgu::Thread::Cond::broadcast
int broadcast()
Definition: mutex.h:451
Cgu::Thread::RecMutex::Lock
A scoped locking class for exception safe RecMutex locking.
Definition: mutex.h:765
Cgu::Thread::Mutex::TrackLock::lock
int lock()
Definition: mutex.h:330
Cgu::Thread::Cond::wait
int wait(Mutex::TrackLock &lock)
Definition: mutex.h:492
Cgu::Thread::RecMutex::trylock
int trylock()
Definition: mutex.h:680
Cgu::Thread::Mutex::Lock::~Lock
~Lock()
Definition: mutex.h:277
Cgu::Thread::RecMutex::test_support
static int test_support()
Cgu::Thread::CondError::what
virtual const char * what() const
Definition: mutex.h:67
Cgu::Thread::Cond::timed_wait
int timed_wait(Mutex::TrackLock &lock, const timespec &abs_time)
Definition: mutex.h:549
Cgu::Thread::Cond
A wrapper class for pthread condition variables.
Definition: mutex.h:424
Cgu::Thread::RecMutex::lock
int lock()
Definition: mutex.h:663
Cgu::Thread::Mutex::Lock::trylock
int trylock()
Definition: mutex.h:233
Cgu::Thread::Cond::timed_wait
int timed_wait(Mutex::Lock &lock, const timespec &abs_time)
Definition: mutex.h:538
Cgu::Thread::Cond::signal
int signal()
Definition: mutex.h:440
Cgu::Thread::Mutex::TrackLock::TrackLock
TrackLock(Mutex &mutex_, DeferLock tag)
Definition: mutex.h:403
Cgu::Thread::Mutex::TrackLock::~TrackLock
~TrackLock()
Definition: mutex.h:411
Cgu::Thread::Mutex::~Mutex
~Mutex()
Definition: mutex.h:172
Cgu::Thread::Cond::~Cond
~Cond(void)
Definition: mutex.h:607
Cgu::Thread::GrecmutexLock::GrecmutexLock
GrecmutexLock(GStaticRecMutex &mutex_, Locked tag)
Definition: mutex.h:1092
Cgu::Thread::RecMutex::Lock::Lock
Lock(RecMutex &mutex_)
Definition: mutex.h:856
Cgu::Thread::Mutex::TrackLock
A scoped locking class for exception safe Mutex locking which tracks the status of its mutex.
Definition: mutex.h:303
Cgu::Thread::Cond::wait
int wait(Mutex &mutex)
Definition: mutex.h:476
Cgu::Thread::RecMutex::TrackLock::trylock
int trylock()
Definition: mutex.h:953
Cgu::Thread::GrecmutexLock::~GrecmutexLock
~GrecmutexLock()
Cgu::Thread::RecMutex::TrackLock::unlock
int unlock()
Definition: mutex.h:972
Cgu::Thread::RecMutex::Lock::lock
int lock()
Definition: mutex.h:793
Cgu::Thread::RecMutex::TrackLock
A scoped locking class for exception safe RecMutex locking which tracks the status of its mutex.
Definition: mutex.h:900
Cgu::Thread::RecMutex::TrackLock::TrackLock
TrackLock(RecMutex &mutex_, Locked tag)
Definition: mutex.h:1008
Cgu::Thread::Mutex::Lock::unlock
int unlock()
Definition: mutex.h:255
Cgu::Thread::RecMutex::Lock::trylock
int trylock()
Definition: mutex.h:816
Cgu::Thread::RecMutex::TrackLock::lock
int lock()
Definition: mutex.h:929
Cgu::Thread::defer
@ defer
Definition: mutex.h:184
Cgu::Thread::Mutex::TrackLock::TrackLock
TrackLock(Mutex &mutex_, Locked tag)
Definition: mutex.h:392
Cgu::Thread::RecMutex::unlock
int unlock()
Definition: mutex.h:695
Cgu::Thread::Mutex::TrackLock::is_owner
bool is_owner() const
Definition: mutex.h:372
Cgu::Thread::DeferLock
DeferLock
Definition: mutex.h:184
Cgu::Thread::RecMutex::TrackLock::is_owner
bool is_owner() const
Definition: mutex.h:982
Cgu::Thread::RecMutex::Lock::Lock
Lock(RecMutex &mutex_, Locked tag)
Definition: mutex.h:865
Cgu::Thread::RecMutex::TrackLock::TrackLock
TrackLock(RecMutex &mutex_, DeferLock tag)
Definition: mutex.h:1019
Cgu::Thread::Mutex::lock
int lock()
Definition: mutex.h:132
Cgu::Thread::Mutex::Lock
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:192
Cgu::Thread::GrecmutexLock::get
GStaticRecMutex * get() const
Definition: mutex.h:1069
Cgu::Thread::Mutex::TrackLock::TrackLock
TrackLock(Mutex &mutex_)
Definition: mutex.h:381
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::Thread::GrecmutexLock
A scoped locking class for exception safe locking of GStaticRecMutex objects.
Definition: mutex.h:1057
Cgu::Thread::Locked
Locked
Definition: mutex.h:181
Cgu::Thread::RecMutexError::what
virtual const char * what() const
Definition: mutex.h:86
Cgu::Thread::RecMutexError
Definition: mutex.h:85
Cgu::Thread::MutexError::what
virtual const char * what() const
Definition: mutex.h:82
Cgu::Thread::RecMutex::RecMutex
RecMutex()
Cgu::Thread::RecMutex::~RecMutex
~RecMutex()
Definition: mutex.h:752
Cgu::Thread::CondError
Definition: mutex.h:66
Cgu::Thread::Mutex::unlock
int unlock()
Definition: mutex.h:155
Cgu::Thread::Mutex::trylock
int trylock()
Definition: mutex.h:142
Cgu::Thread::MutexError
Definition: mutex.h:81
Cgu::Thread::Mutex::Lock::Lock
Lock(Mutex &mutex_)
Definition: mutex.h:262
Cgu::Thread::Mutex::Mutex
Mutex()
Definition: mutex.h:164
Cgu::Thread::Mutex
A wrapper class for pthread mutexes.
Definition: mutex.h:109
cgu_config.h
Cgu::Thread::RecMutex::Lock::~Lock
~Lock()
Definition: mutex.h:872
Cgu::Thread::Mutex::Lock::lock
int lock()
Definition: mutex.h:218
Cgu::Thread::RecMutex::TrackLock::TrackLock
TrackLock(RecMutex &mutex_)
Definition: mutex.h:997