1 /* === S Y N F I G ========================================================= */
2 /*!	\file mutex.h
3 **	\brief Template Header
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **	This package is free software; you can redistribute it and/or
11 **	modify it under the terms of the GNU General Public License as
12 **	published by the Free Software Foundation; either version 2 of
13 **	the License, or (at your option) any later version.
14 **
15 **	This package is distributed in the hope that it will be useful,
16 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **	General Public License for more details.
19 **	\endlegal
20 */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_MUTEX_H
26 #define __SYNFIG_MUTEX_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 /* === M A C R O S ========================================================= */
31 
32 /* === T Y P E D E F S ===================================================== */
33 
34 /* === C L A S S E S & S T R U C T S ======================================= */
35 
36 namespace synfig {
37 
38 class RecMutex;
39 
40 class Mutex
41 {
42 	friend class RecMutex;
43 
44 protected:
45 	void* blackbox;
46 
47 public:
48 
49 	class Lock
50 	{
51 		Mutex& mutex;
52 	public:
Lock(Mutex & x)53 		Lock(Mutex& x):mutex(x) { mutex.lock(); }
~Lock()54 		~Lock() { mutex.unlock(); }
55 	};
56 
57 	Mutex();
58 	~Mutex();
59 
60 	void lock();
61 	void unlock();
62 	bool try_lock();
63 	bool is_locked();
64 
65 private:
66 	//! Non-copyable
67 	Mutex(const Mutex&);
68 
69 	//! Non-assignable
70 	void operator=(const Mutex&);
71 };
72 
73 class RecMutex : public Mutex
74 {
75 public:
76 	RecMutex();
77 
78 	void unlock_all();
79 };
80 
81 class RWLock
82 {
83 	void* blackbox;
84 
85 public:
86 
87 	class ReaderLock
88 	{
89 		RWLock& rw_lock;
90 	public:
ReaderLock(RWLock & x)91 		ReaderLock(RWLock& x):rw_lock(x) { rw_lock.reader_lock(); }
~ReaderLock()92 		~ReaderLock() { rw_lock.reader_unlock(); }
93 	};
94 	class WriterLock
95 	{
96 		RWLock& rw_lock;
97 	public:
WriterLock(RWLock & x)98 		WriterLock(RWLock& x):rw_lock(x) { rw_lock.writer_lock(); }
~WriterLock()99 		~WriterLock() { rw_lock.writer_unlock(); }
100 	};
101 
102 	RWLock();
103 	~RWLock();
104 
105 	void reader_lock();
106 	void reader_unlock();
107 	bool reader_trylock();
108 
109 	void writer_lock();
110 	void writer_unlock();
111 	bool writer_trylock();
112 };
113 
114 }; // END of namespace synfig
115 
116 /* === E N D =============================================================== */
117 
118 #endif
119