Latest Dasynq updates
[oweals/dinit.git] / src / dasynq / dasynq-mutex.h
1 #ifndef D_MUTEX_H_INCLUDED
2 #define D_MUTEX_H_INCLUDED
3
4 //#include <pthread.h>
5 #include <mutex>
6
7 namespace dasynq {
8
9 // Simple non-recursive mutex, with priority inheritance to avoid priority inversion.
10 /*
11 class DMutex
12 {
13     private:
14     pthread_mutex_t mutex;
15     
16     public:
17     DMutex()
18     {
19         // Avoid priority inversion by using PTHREAD_PRIO_INHERIT
20         pthread_mutexattr_t attribs;
21         pthread_mutexattr_init(&attribs);
22         pthread_mutexattr_setprotocol(&attribs, PTHREAD_PRIO_INHERIT);
23         pthread_mutex_init(&mutex, &attribs);
24     }
25     
26     void lock()
27     {
28         pthread_mutex_lock(&mutex);
29     }
30     
31     void unlock()
32     {
33         pthread_mutex_unlock(&mutex);
34     }    
35 };
36 */
37
38 using DMutex = std::mutex;
39
40 // A "null" mutex, for which locking / unlocking actually does nothing.
41 class NullMutex
42 {
43     EMPTY_BODY
44     
45     public:
46     void lock() { }
47     void unlock() { }
48     void try_lock() { }
49 };
50
51
52 } // end of namespace
53
54
55 #endif