b7dda75d010d1f57f4efe5681a3d1670c97ed836
[synfig.git] / ETL / tags / stable / ETL / _mutex_win32.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__MUTEX_WIN32_H_
28 #define __ETL__MUTEX_WIN32_H_
29
30 /* === H E A D E R S ======================================================= */
31
32 #include <windows.h>
33 // extern HANDLE CreateMutex(NULL, FALSE, NULL);
34 // extern CloseHandle(handle);
35 // extern WaitForSingleObject(handle, INFINITE);
36 // extern ReleaseMutex(handle);
37
38 /* === M A C R O S ========================================================= */
39
40
41 /* === C L A S S E S & S T R U C T S ======================================= */
42
43 _ETL_BEGIN_NAMESPACE
44
45 class mutex
46 {
47         HANDLE handle;
48 public:
49
50         mutex()
51         { handle = CreateMutex(NULL, FALSE, NULL); }
52
53         ~mutex()
54         { CloseHandle(handle); }
55
56         //! Exception-safe mutex lock class
57         class lock
58         {
59                 mutex *_mtx;
60         public:
61                 lock(mutex &x):_mtx(&x) { _mtx->lock_mutex(); }
62                 ~lock() { _mtx->unlock_mutex(); }
63                 mutex &get() { return *_mtx; }
64         };
65
66         void lock_mutex(void)
67         { WaitForSingleObject(handle, INFINITE); }
68
69         bool try_lock_mutex(void)
70         { return WaitForSingleObject(handle, INFINITE)==WAIT_FAILED; }
71
72         void unlock_mutex(void)
73         { ReleaseMutex(handle); }
74 };
75
76 _ETL_END_NAMESPACE
77
78 /* === E X T E R N S ======================================================= */
79
80 /* === E N D =============================================================== */
81
82 #endif