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 | ||
42 | int mutex_init_pshared(pthread_mutex_t *mutex) | |
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 | ret = pthread_mutex_init(mutex, &mattr); | |
64 | if (ret) { | |
65 | log_err("pthread_mutex_init: %s\n", strerror(ret)); | |
66 | return ret; | |
67 | } | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
72 | int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond) | |
73 | { | |
74 | int ret; | |
75 | ||
76 | ret = mutex_init_pshared(mutex); | |
77 | if (ret) | |
78 | return ret; | |
79 | ||
80 | ret = cond_init_pshared(cond); | |
81 | if (ret) | |
82 | return ret; | |
83 | ||
84 | return 0; | |
85 | } |