remove \r
[extl.git] / extl / synch / lock_base.h
blob96806f9b21b94bca4497fee64fbab8b38f4ceb90
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: lock_base.h
4 * Created: 08.03.20
5 * Updated: 08.12.13
7 * Brief: The lock_base class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_SYNCH_LOCK_BASE_H
13 #define EXTL_SYNCH_LOCK_BASE_H
15 /*!\file lock_base.h
16 * \brief The lock_base class
18 #ifndef __cplusplus
19 # error lock_base.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 namespace
32 EXTL_BEGIN_NAMESPACE
34 /*!\brief lock_base class
36 * \param Dev The derived type
38 * \ingroup extl_group_synch
40 template<typename_param_k Dev>
41 class lock_base
42 : private uncopyable<lock_base<Dev> >
44 /// \name Types
45 /// @{
46 public:
47 typedef lock_base class_type;
48 typedef Dev derived_type;
49 typedef e_bool_t bool_type;
50 typedef e_size_t size_type;
51 /// @}
53 /// \name Members
54 /// @{
55 private:
56 size_type m_lock_n;
57 /// @}
59 /// \name Constructors
60 /// @{
61 public:
62 lock_base()
63 : m_lock_n(0)
65 /// @}
67 /// \name Methods
68 /// @{
69 public:
70 void lock(); //!< Lock
71 bool_type trylock(); //!< Try lock
72 void unlock(); //!< Unlock
73 /// @}
75 /// \name Attributes
76 /// @{
77 public:
78 /// Returns the lock count
79 size_type count() const { return m_lock_n; }
80 /// Indicates whether is locked
81 bool_type is_locked() const { return (count() != 0); }
82 /// @}
84 /// \name Helpers
85 /// @{
86 protected:
87 derived_type& derive() { return static_cast<derived_type&>(*this); }
88 derived_type const& derive() const { return static_cast<derived_type const&>(*this); }
89 /// @}
91 /* ///////////////////////////////////////////////////////////////////////
92 * Implemention
94 template<typename_param_k Dev>
95 inline void lock_base<Dev>::lock()
97 EXTL_MESSAGE_ASSERT(0 == count(), "lock repeatly");
98 EXTL_ASSERT_THROW(0 == count(), lock_error("lock_error: repeat lock"));
100 derive().do_lock();
101 ++m_lock_n;
103 template<typename_param_k Dev>
104 inline typename_type_ret_k lock_base<Dev>::bool_type
105 lock_base<Dev>::trylock()
107 EXTL_MESSAGE_ASSERT(0 == count(), "lock repeatly");
108 EXTL_ASSERT_THROW(0 == count(), lock_error("lock_error: repeat trylock"));
110 bool_type ret = derive().do_trylock();
111 // lock successfully
112 if (ret) ++m_lock_n;
113 return ret;
116 template<typename_param_k Dev>
117 inline void lock_base<Dev>::unlock()
119 EXTL_MESSAGE_ASSERT(0 != count(), "unlock repeatly");
120 EXTL_ASSERT_THROW(0 != count(), lock_error("lock_error: repeat unlock"));
122 derive().do_unlock();
123 --m_lock_n;
126 /* ///////////////////////////////////////////////////////////////////////
127 * ::extl namespace
129 EXTL_END_NAMESPACE
131 /* //////////////////////////////////////////////////////////////////// */
132 #endif /* EXTL_SYNCH_LOCK_BASE_H */
133 /* //////////////////////////////////////////////////////////////////// */