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