Merge branch 'wip-http-swift' of https://github.com/l-mb/fio
[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);
0ffccc21 31 pthread_rwlock_destroy(&lock->lock);
ae626d4e
BVA
32 munmap((void *) lock, sizeof(*lock));
33}
34
35struct fio_rwlock *fio_rwlock_init(void)
36{
37 struct fio_rwlock *lock;
38 pthread_rwlockattr_t attr;
39 int ret;
40
41 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
42 PROT_READ | PROT_WRITE,
43 OS_MAP_ANON | MAP_SHARED, -1, 0);
44 if (lock == MAP_FAILED) {
45 perror("mmap rwlock");
46 lock = NULL;
47 goto err;
48 }
49
50 lock->magic = FIO_RWLOCK_MAGIC;
51
52 ret = pthread_rwlockattr_init(&attr);
53 if (ret) {
54 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
55 goto err;
56 }
57#ifdef CONFIG_PSHARED
58 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
59 if (ret) {
60 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
61 goto destroy_attr;
62 }
63
64 ret = pthread_rwlock_init(&lock->lock, &attr);
65#else
66 ret = pthread_rwlock_init(&lock->lock, NULL);
67#endif
68
69 if (ret) {
70 log_err("pthread_rwlock_init: %s\n", strerror(ret));
71 goto destroy_attr;
72 }
73
74 pthread_rwlockattr_destroy(&attr);
75
76 return lock;
77destroy_attr:
78 pthread_rwlockattr_destroy(&attr);
79err:
80 if (lock)
81 fio_rwlock_remove(lock);
82 return NULL;
83}