Commit | Line | Data |
---|---|---|
ae626d4e BVA |
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 | |
78b66d32 BVA |
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 | ||
ae626d4e BVA |
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 | ||
3ed6894b | 42 | int mutex_init_pshared_with_type(pthread_mutex_t *mutex, int type) |
ae626d4e BVA |
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 | /* | |
5ced70d4 | 54 | * Not all platforms support process shared mutexes (NetBSD/OpenBSD) |
ae626d4e BVA |
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 | |
3ed6894b SK |
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 | } | |
ae626d4e BVA |
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 | ||
3ed6894b SK |
80 | int mutex_init_pshared(pthread_mutex_t *mutex) |
81 | { | |
82 | return mutex_init_pshared_with_type(mutex, 0); | |
83 | } | |
84 | ||
ae626d4e BVA |
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 | } |