cgv
Loading...
Searching...
No Matches
mutex_pthread.h
1#include <pthread.h>
2#include <iostream>
3#include <errno.h>
4
5namespace cgv {
6 namespace os {
7
8unsigned& ref_debug_lock_counter()
9{
10 static unsigned counter = 0;
11 return counter;
12}
13
14mutex& 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
52void 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}
64void 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
104
107{
108 pthread_cond_broadcast(&(pthread_cond_t&)pcond);
109}
110
118
119 }
120}
121
the cgv namespace
Definition print.h:11
void send_signal_with_lock()
prefered approach to send the signal and implemented as {lock();send_signal();unlock();}
~condition_mutex()
destruct a mutex
void send_signal()
send the signal to unblock a thread waiting for the condition represented by this condition_mutex
void broadcast_signal_with_lock()
prefered approach to broadcast the signal and implemented as {lock();broadcast_signal();unlock();}
void broadcast_signal()
broadcast signal to unblock several threads waiting for the condition represented by this condition_m...
condition_mutex()
construct a mutex
void unlock()
unlock the mutex
static unsigned get_debug_lock_counter()
return the global locking counter that is used for mutex debugging
~mutex()
destruct a mutex
void debug_unlock(const std::string &info)
same unlock but with printing debug information
bool try_lock()
try to lock the mutex (return false if the mutex is still locked)
mutex()
construct a mutex
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...
void debug_lock(const std::string &info)
same as lock but with printing debug information