]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
279da1e3 DH |
2 | #pragma once |
3 | ||
11c3a366 TA |
4 | #include <stdbool.h> |
5 | #include <stdint.h> | |
279da1e3 DH |
6 | #include <sys/types.h> |
7 | ||
8 | #include "macro.h" | |
279da1e3 DH |
9 | |
10 | /* See source file for an API description. */ | |
11 | ||
12 | typedef struct Barrier Barrier; | |
13 | ||
14 | enum { | |
15 | BARRIER_SINGLE = 1LL, | |
16 | BARRIER_ABORTION = INT64_MAX, | |
17 | ||
18 | /* bias values to store state; keep @WE < @THEY < @I */ | |
19 | BARRIER_BIAS = INT64_MIN, | |
20 | BARRIER_WE_ABORTED = BARRIER_BIAS + 1LL, | |
21 | BARRIER_THEY_ABORTED = BARRIER_BIAS + 2LL, | |
22 | BARRIER_I_ABORTED = BARRIER_BIAS + 3LL, | |
23 | }; | |
24 | ||
25 | enum { | |
26 | BARRIER_PARENT, | |
27 | BARRIER_CHILD, | |
28 | }; | |
29 | ||
30 | struct Barrier { | |
31 | int me; | |
32 | int them; | |
33 | int pipe[2]; | |
34 | int64_t barriers; | |
35 | }; | |
36 | ||
7566e267 ZJS |
37 | #define BARRIER_NULL {-1, -1, {-1, -1}, 0} |
38 | ||
39 | int barrier_create(Barrier *obj); | |
279da1e3 DH |
40 | void barrier_destroy(Barrier *b); |
41 | ||
fc808616 DH |
42 | DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy); |
43 | ||
14cb109d | 44 | void barrier_set_role(Barrier *b, unsigned role); |
279da1e3 DH |
45 | |
46 | bool barrier_place(Barrier *b); | |
47 | bool barrier_abort(Barrier *b); | |
48 | ||
49 | bool barrier_wait_next(Barrier *b); | |
50 | bool barrier_wait_abortion(Barrier *b); | |
51 | bool barrier_sync_next(Barrier *b); | |
52 | bool barrier_sync(Barrier *b); | |
53 | ||
54 | static inline bool barrier_i_aborted(Barrier *b) { | |
3742095b | 55 | return IN_SET(b->barriers, BARRIER_I_ABORTED, BARRIER_WE_ABORTED); |
279da1e3 DH |
56 | } |
57 | ||
58 | static inline bool barrier_they_aborted(Barrier *b) { | |
3742095b | 59 | return IN_SET(b->barriers, BARRIER_THEY_ABORTED, BARRIER_WE_ABORTED); |
279da1e3 DH |
60 | } |
61 | ||
62 | static inline bool barrier_we_aborted(Barrier *b) { | |
63 | return b->barriers == BARRIER_WE_ABORTED; | |
64 | } | |
65 | ||
66 | static inline bool barrier_is_aborted(Barrier *b) { | |
d29ad81e ZJS |
67 | return IN_SET(b->barriers, |
68 | BARRIER_I_ABORTED, BARRIER_THEY_ABORTED, BARRIER_WE_ABORTED); | |
279da1e3 DH |
69 | } |
70 | ||
71 | static inline bool barrier_place_and_sync(Barrier *b) { | |
dc751688 | 72 | (void) barrier_place(b); |
279da1e3 DH |
73 | return barrier_sync(b); |
74 | } |