Fork me on GitHub
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1
10#ifndef JANUS_MUTEX_H
11#define JANUS_MUTEX_H
12
13#include <pthread.h>
14#include <errno.h>
15
16#include "debug.h"
17
18extern int lock_debug;
19
20#ifdef USE_PTHREAD_MUTEX
21
23typedef pthread_mutex_t janus_mutex;
25#define janus_mutex_init(a) pthread_mutex_init(a,NULL)
27#define JANUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
29#define janus_mutex_destroy(a) pthread_mutex_destroy(a)
31#define janus_mutex_lock_nodebug(a) pthread_mutex_lock(a)
33#define janus_mutex_lock_debug(a) { JANUS_PRINT("[%s:%s:%d:lock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); pthread_mutex_lock(a); }
35#define janus_mutex_lock(a) { if(!lock_debug) { janus_mutex_lock_nodebug(a); } else { janus_mutex_lock_debug(a); } }
37#define janus_mutex_trylock_nodebug(a) { ret = !pthread_mutex_trylock(a); }
39#define janus_mutex_trylock_debug(a) { JANUS_PRINT("[%s:%s:%d:trylock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); ret = !pthread_mutex_trylock(a); }
41#define janus_mutex_trylock(a) ({ int ret; if(!lock_debug) { janus_mutex_trylock_nodebug(a); } else { janus_mutex_trylock_debug(a); } ret; })
43#define janus_mutex_unlock_nodebug(a) pthread_mutex_unlock(a)
45#define janus_mutex_unlock_debug(a) { JANUS_PRINT("[%s:%s:%d:unlock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); pthread_mutex_unlock(a); }
47#define janus_mutex_unlock(a) { if(!lock_debug) { janus_mutex_unlock_nodebug(a); } else { janus_mutex_unlock_debug(a); } }
48
50typedef pthread_cond_t janus_condition;
52#define janus_condition_init(a) pthread_cond_init(a,NULL)
54#define janus_condition_destroy(a) pthread_cond_destroy(a)
56#define janus_condition_wait(a, b) pthread_cond_wait(a, b);
58#define janus_condition_wait_until(a, b, c) { \
59 const struct timespec jct = { \
60 .tv_sec = c / G_USEC_PER_SEC, \
61 .tv_nsec = (c % G_USEC_PER_SEC)*1000 \
62 }; \
63 pthread_cond_timedwait(a, b, &jct); \
64}
66#define janus_condition_signal(a) pthread_cond_signal(a);
68#define janus_condition_broadcast(a) pthread_cond_broadcast(a);
69
70#else
71
73typedef GMutex janus_mutex;
75#define janus_mutex_init(a) g_mutex_init(a)
77#define JANUS_MUTEX_INITIALIZER {0}
79#define janus_mutex_destroy(a) g_mutex_clear(a)
81#define janus_mutex_lock_nodebug(a) g_mutex_lock(a)
83#define janus_mutex_lock_debug(a) { JANUS_PRINT("[%s:%s:%d:lock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); g_mutex_lock(a); }
85#define janus_mutex_lock(a) { if(!lock_debug) { janus_mutex_lock_nodebug(a); } else { janus_mutex_lock_debug(a); } }
87#define janus_mutex_trylock_nodebug(a) { ret = g_mutex_trylock(a); }
89#define janus_mutex_trylock_debug(a) { JANUS_PRINT("[%s:%s:%d:trylock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); ret = g_mutex_trylock(a); }
91#define janus_mutex_trylock(a) ({ gboolean ret; if(!lock_debug) { janus_mutex_trylock_nodebug(a); } else { janus_mutex_trylock_debug(a); } ret; })
93#define janus_mutex_unlock_nodebug(a) g_mutex_unlock(a)
95#define janus_mutex_unlock_debug(a) { JANUS_PRINT("[%s:%s:%d:unlock] %p\n", __FILE__, __FUNCTION__, __LINE__, a); g_mutex_unlock(a); }
97#define janus_mutex_unlock(a) { if(!lock_debug) { janus_mutex_unlock_nodebug(a); } else { janus_mutex_unlock_debug(a); } }
98
100typedef GCond janus_condition;
102#define janus_condition_init(a) g_cond_init(a)
104#define janus_condition_destroy(a) g_cond_clear(a)
106#define janus_condition_wait(a, b) g_cond_wait(a, b);
108#define janus_condition_wait_until(a, b, c) g_cond_wait_until(a, b, c);
110#define janus_condition_signal(a) g_cond_signal(a);
112#define janus_condition_broadcast(a) g_cond_broadcast(a);
113
114#endif
115
116#endif
Logging and Debugging.
GCond janus_condition
Janus condition implementation.
Definition mutex.h:100
int lock_debug
Definition janus-cfgconv.c:38
GMutex janus_mutex
Janus mutex implementation.
Definition mutex.h:73