| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |