remove \r
[extl.git] / extl / platform_ / windows / synch / basic_mutex.h
blob78283783d17bf61570c03151638db3308e8dcd7c
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: basic_mutex.h
4 * Created: 08.03.23
5 * Updated: 08.11.26
7 * Brief: The Mutex Object
9 * [<Home>]
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
18 #ifndef __cplusplus
19 # error basic_mutex.h need be supported by c++.
20 #endif
22 /* ///////////////////////////////////////////////////////////////////////
23 * Includes
25 #include "prefix.h"
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
38 class basic_mutex
39 : private uncopyable<basic_mutex>
41 /// \name Types
42 /// @{
43 public:
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;
49 /// @}
51 /// \name Members
52 /// @{
53 private:
54 handle_type m_hm;
55 /// @}
57 /// \name Constructors
58 /// @{
59 public:
60 basic_mutex()
61 : m_hm(NULL)
63 explicit_k basic_mutex(bool_type is_owner, psa_type psa = NULL)
64 : m_hm(NULL)
66 create(NULL, is_owner, psa);
68 explicit_k basic_mutex(char_type const* name, bool_type is_owner, psa_type psa = NULL)
69 : m_hm(NULL)
71 create(name, is_owner, psa);
73 ~basic_mutex() EXTL_THROW_0()
75 if(NULL != handle())
77 ::CloseHandle(handle());
78 m_hm = NULL;
81 /// @}
83 /// \name Methods
84 /// @{
85 public:
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"));
91 return hm;
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");
99 if (NULL == handle())
101 m_hm = create_mutex(NULL, is_owner, psa);
102 return (NULL != handle());
104 return e_false_v;
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());
117 return e_false_v;
119 /// release the basic_mutex object
120 bool_type release()
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"));
128 return e_false_v;
130 else return e_true_v;
132 /// @}
134 /// \name Accessors
135 /// @{
136 public:
137 handle_type handle() const { return m_hm; }
138 bool_type is_valid() const
140 return ((handle() != NULL) && (handle() != INVALID_HANDLE_VALUE));
142 /// @}
145 /* ///////////////////////////////////////////////////////////////////////
146 * ::extl::platform::windows namespace
148 EXTL_WINDOWS_END_WHOLE_NAMESPACE
150 /* //////////////////////////////////////////////////////////////////// */
151 #endif /* EXTL_PLATFORM_WINDOWS_SYNCH_BASIC_MUTEX_H */
152 /* //////////////////////////////////////////////////////////////////// */