Debian Codegraph @

contrib/epee/include/syncobj.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29
30 #ifndef __WINH_OBJ_H__
31 #define __WINH_OBJ_H__
32
33 #include <boost/chrono/duration.hpp>
34 #include <boost/thread/condition_variable.hpp>
35 #include <boost/thread/locks.hpp>
36 #include <boost/thread/mutex.hpp>
37 #include <boost/thread/recursive_mutex.hpp>
38 #include <boost/thread/thread.hpp>
39
40 namespace epee
41 {
42
43 namespace debug
44 {
45 inline unsigned int &g_test_dbg_lock_sleep()
46 {
47 static unsigned int value = 0;
48 return value;
49 }
50 }
51
52 struct simple_event
53 {
54 simple_event() : m_rised(false)
55 {
56 }
57
58 void raise()
59 {
60 boost::unique_lock<boost::mutex> lock(m_mx);
61 m_rised = true;
62 m_cond_var.notify_one();
63 }
64
65 void wait()
66 {
67 boost::unique_lock<boost::mutex> lock(m_mx);
68 while (!m_rised)
69 m_cond_var.wait(lock);
70 m_rised = false;
71 }
72
73 private:
74 boost::mutex m_mx;
75 boost::condition_variable m_cond_var;
76 bool m_rised;
77 };
78
79 class critical_region;
80
81 class critical_section
82 {
83 boost::recursive_mutex m_section;
84
85 public:
86 //to make copy fake!
87 critical_section(const critical_section& section)
88 {
89 }
90
91 critical_section()
92 {
93 }
94
95 ~critical_section()
96 {
97 }
98
99 void lock()
100 {
101 m_section.lock();
102 //EnterCriticalSection( &m_section );
103 }
104
105 void unlock()
106 {
107 m_section.unlock();
108 }
109
110 bool tryLock()
111 {
112 return m_section.try_lock();
113 }
114
115 // to make copy fake
116 critical_section& operator=(const critical_section& section)
117 {
118 return *this;
119 }
120 };
121
122
123 template<class t_lock>
124 class critical_region_t
125 {
126 t_lock& m_locker;
127 bool m_unlocked;
128
129 critical_region_t(const critical_region_t&) {}
130
131 public:
132 critical_region_t(t_lock& cs): m_locker(cs), m_unlocked(false)
133 {
134 m_locker.lock();
135 }
136
137 ~critical_region_t()
138 {
139 unlock();
140 }
141
142 void unlock()
143 {
144 if (!m_unlocked)
145 {
146 m_locker.unlock();
147 m_unlocked = true;
148 }
149 }
150 };
151
152
153 #define CRITICAL_REGION_LOCAL(x) {} epee::critical_region_t<decltype(x)> critical_region_var(x)
154 #define CRITICAL_REGION_BEGIN(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var(x)
155 #define CRITICAL_REGION_LOCAL1(x) {boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep()));} epee::critical_region_t<decltype(x)> critical_region_var1(x)
156 #define CRITICAL_REGION_BEGIN1(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var1(x)
157
158 #define CRITICAL_REGION_END() }
159
160 }
161
162 #endif