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 | |
24 | ret = pthread_cond_init(cond, &cattr); | |
25 | if (ret) { | |
26 | log_err("pthread_cond_init: %s\n", strerror(ret)); | |
27 | return ret; | |
28 | } | |
29 | ||
30 | return 0; | |
31 | } | |
32 | ||
33 | int mutex_init_pshared(pthread_mutex_t *mutex) | |
34 | { | |
35 | pthread_mutexattr_t mattr; | |
36 | int ret; | |
37 | ||
38 | ret = pthread_mutexattr_init(&mattr); | |
39 | if (ret) { | |
40 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); | |
41 | return ret; | |
42 | } | |
43 | ||
44 | /* | |
45 | * Not all platforms support process shared mutexes (FreeBSD) | |
46 | */ | |
47 | #ifdef CONFIG_PSHARED | |
48 | ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); | |
49 | if (ret) { | |
50 | log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); | |
51 | return ret; | |
52 | } | |
53 | #endif | |
54 | ret = pthread_mutex_init(mutex, &mattr); | |
55 | if (ret) { | |
56 | log_err("pthread_mutex_init: %s\n", strerror(ret)); | |
57 | return ret; | |
58 | } | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
63 | int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond) | |
64 | { | |
65 | int ret; | |
66 | ||
67 | ret = mutex_init_pshared(mutex); | |
68 | if (ret) | |
69 | return ret; | |
70 | ||
71 | ret = cond_init_pshared(cond); | |
72 | if (ret) | |
73 | return ret; | |
74 | ||
75 | return 0; | |
76 | } |