Rename fio_mutex into fio_sem
[fio.git] / rwlock.c
CommitLineData
ae626d4e
BVA
1#include <stdio.h>
2#include <string.h>
3#include <sys/mman.h>
4#include <assert.h>
5
6#include "log.h"
7#include "rwlock.h"
8#include "os/os.h"
9
10void fio_rwlock_write(struct fio_rwlock *lock)
11{
12 assert(lock->magic == FIO_RWLOCK_MAGIC);
13 pthread_rwlock_wrlock(&lock->lock);
14}
15
16void fio_rwlock_read(struct fio_rwlock *lock)
17{
18 assert(lock->magic == FIO_RWLOCK_MAGIC);
19 pthread_rwlock_rdlock(&lock->lock);
20}
21
22void fio_rwlock_unlock(struct fio_rwlock *lock)
23{
24 assert(lock->magic == FIO_RWLOCK_MAGIC);
25 pthread_rwlock_unlock(&lock->lock);
26}
27
28void fio_rwlock_remove(struct fio_rwlock *lock)
29{
30 assert(lock->magic == FIO_RWLOCK_MAGIC);
31 munmap((void *) lock, sizeof(*lock));
32}
33
34struct fio_rwlock *fio_rwlock_init(void)
35{
36 struct fio_rwlock *lock;
37 pthread_rwlockattr_t attr;
38 int ret;
39
40 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
41 PROT_READ | PROT_WRITE,
42 OS_MAP_ANON | MAP_SHARED, -1, 0);
43 if (lock == MAP_FAILED) {
44 perror("mmap rwlock");
45 lock = NULL;
46 goto err;
47 }
48
49 lock->magic = FIO_RWLOCK_MAGIC;
50
51 ret = pthread_rwlockattr_init(&attr);
52 if (ret) {
53 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
54 goto err;
55 }
56#ifdef CONFIG_PSHARED
57 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
58 if (ret) {
59 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
60 goto destroy_attr;
61 }
62
63 ret = pthread_rwlock_init(&lock->lock, &attr);
64#else
65 ret = pthread_rwlock_init(&lock->lock, NULL);
66#endif
67
68 if (ret) {
69 log_err("pthread_rwlock_init: %s\n", strerror(ret));
70 goto destroy_attr;
71 }
72
73 pthread_rwlockattr_destroy(&attr);
74
75 return lock;
76destroy_attr:
77 pthread_rwlockattr_destroy(&attr);
78err:
79 if (lock)
80 fio_rwlock_remove(lock);
81 return NULL;
82}