remove \r
[extl.git] / extl / win / synch / mutex_lock.h
blob11cf8f0d08d4da3875babebd6479862a584b07e7
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: mutex_lock.h
4 * Created: 08.03.21
5 * Updated: 08.05.05
7 * Brief: mutex_lock class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_WIN_SYNCH_MUTEX_LOCK_H
13 #define EXTL_WIN_SYNCH_MUTEX_LOCK_H
15 /*!\file mutex_lock.h
16 * \brief mutex_lock class
18 #ifndef __cplusplus
19 # error mutex_lock.h need be supported by c++.
20 #endif
22 /* ///////////////////////////////////////////////////////////////////////
23 * Includes
25 #include "../win.h"
26 #include "../../synch/lock_base.h"
27 #include "mutex.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl::platform::win namespace
32 EXTL_WIN_BEGIN_WHOLE_NAMESPACE
34 /*!\brief mutex_lock class
35 * \ingroup extl_group_synch
37 class mutex_lock
38 : public lock_base< mutex_lock >
40 public:
41 typedef lock_base< mutex_lock > base_type;
42 typedef mutex_lock class_type;
43 typedef base_type::size_type size_type;
45 private:
46 mutex m_mutex_obj;
48 public:
49 mutex_lock()
50 : base_type(), m_mutex_obj(e_false_v)
53 EXTL_ASSERT(is_valid());
56 ~mutex_lock()
58 if(base_type::is_locked())
60 base_type::unlock();
64 public:
65 void do_lock()
67 EXTL_ASSERT(is_valid());
69 /* Waits the mutex object */
70 ::WaitForSingleObject(m_mutex_obj, INFINITE);
73 e_bool_t do_trylock()
75 EXTL_ASSERT(is_valid());
76 /* Try waiting the mutex object for 100 milliseconds */
77 return (WAIT_TIMEOUT != ::WaitForSingleObject(m_mutex_obj, 100));
79 void do_unlock()
81 EXTL_ASSERT(is_valid());
82 /* Release the mutex object */
83 m_mutex_obj.release();
85 private:
86 e_bool_t is_valid()
88 return ((m_mutex_obj != NULL) && (m_mutex_obj != INVALID_HANDLE_VALUE));
92 /* ///////////////////////////////////////////////////////////////////////
93 * Unit-testing
95 #ifdef EXTL_SYNCH_LOCK_TEST_ENABLE
96 # include "unit_test/mutex_lock_test.h"
97 #endif
98 /* ///////////////////////////////////////////////////////////////////////
99 * ::extl::platform::win namespace
101 EXTL_WIN_END_WHOLE_NAMESPACE
103 /* //////////////////////////////////////////////////////////////////// */
104 #endif /* EXTL_WIN_SYNCH_MUTEX_LOCK_H */
105 /* //////////////////////////////////////////////////////////////////// */