1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The lock_base class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_SYNCH_LOCK_BASE_H
13 #define EXTL_SYNCH_LOCK_BASE_H
16 * \brief The lock_base class
19 # error lock_base.h need be supported by c++.
22 /* ///////////////////////////////////////////////////////////////////////
26 #include "../utility/uncopyable.h"
27 #include "../error/error.h"
29 /* ///////////////////////////////////////////////////////////////////////
34 /*!\brief lock_base class
36 * \param Dev The derived type
38 * \ingroup extl_group_synch
40 template<typename_param_k Dev
>
42 : private uncopyable
<lock_base
<Dev
> >
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
;
59 /// \name Constructors
70 void lock(); //!< Lock
71 bool_type
trylock(); //!< Try lock
72 void unlock(); //!< Unlock
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); }
87 derived_type
& derive() { return static_cast<derived_type
&>(*this); }
88 derived_type
const& derive() const { return static_cast<derived_type
const&>(*this); }
91 /* ///////////////////////////////////////////////////////////////////////
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"));
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();
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();
126 /* ///////////////////////////////////////////////////////////////////////
131 /* //////////////////////////////////////////////////////////////////// */
132 #endif /* EXTL_SYNCH_LOCK_BASE_H */
133 /* //////////////////////////////////////////////////////////////////// */