remove \r
[extl.git] / extl / win / synch / semaphore.h
blob2f50044db4e34436b9563fe46c734d5293ab8522
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: semaphore.h
4 * Created: 08.03.23
5 * Updated: 08.05.05
7 * Brief: semaphore object class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_WIN_SYNCH_SEMAPHORE_H
13 #define EXTL_WIN_SYNCH_SEMAPHORE_H
15 /*!\file semaphore.h
16 * \brief semaphore object class
18 #ifndef __cplusplus
19 # error semaphore.h need be supported by c++.
20 #endif
22 /* ///////////////////////////////////////////////////////////////////////
23 * Includes
25 #include "../win.h"
26 #include "../../utility/uncopyable.h"
28 #if defined(EXTL_EXCEPTION_SUPPORT) && \
29 defined(EXTL_EXCEPTION_ENABLE)
30 # include "../error/win_synch_error.h"
31 #endif
33 /* ///////////////////////////////////////////////////////////////////////
34 * ::extl::platform::win namespace
36 EXTL_WIN_BEGIN_WHOLE_NAMESPACE
38 /*!\brief semaphore class
39 * \ingroup extl_group_synch
41 class semaphore : private uncopyable<semaphore>
43 /// \name Types
44 /// @{
45 public:
46 typedef semaphore class_type;
47 typedef HANDLE handle_type;
48 typedef HANDLE synch_object_type;
49 typedef LONG size_type;
50 /// @}
52 private:
53 /// The semaphore object
54 synch_object_type m_semaphore_obj;
56 /// \name Constructors
57 /// @{
58 public:
59 semaphore()
60 : m_semaphore_obj(NULL)
62 explicit_k semaphore(size_type nInitialCount, size_type nMaxCount,
63 LPSECURITY_ATTRIBUTES psa = NULL)
64 : m_semaphore_obj(create_semaphore_(psa, nInitialCount, nMaxCount, NULL))
67 explicit_k semaphore(e_tchar_t const* szName, size_type nInitialCount,
68 size_type nMaxCount, LPSECURITY_ATTRIBUTES psa = NULL)
69 : m_semaphore_obj(create_semaphore_(psa, nInitialCount, nMaxCount, szName))
72 public:
73 virtual ~semaphore() EXTL_THROW_0()
75 if(NULL != m_semaphore_obj)
77 ::CloseHandle(m_semaphore_obj);
78 m_semaphore_obj = NULL;
81 /// @}
83 public:
84 /// Release the semaphore object
85 e_bool_t release(size_type n, size_type* lpPrevCount = NULL)
87 EXTL_ASSERT(NULL != m_semaphore_obj);
89 if(!::ReleaseSemaphore(m_semaphore_obj, n, lpPrevCount))
91 EXTL_THROW_E(win_synch_error("ReleaseMutex operation failed"));
92 return e_false_v;
94 else return e_true_v;
97 public:
98 /// Returns object handle
99 operator handle_type() const
101 return m_semaphore_obj;
103 /// Returns object handle
104 handle_type get_handle() const
106 return m_semaphore_obj;
109 /// Create semaphore object
110 void create(size_type nInitialCount, size_type nMaxCount,
111 LPSECURITY_ATTRIBUTES psa = NULL)
113 m_semaphore_obj = create_semaphore_(psa, nInitialCount, nMaxCount, NULL);
115 /// Create semaphore object
116 void create(e_tchar_t const* szName, size_type nInitialCount,
117 size_type nMaxCount, LPSECURITY_ATTRIBUTES psa = NULL)
119 m_semaphore_obj = create_semaphore_(psa, nInitialCount, nMaxCount, szName);
122 private:
123 synch_object_type create_semaphore_(LPSECURITY_ATTRIBUTES psa, size_type nInitialCount, size_type nMaxCount, e_tchar_t const* pszName)
125 synch_object_type semaphore_obj = ::CreateSemaphore(psa, nInitialCount, nMaxCount, pszName);
126 EXTL_ASSERT_THROW(semaphore_obj != NULL, win_synch_error("Failed to create kernel semaphore object"));
127 return semaphore_obj;
131 /* ///////////////////////////////////////////////////////////////////////
132 * ::extl::platform::win namespace
134 EXTL_WIN_END_WHOLE_NAMESPACE
136 /* //////////////////////////////////////////////////////////////////// */
137 #endif /* EXTL_WIN_SYNCH_SEMAPHORE_H */
138 /* //////////////////////////////////////////////////////////////////// */