GCS  0.2.3
gu_mutex.h
1 // Copyright (C) 2007 Codership Oy <info@codership.com>
2 
9 #ifndef _gu_mutex_h_
10 #define _gu_mutex_h_
11 
12 #include <pthread.h>
13 
14 struct gu_mutex
15 {
16  pthread_mutex_t target_mutex;
17  pthread_mutex_t control_mutex;
18 
19  volatile int lock_waiter_count;
20  volatile int cond_waiter_count;
21  volatile int holder_count;
22  volatile pthread_t thread;
23 
24  /* point in source code, where called from */
25  volatile const char *file;
26  volatile int line;
27 };
28 
31 int gu_mutex_init_dbg (struct gu_mutex *mutex,
32  const pthread_mutexattr_t *attr,
33  const char *file, unsigned int line);
34 int gu_mutex_lock_dbg (struct gu_mutex *mutex,
35  const char *file, unsigned int line);
36 int gu_mutex_unlock_dbg (struct gu_mutex *mutex,
37  const char *file, unsigned int line);
38 int gu_mutex_destroy_dbg (struct gu_mutex *mutex,
39  const char *file, unsigned int line);
40 int gu_cond_wait_dbg (pthread_cond_t *cond,
41  struct gu_mutex *mutex,
42  const char *file, unsigned int line);
49 #ifdef DEBUG_MUTEX
50 
51 typedef struct gu_mutex gu_mutex_t;
52 
53 #define gu_mutex_init(M,A) gu_mutex_init_dbg ((M),(A), __FILE__, __LINE__)
54 #define gu_mutex_lock(M) gu_mutex_lock_dbg ((M), __FILE__, __LINE__)
55 #define gu_mutex_unlock(M) gu_mutex_unlock_dbg ((M), __FILE__, __LINE__)
56 #define gu_mutex_destroy(M) gu_mutex_destroy_dbg((M), __FILE__, __LINE__)
57 #define gu_cond_wait(S,M) gu_cond_wait_dbg ((S),(M), __FILE__, __LINE__)
58 
59 #define GU_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, \
60  PTHREAD_MUTEX_INITIALIZER, \
61  0,0,0,0,0,0 }
62 
63 #else /* DEBUG_MUTEX not defined - use regular pthread functions */
64 
65 typedef pthread_mutex_t gu_mutex_t;
66 
67 #define gu_mutex_init(M,A) pthread_mutex_init ((M),(A))
68 #define gu_mutex_lock(M) pthread_mutex_lock ((M))
69 #define gu_mutex_unlock(M) pthread_mutex_unlock ((M))
70 #define gu_mutex_destroy(M) pthread_mutex_destroy((M))
71 #define gu_cond_wait(S,M) pthread_cond_wait ((S),(M))
72 
73 #define GU_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
74 
75 #endif /* DEBUG_MUTEX */
76 
78 /* The following typedefs and macros don't do anything now,
79  * but may be used later */
80 typedef pthread_t gu_thread_t;
81 typedef pthread_cond_t gu_cond_t;
82 #define gu_thread_create pthread_create
83 #define gu_thread_join pthread_join
84 #define gu_thread_cancel pthread_cancel
85 #define gu_thread_exit pthread_exit
86 #define gu_cond_init pthread_cond_init
87 #define gu_cond_destroy pthread_cond_destroy
88 #define gu_cond_signal pthread_cond_signal
89 #define gu_cond_broadcast pthread_cond_broadcast
90 #define gu_cond_timedwait pthread_cond_timedwait
91 
92 #if defined(__APPLE__)
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 typedef int pthread_barrierattr_t;
99 typedef struct
100 {
101  pthread_mutex_t mutex;
102  pthread_cond_t cond;
103  int count;
104  int tripCount;
105 } pthread_barrier_t;
106 
107 int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count);
108 int pthread_barrier_destroy (pthread_barrier_t *barrier);
109 int pthread_barrier_wait (pthread_barrier_t *barrier);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* __APPLE__ */
116 
117 #endif /* _gu_mutex_h_ */
Definition: gu_mutex.h:14
pthread_mutex_t control_mutex
for mutex operations
Definition: gu_mutex.h:17
volatile int holder_count
must be 0 or 1
Definition: gu_mutex.h:21
volatile int cond_waiter_count
of threads waiting for cond
Definition: gu_mutex.h:20
pthread_mutex_t target_mutex
for critical section
Definition: gu_mutex.h:16
volatile int lock_waiter_count
of threads waiting for lock
Definition: gu_mutex.h:19