1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The Mutex Object
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_PLATFORM_WINDOWS_SYNCH_BASIC_MUTEX_H
13 #define EXTL_PLATFORM_WINDOWS_SYNCH_BASIC_MUTEX_H
15 /*!\file basic_mutex.h
16 * \brief The Mutex Object
19 # error basic_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
34 /*!\brief basic_mutex class
36 * \ingroup extl_group_synch
39 : private uncopyable
<basic_mutex
>
44 typedef basic_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
basic_mutex(bool_type is_owner
, psa_type psa
= NULL
)
66 create(NULL
, is_owner
, psa
);
68 explicit_k
basic_mutex(char_type
const* name
, bool_type is_owner
, psa_type psa
= NULL
)
71 create(name
, is_owner
, psa
);
73 ~basic_mutex() EXTL_THROW_0()
77 ::CloseHandle(handle());
86 /// create basic_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 basic_mutex object"));
93 /// create basic_mutex object
94 bool_type
create(bool_type is_owner
, psa_type psa
= NULL
)
96 EXTL_MESSAGE_ASSERT(NULL
== handle(), "the basic_mutex object has been created");
97 EXTL_ASSERT_THROW(NULL
== handle(), "the basic_mutex object has been created");
101 m_hm
= create_mutex(NULL
, is_owner
, psa
);
102 return (NULL
!= handle());
106 /// create basic_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 basic_mutex object has been created");
110 EXTL_ASSERT_THROW(NULL
== handle(), "the basic_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 basic_mutex object
122 EXTL_MESSAGE_ASSERT(NULL
!= handle(), "the basic_mutex object hasn't been created");
123 EXTL_ASSERT_THROW(NULL
!= handle(), windows_synch_error("the basic_mutex object hasn't been created"));
125 if(NULL
== handle() || !::ReleaseMutex(handle()))
127 EXTL_THROW_E(windows_synch_error("failed to release basic_mutex object"));
130 else return e_true_v
;
137 handle_type
handle() const { return m_hm
; }
138 bool_type
is_valid() const
140 return ((handle() != NULL
) && (handle() != INVALID_HANDLE_VALUE
));
145 /* ///////////////////////////////////////////////////////////////////////
146 * ::extl::platform::windows namespace
148 EXTL_WINDOWS_END_WHOLE_NAMESPACE
150 /* //////////////////////////////////////////////////////////////////// */
151 #endif /* EXTL_PLATFORM_WINDOWS_SYNCH_BASIC_MUTEX_H */
152 /* //////////////////////////////////////////////////////////////////// */