1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The Mutex Object
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_PLATFORM_WINDOWS_SYNCH_MUTEX_H
13 #define EXTL_PLATFORM_WINDOWS_SYNCH_MUTEX_H
16 * \brief The Mutex Object
19 # error mutex.h need be supported by c++.
22 /* ///////////////////////////////////////////////////////////////////////
26 #include "../../utility/uncopyable.h"
27 #include "../error/error.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl::platform::windows namespace
32 EXTL_WINDOWS_BEGIN_WHOLE_NAMESPACE
36 * \ingroup extl_group_synch
39 : private uncopyable
<mutex
>
44 typedef mutex class_type
;
45 typedef HANDLE handle_type
;
46 typedef e_bool_t bool_type
;
47 typedef LPSECURITY_ATTRIBUTES psa_type
;
48 typedef e_tchar_t char_type
;
57 /// \name Constructors
63 explicit_k
mutex(bool_type is_owner
, psa_type psa
= NULL
)
66 create(NULL
, is_owner
, psa
);
68 explicit_k
mutex(char_type
const* name
, bool_type is_owner
, psa_type psa
= NULL
)
71 create(name
, is_owner
, psa
);
73 virtual ~mutex() EXTL_THROW_0()
77 ::CloseHandle(handle());
86 /// create mutex object - static
87 static handle_type
create_mutex(char_type
const* name
, bool_type is_owner
, psa_type psa
= NULL
)
89 handle_type hm
= ::CreateMutex(psa
, is_owner
, name
);
90 EXTL_ASSERT_THROW(hm
!= NULL
, windows_synch_error("failed to create kernel mutex object"));
93 /// create mutex object
94 bool_type
create(bool_type is_owner
, psa_type psa
= NULL
)
96 EXTL_MESSAGE_ASSERT(NULL
== handle(), "the mutex object has been created");
97 EXTL_ASSERT_THROW(NULL
== handle(), "the mutex object has been created");
101 m_hm
= create_mutex(NULL
, is_owner
, psa
);
102 return (NULL
!= handle());
106 /// create mutex object
107 bool_type
create(char_type
const* name
, bool_type is_owner
, psa_type psa
= NULL
)
109 EXTL_MESSAGE_ASSERT(NULL
== handle(), "the mutex object has been created");
110 EXTL_ASSERT_THROW(NULL
== handle(), "the mutex object has been created");
112 if (NULL
== handle())
114 m_hm
= create_mutex(name
, is_owner
, psa
);
115 return (NULL
!= handle());
119 /// release the mutex object
122 EXTL_MESSAGE_ASSERT(NULL
!= handle(), "the mutex object hasn't been created");
123 EXTL_ASSERT_THROW(NULL
!= handle(), windows_synch_error("the mutex object hasn't been created"));
125 if(NULL
== handle() || !::ReleaseMutex(handle()))
127 EXTL_THROW_E(windows_synch_error("failed to release mutex object"));
130 else return e_true_v
;
137 /// gets object handle
138 operator handle_type() const { return m_hm
; }
139 /// gets object handle
140 handle_type
handle() const { return m_hm
; }
144 /* ///////////////////////////////////////////////////////////////////////
145 * ::extl::platform::windows namespace
147 EXTL_WINDOWS_END_WHOLE_NAMESPACE
149 /* //////////////////////////////////////////////////////////////////// */
150 #endif /* EXTL_PLATFORM_WINDOWS_SYNCH_MUTEX_H */
151 /* //////////////////////////////////////////////////////////////////// */