File: lock.h

package info (click to toggle)
multipath-tools 0.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,088 kB
  • sloc: ansic: 64,885; perl: 1,622; makefile: 742; sh: 732; pascal: 155
file content (69 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download
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 */