remove \r
[extl.git] / extl / platform_ / windows / synch / mutex.h
blobc6f06a80620dd88b2b78ae42f0c35bf007d10daa
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: 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_MUTEX_H
13 #define EXTL_PLATFORM_WINDOWS_SYNCH_MUTEX_H
15 /*!\file mutex.h
16 * \brief The Mutex Object
18 #ifndef __cplusplus
19 # error 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 mutex class
36 * \ingroup extl_group_synch
38 class mutex
39 : private uncopyable<mutex>
41 /// \name Types
42 /// @{
43 public:
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;
49 /// @}
51 /// \name Members
52 /// @{
53 private:
54 handle_type m_hm;
55 /// @}
57 /// \name Constructors
58 /// @{
59 public:
60 mutex()
61 : m_hm(NULL)
63 explicit_k mutex(bool_type is_owner, psa_type psa = NULL)
64 : m_hm(NULL)
66 create(NULL, is_owner, psa);
68 explicit_k mutex(char_type const* name, bool_type is_owner, psa_type psa = NULL)
69 : m_hm(NULL)
71 create(name, is_owner, psa);
73 virtual ~mutex() EXTL_THROW_0()
75 if(NULL != handle())
77 ::CloseHandle(handle());
78 m_hm = NULL;
81 /// @}
83 /// \name Methods
84 /// @{
85 public:
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"));
91 return hm;
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");
99 if (NULL == handle())
101 m_hm = create_mutex(NULL, is_owner, psa);
102 return (NULL != handle());
104 return e_false_v;
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());
117 return e_false_v;
119 /// release the mutex object
120 bool_type release()
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"));
128 return e_false_v;
130 else return e_true_v;
132 /// @}
134 /// \name Accessors
135 /// @{
136 public:
137 /// gets object handle
138 operator handle_type() const { return m_hm; }
139 /// gets object handle
140 handle_type handle() const { return m_hm; }
141 /// @}
144 /* ///////////////////////////////////////////////////////////////////////
145 * ::extl::platform::windows namespace
147 EXTL_WINDOWS_END_WHOLE_NAMESPACE
149 /* //////////////////////////////////////////////////////////////////// */
150 #endif /* EXTL_PLATFORM_WINDOWS_SYNCH_MUTEX_H */
151 /* //////////////////////////////////////////////////////////////////// */