1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#ifndef LOCK_H_INCLUDED
#define LOCK_H_INCLUDED
#include <pthread.h>
#include <urcu.h>
#include <urcu/uatomic.h>
#include <stdbool.h>
typedef void (wakeup_fn)(void);
struct mutex_lock {
pthread_mutex_t mutex;
wakeup_fn *wakeup;
int waiters; /* uatomic access only */
};
static inline void init_lock(struct mutex_lock *a)
{
pthread_mutex_init(&a->mutex, NULL);
uatomic_set(&a->waiters, 0);
}
#if defined(__GNUC__) && __GNUC__ == 12 && URCU_VERSION < 0xe00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#endif
static inline int uatomic_xchg_int(int *ptr, int val)
{
return uatomic_xchg(ptr, val);
}
static inline void lock(struct mutex_lock *a)
{
uatomic_inc(&a->waiters);
pthread_mutex_lock(&a->mutex);
uatomic_dec(&a->waiters);
}
#if defined(__GNUC__) && __GNUC__ == 12 && URCU_VERSION < 0xe00
#pragma GCC diagnostic pop
#endif
static inline int trylock(struct mutex_lock *a)
{
return pthread_mutex_trylock(&a->mutex);
}
static inline int timedlock(struct mutex_lock *a, struct timespec *tmo)
{
return pthread_mutex_timedlock(&a->mutex, tmo);
}
static inline void unlock__(struct mutex_lock *a)
{
pthread_mutex_unlock(&a->mutex);
}
static inline bool lock_has_waiters(struct mutex_lock *a)
{
return (uatomic_read(&a->waiters) > 0);
}
#define lock_cleanup_pop(a) pthread_cleanup_pop(1)
void cleanup_lock (void * data);
void set_wakeup_fn(struct mutex_lock *lock, wakeup_fn *fn);
#endif /* LOCK_H_INCLUDED */
|