Monero
Loading...
Searching...
No Matches
syncobj.h
Go to the documentation of this file.
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
40namespace 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
53 {
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
82 {
83 boost::recursive_mutex m_section;
84
85 public:
86 //to make copy fake!
88 {
89 }
90
92 {
93 }
94
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
117 {
118 return *this;
119 }
120 };
121
122
123 template<class t_lock>
125 {
126 t_lock& m_locker;
128
130
131 public:
133 {
134 m_locker.lock();
135 }
136
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
t_lock & m_locker
Definition syncobj.h:126
critical_region_t(const critical_region_t &)
Definition syncobj.h:129
void unlock()
Definition syncobj.h:142
bool m_unlocked
Definition syncobj.h:127
~critical_region_t()
Definition syncobj.h:137
critical_region_t(t_lock &cs)
Definition syncobj.h:132
void lock()
Definition syncobj.h:99
~critical_section()
Definition syncobj.h:95
void unlock()
Definition syncobj.h:105
boost::recursive_mutex m_section
Definition syncobj.h:83
critical_section & operator=(const critical_section &section)
Definition syncobj.h:116
critical_section(const critical_section &section)
Definition syncobj.h:87
bool tryLock()
Definition syncobj.h:110
critical_section()
Definition syncobj.h:91
#define false
Definition syncobj.h:44
unsigned int & g_test_dbg_lock_sleep()
Definition syncobj.h:45
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
void raise()
Definition syncobj.h:58
boost::condition_variable m_cond_var
Definition syncobj.h:75
bool m_rised
Definition syncobj.h:76
boost::mutex m_mx
Definition syncobj.h:74
void wait()
Definition syncobj.h:65
simple_event()
Definition syncobj.h:54