]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/barrier.h
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / shared / barrier.h
CommitLineData
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
12typedef struct Barrier Barrier;
13
14enum {
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
25enum {
26 BARRIER_PARENT,
27 BARRIER_CHILD,
28};
29
30struct 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
39int barrier_create(Barrier *obj);
279da1e3
DH
40void barrier_destroy(Barrier *b);
41
fc808616
DH
42DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
43
14cb109d 44void barrier_set_role(Barrier *b, unsigned role);
279da1e3
DH
45
46bool barrier_place(Barrier *b);
47bool barrier_abort(Barrier *b);
48
49bool barrier_wait_next(Barrier *b);
50bool barrier_wait_abortion(Barrier *b);
51bool barrier_sync_next(Barrier *b);
52bool barrier_sync(Barrier *b);
53
54static inline bool barrier_i_aborted(Barrier *b) {
3742095b 55 return IN_SET(b->barriers, BARRIER_I_ABORTED, BARRIER_WE_ABORTED);
279da1e3
DH
56}
57
58static inline bool barrier_they_aborted(Barrier *b) {
3742095b 59 return IN_SET(b->barriers, BARRIER_THEY_ABORTED, BARRIER_WE_ABORTED);
279da1e3
DH
60}
61
62static inline bool barrier_we_aborted(Barrier *b) {
63 return b->barriers == BARRIER_WE_ABORTED;
64}
65
66static 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
71static inline bool barrier_place_and_sync(Barrier *b) {
dc751688 72 (void) barrier_place(b);
279da1e3
DH
73 return barrier_sync(b);
74}