cgv
mutex_pthread.h
1 #include <pthread.h>
2 #include <iostream>
3 #include <errno.h>
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  pthread_mutex_init (&(pthread_mutex_t&)pmutex, NULL);
30 }
31 
34 {
35  pthread_mutex_destroy (&(pthread_mutex_t&)pmutex);
36 }
37 
41 {
42  pthread_mutex_lock (&(pthread_mutex_t&)pmutex);
43 }
44 
47 {
48  pthread_mutex_unlock (&(pthread_mutex_t&)pmutex);
49 }
50 
52 void mutex::debug_lock(const std::string& info)
53 {
54  ref_debug_mutex().lock();
55  std::cout << info << ": wait for lock [" << this << ":" << get_debug_lock_counter() << "]" << std::endl;
56  ++ref_debug_lock_counter();
57  ref_debug_mutex().unlock();
58  lock();
59  ref_debug_mutex().lock();
60  std::cout << info << ": received lock [" << this << "]" << std::endl;
61  ref_debug_mutex().unlock();
62 }
64 void mutex::debug_unlock(const std::string& info)
65 {
66  unlock();
67  ref_debug_mutex().lock();
68  std::cout << info << ": unlock [" << this << "]" << std::endl;
69  ref_debug_mutex().unlock();
70 }
71 
72 
75 {
76  return pthread_mutex_trylock(&(pthread_mutex_t&)pmutex) != EBUSY;
77 }
78 
81 {
82  pthread_cond_init(&(pthread_cond_t&)pcond, NULL);
83 }
84 
87 {
88  pthread_cond_destroy(&(pthread_cond_t&)pcond);
89 }
90 
93 {
94  pthread_cond_signal(&(pthread_cond_t&)pcond);
95 }
96 
99 {
100  lock();
101  send_signal();
102  unlock();
103 }
104 
107 {
108  pthread_cond_broadcast(&(pthread_cond_t&)pcond);
109 }
110 
113 {
114  lock();
116  unlock();
117 }
118 
119  }
120 }
121 
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