]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/barrier.h
Merge pull request #653 from dvdhrm/bus-gold
[thirdparty/systemd.git] / src / basic / barrier.h
CommitLineData
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
30typedef struct Barrier Barrier;
31
32enum {
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
43enum {
44 BARRIER_PARENT,
45 BARRIER_CHILD,
46};
47
48struct 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
57int barrier_create(Barrier *obj);
279da1e3
DH
58void barrier_destroy(Barrier *b);
59
fc808616
DH
60DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
61
279da1e3
DH
62void barrier_set_role(Barrier *b, unsigned int role);
63
64bool barrier_place(Barrier *b);
65bool barrier_abort(Barrier *b);
66
67bool barrier_wait_next(Barrier *b);
68bool barrier_wait_abortion(Barrier *b);
69bool barrier_sync_next(Barrier *b);
70bool barrier_sync(Barrier *b);
71
72static inline bool barrier_i_aborted(Barrier *b) {
73 return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_WE_ABORTED;
74}
75
76static inline bool barrier_they_aborted(Barrier *b) {
77 return b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
78}
79
80static inline bool barrier_we_aborted(Barrier *b) {
81 return b->barriers == BARRIER_WE_ABORTED;
82}
83
84static inline bool barrier_is_aborted(Barrier *b) {
85 return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
86}
87
88static inline bool barrier_place_and_sync(Barrier *b) {
dc751688 89 (void) barrier_place(b);
279da1e3
DH
90 return barrier_sync(b);
91}