cgv
mutex_std_thread.h
1 #include "mutex.h"
2 #include "common_std_thread.h"
3 #include <iostream>
4 
5 namespace cgv {
6  namespace os {
7 
8 unsigned& ref_debug_lock_counter()
9 {
10  static unsigned counter = 0;
11  return counter;
12 }
13 
14 mutex& ref_debug_mutex()
15 {
16  static mutex m;
17  return m;
18 }
19 
22 {
23  return ref_debug_lock_counter();
24 }
25 
28 {
29  Mutex*& m_ptr = ((Mutex*&) pmutex);
30  m_ptr = new Mutex();
31 }
32 
35 {
36  Mutex*& m_ptr = ((Mutex*&) pmutex);
37  delete m_ptr;
38 }
39 
42 void mutex::lock()
43 {
44  Mutex& m = *((Mutex*&) pmutex);
45  m.lock();
46 }
47 
49 void mutex::unlock()
50 {
51  Mutex& m = *((Mutex*&) pmutex);
52  m.unlock();
53 }
54 
56 void mutex::debug_lock(const std::string& info)
57 {
58  ref_debug_mutex().lock();
59  std::cout << info << ": wait for lock [" << this << ":" << get_debug_lock_counter() << "]" << std::endl;
60  ++ref_debug_lock_counter();
61  ref_debug_mutex().unlock();
62  lock();
63  ref_debug_mutex().lock();
64  std::cout << info << ": received lock [" << this << "]" << std::endl;
65  ref_debug_mutex().unlock();
66 }
68 void mutex::debug_unlock(const std::string& info)
69 {
70  unlock();
71  ref_debug_mutex().lock();
72  std::cout << info << ": unlock [" << this << "]" << std::endl;
73  ref_debug_mutex().unlock();
74 }
75 
77 bool mutex::try_lock()
78 {
79  Mutex& m = *((Mutex*&) pmutex);
80  return m.try_lock();
81 }
82 
85 {
86  Mutex& m = *((Mutex*&) pmutex);
87  Condition*& c_ptr = (Condition*&) pcond;
88  c_ptr = new Condition(m);
89 }
90 
93 {
94  Condition*& c_ptr = (Condition*&) pcond;
95  delete c_ptr;
96 }
97 
100 {
101  Condition& c = *((Condition*&) pcond);
102  c.cv.notify_one();
103 }
104 
107 {
108  lock();
109  send_signal();
110  unlock();
111 }
112 
115 {
116  Condition& c = *((Condition*&) pcond);
117  c.cv.notify_all();
118 }
119 
122 {
123  lock();
125  unlock();
126 }
127 
128  }
129 }
130 
cgv::os::mutex::~mutex
~mutex()
destruct a mutex
Definition: mutex_pthread.h:33
cgv::os::condition_mutex::send_signal
void send_signal()
send the signal to unblock a thread waiting for the condition represented by this condition_mutex
Definition: mutex_pthread.h:92
cgv::os::condition_mutex::broadcast_signal_with_lock
void broadcast_signal_with_lock()
prefered approach to broadcast the signal and implemented as {lock();broadcast_signal();unlock();}
Definition: mutex_pthread.h:112
cgv::os::mutex::unlock
void unlock()
unlock the mutex
Definition: mutex_pthread.h:46
cgv::os::mutex::mutex
mutex()
construct a mutex
Definition: mutex_pthread.h:27
cgv::os::mutex::debug_lock
void debug_lock(const std::string &info)
same as lock but with printing debug information
Definition: mutex_pthread.h:52
cgv::os::mutex::lock
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...
Definition: mutex_pthread.h:40
cgv::os::condition_mutex::broadcast_signal
void broadcast_signal()
broadcast signal to unblock several threads waiting for the condition represented by this condition_m...
Definition: mutex_pthread.h:106
cgv::os::condition_mutex::condition_mutex
condition_mutex()
construct a mutex
Definition: mutex_pthread.h:80
cgv::os::condition_mutex::send_signal_with_lock
void send_signal_with_lock()
prefered approach to send the signal and implemented as {lock();send_signal();unlock();}
Definition: mutex_pthread.h:98
cgv::os::condition_mutex::~condition_mutex
~condition_mutex()
destruct a mutex
Definition: mutex_pthread.h:86
cgv::os::mutex::try_lock
bool try_lock()
try to lock the mutex (return false if the mutex is still locked)
Definition: mutex_pthread.h:74
cgv::os::mutex::get_debug_lock_counter
static unsigned get_debug_lock_counter()
return the global locking counter that is used for mutex debugging
Definition: mutex_pthread.h:21
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::os::mutex::debug_unlock
void debug_unlock(const std::string &info)
same unlock but with printing debug information
Definition: mutex_pthread.h:64