]>
Commit | Line | Data |
---|---|---|
279da1e3 DH |
1 | #pragma once |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 David Herrmann <dh.herrmann@gmail.com> | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
11c3a366 TA |
22 | #include <stdbool.h> |
23 | #include <stdint.h> | |
279da1e3 DH |
24 | #include <sys/types.h> |
25 | ||
26 | #include "macro.h" | |
279da1e3 DH |
27 | |
28 | /* See source file for an API description. */ | |
29 | ||
30 | typedef struct Barrier Barrier; | |
31 | ||
32 | enum { | |
33 | BARRIER_SINGLE = 1LL, | |
34 | BARRIER_ABORTION = INT64_MAX, | |
35 | ||
36 | /* bias values to store state; keep @WE < @THEY < @I */ | |
37 | BARRIER_BIAS = INT64_MIN, | |
38 | BARRIER_WE_ABORTED = BARRIER_BIAS + 1LL, | |
39 | BARRIER_THEY_ABORTED = BARRIER_BIAS + 2LL, | |
40 | BARRIER_I_ABORTED = BARRIER_BIAS + 3LL, | |
41 | }; | |
42 | ||
43 | enum { | |
44 | BARRIER_PARENT, | |
45 | BARRIER_CHILD, | |
46 | }; | |
47 | ||
48 | struct Barrier { | |
49 | int me; | |
50 | int them; | |
51 | int pipe[2]; | |
52 | int64_t barriers; | |
53 | }; | |
54 | ||
7566e267 ZJS |
55 | #define BARRIER_NULL {-1, -1, {-1, -1}, 0} |
56 | ||
57 | int barrier_create(Barrier *obj); | |
279da1e3 DH |
58 | void barrier_destroy(Barrier *b); |
59 | ||
fc808616 DH |
60 | DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy); |
61 | ||
279da1e3 DH |
62 | void barrier_set_role(Barrier *b, unsigned int role); |
63 | ||
64 | bool barrier_place(Barrier *b); | |
65 | bool barrier_abort(Barrier *b); | |
66 | ||
67 | bool barrier_wait_next(Barrier *b); | |
68 | bool barrier_wait_abortion(Barrier *b); | |
69 | bool barrier_sync_next(Barrier *b); | |
70 | bool barrier_sync(Barrier *b); | |
71 | ||
72 | static inline bool barrier_i_aborted(Barrier *b) { | |
3742095b | 73 | return IN_SET(b->barriers, BARRIER_I_ABORTED, BARRIER_WE_ABORTED); |
279da1e3 DH |
74 | } |
75 | ||
76 | static inline bool barrier_they_aborted(Barrier *b) { | |
3742095b | 77 | return IN_SET(b->barriers, BARRIER_THEY_ABORTED, BARRIER_WE_ABORTED); |
279da1e3 DH |
78 | } |
79 | ||
80 | static inline bool barrier_we_aborted(Barrier *b) { | |
81 | return b->barriers == BARRIER_WE_ABORTED; | |
82 | } | |
83 | ||
84 | static inline bool barrier_is_aborted(Barrier *b) { | |
3742095b AR |
85 | return IN_SET(b->barriers, BARRIER_I_ABORTED, BARRIER_THEY_ABORTED, |
86 | BARRIER_WE_ABORTED); | |
279da1e3 DH |
87 | } |
88 | ||
89 | static inline bool barrier_place_and_sync(Barrier *b) { | |
dc751688 | 90 | (void) barrier_place(b); |
279da1e3 DH |
91 | return barrier_sync(b); |
92 | } |