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