summaryrefslogtreecommitdiff
path: root/include/linux/rwsem.h
blob: 4dd30b7132175ba24cc75e20bdc49712f9656c2f (plain)
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
70
71
#ifndef __TOOLS_LINUX_RWSEM_H
#define __TOOLS_LINUX_RWSEM_H

#include <pthread.h>
#include <linux/cleanup.h>

struct rw_semaphore {
	pthread_rwlock_t	lock;
};

#define __RWSEM_INITIALIZER(name)				\
	{ .lock = PTHREAD_RWLOCK_INITIALIZER }

#define DECLARE_RWSEM(name) \
	struct rw_semaphore name = __RWSEM_INITIALIZER(name)

static inline void init_rwsem(struct rw_semaphore *lock)
{
	pthread_rwlock_init(&lock->lock, NULL);
}

static inline void down_read(struct rw_semaphore *sem)
{
	pthread_rwlock_rdlock(&sem->lock);
}

static inline int down_read_trylock(struct rw_semaphore *sem)
{
	return !pthread_rwlock_tryrdlock(&sem->lock);
}

static inline int down_read_interruptible(struct rw_semaphore *sem)
{
	pthread_rwlock_rdlock(&sem->lock);
	return 0;
}

static inline int down_read_killable(struct rw_semaphore *sem)
{
	pthread_rwlock_rdlock(&sem->lock);
	return 0;
}

static inline void up_read(struct rw_semaphore *sem)
{
	pthread_rwlock_unlock(&sem->lock);
}

static inline void down_write(struct rw_semaphore *sem)
{
	pthread_rwlock_wrlock(&sem->lock);
}

static inline int down_write_trylock(struct rw_semaphore *sem)
{
	return !pthread_rwlock_trywrlock(&sem->lock);
}

static inline void up_write(struct rw_semaphore *sem)
{
	pthread_rwlock_unlock(&sem->lock);
}

DEFINE_GUARD(rwsem_read, struct rw_semaphore *, down_read(_T), up_read(_T))
DEFINE_GUARD_COND(rwsem_read, _try, down_read_trylock(_T))
DEFINE_GUARD_COND(rwsem_read, _intr, down_read_interruptible(_T) == 0)

DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T))
DEFINE_GUARD_COND(rwsem_write, _try, down_write_trylock(_T))

#endif /* __TOOLS_LINUX_RWSEM_H */