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