]> git.proxmox.com Git - systemd.git/blame - src/shared/barrier.h
Imported Upstream version 220
[systemd.git] / src / shared / barrier.h
CommitLineData
5eef597e
MP
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
5eef597e
MP
24#include <sys/types.h>
25
26#include "macro.h"
5eef597e
MP
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
55#define BARRIER_NULL {-1, -1, {-1, -1}, 0}
56
57int barrier_create(Barrier *obj);
58void barrier_destroy(Barrier *b);
59
60DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
61
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) {
e3bff60a 89 (void) barrier_place(b);
5eef597e
MP
90 return barrier_sync(b);
91}