1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Mutex Abstraction Class Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__MUTEX_PTHREADS_H_
28 #define __ETL__MUTEX_PTHREADS_H_
30 /* === H E A D E R S ======================================================= */
40 /* === M A C R O S ========================================================= */
42 /* === C L A S S E S & S T R U C T S ======================================= */
55 pthread_mutexattr_t attr;
56 pthread_mutexattr_init(&attr);
57 //#ifdef PTHREAD_PRIO_INHERIT
58 //pthread_mutexattr_setprioceiling(&attr,PTHREAD_PRIO_INHERIT);
60 #ifdef PTHREAD_MUTEX_RECURSIVE
61 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
63 pthread_mutex_init(&mtx,&attr);
64 pthread_mutexattr_destroy(&attr);
70 { pthread_mutex_destroy(&mtx); }
73 //! Exception-safe mutex lock class
78 lock(mutex &x):_mtx(&x) { _mtx->lock_mutex(); }
79 ~lock() { _mtx->unlock_mutex(); }
80 mutex &get() { return *_mtx; }
85 if(!locker || locker!=pthread_self())
87 pthread_mutex_lock(&mtx);
88 locker=pthread_self();
95 bool try_lock_mutex(void)
96 { return !(bool) pthread_mutex_trylock(&mtx); }
98 void unlock_mutex(void)
105 pthread_mutex_unlock(&mtx);
112 /* === E X T E R N S ======================================================= */
114 /* === E N D =============================================================== */