Merge branch 'python3-testing' of https://github.com/vincentkfu/fio
[fio.git] / pshared.c
1 #include <string.h>
2
3 #include "log.h"
4 #include "pshared.h"
5
6 int cond_init_pshared(pthread_cond_t *cond)
7 {
8         pthread_condattr_t cattr;
9         int ret;
10
11         ret = pthread_condattr_init(&cattr);
12         if (ret) {
13                 log_err("pthread_condattr_init: %s\n", strerror(ret));
14                 return ret;
15         }
16
17 #ifdef CONFIG_PSHARED
18         ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
19         if (ret) {
20                 log_err("pthread_condattr_setpshared: %s\n", strerror(ret));
21                 return ret;
22         }
23 #endif
24
25 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
26         ret = pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
27         if (ret) {
28                 log_err("pthread_condattr_setclock: %s\n", strerror(ret));
29                 return ret;
30         }
31 #endif
32
33         ret = pthread_cond_init(cond, &cattr);
34         if (ret) {
35                 log_err("pthread_cond_init: %s\n", strerror(ret));
36                 return ret;
37         }
38
39         return 0;
40 }
41
42 int mutex_init_pshared_with_type(pthread_mutex_t *mutex, int type)
43 {
44         pthread_mutexattr_t mattr;
45         int ret;
46
47         ret = pthread_mutexattr_init(&mattr);
48         if (ret) {
49                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
50                 return ret;
51         }
52
53         /*
54          * Not all platforms support process shared mutexes (FreeBSD)
55          */
56 #ifdef CONFIG_PSHARED
57         ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
58         if (ret) {
59                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
60                 return ret;
61         }
62 #endif
63         if (type) {
64                 ret = pthread_mutexattr_settype(&mattr, type);
65                 if (ret) {
66                         log_err("pthread_mutexattr_settype: %s\n",
67                                 strerror(ret));
68                         return ret;
69                 }
70         }
71         ret = pthread_mutex_init(mutex, &mattr);
72         if (ret) {
73                 log_err("pthread_mutex_init: %s\n", strerror(ret));
74                 return ret;
75         }
76
77         return 0;
78 }
79
80 int mutex_init_pshared(pthread_mutex_t *mutex)
81 {
82         return mutex_init_pshared_with_type(mutex, 0);
83 }
84
85 int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond)
86 {
87         int ret;
88
89         ret = mutex_init_pshared(mutex);
90         if (ret)
91                 return ret;
92
93         ret = cond_init_pshared(cond);
94         if (ret)
95                 return ret;
96
97         return 0;
98 }