Release ETL_@VERSION_MAJ@_@VERSION_MIN@_@VERSION_REV@
[synfig.git] / ETL / tags / ETL_@VERSION_MAJ@_@VERSION_MIN@_@VERSION_REV@ / ETL / _rwlock.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Mutex Abstraction Class Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
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.
12 **
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.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL__RWLOCK_H_
28 #define __ETL__RWLOCK_H_
29
30 /* === H E A D E R S ======================================================= */
31
32 /* === M A C R O S ========================================================= */
33
34 /* === C L A S S E S & S T R U C T S ======================================= */
35
36 _ETL_BEGIN_NAMESPACE
37
38 class read_write_lock : private Mutex
39 {
40 public:
41
42         read_write_lock()
43         {  }
44
45         ~read_write_lock()
46         {  }
47
48         //! Exception-safe read-lock class
49         class read_lock
50         {
51                 read_write_lock *_mtx;
52         public:
53                 read_lock(read_write_lock &x):_mtx(&x) { _mtx->lock_read(); }
54                 ~read_lock() { _mtx->unlock_read(); }
55                 read_write_lock &get() { return *_mtx; }
56         };
57
58         //! Exception-safe write-lock class
59         class write_lock
60         {
61                 read_write_lock *_mtx;
62         public:
63                 write_lock(read_write_lock &x):_mtx(&x) { _mtx->lock_write(); }
64                 ~read_lock() { _mtx->unlock_write(); }
65                 read_write_lock &get() { return *_mtx; }
66         };
67
68         void lock_read(void)
69         { lock_mutex(); }
70
71         void lock_write(void)
72         { lock_mutex(); }
73
74         bool try_lock_read(void)
75         { return try_lock_mutex(); }
76
77         bool try_lock_write(void)
78         { return try_lock_mutex(); }
79
80         void unlock_write(void)
81         { unlock_mutex(); }
82
83         void unlock_read(void)
84         { unlock_mutex(); }
85 };
86
87 _ETL_END_NAMESPACE
88
89 /* === E X T E R N S ======================================================= */
90
91 /* === E N D =============================================================== */
92
93 #endif