chiark / gitweb /
unit-name: fix escaping logic in unit_name_mangle_with_suffix()
[elogind.git] / src / shared / 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 <errno.h>
25 #include <inttypes.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29
30 #include "macro.h"
31 #include "util.h"
32
33 /* See source file for an API description. */
34
35 typedef struct Barrier Barrier;
36
37 enum {
38         BARRIER_SINGLE                  = 1LL,
39         BARRIER_ABORTION                = INT64_MAX,
40
41         /* bias values to store state; keep @WE < @THEY < @I */
42         BARRIER_BIAS                    = INT64_MIN,
43         BARRIER_WE_ABORTED              = BARRIER_BIAS + 1LL,
44         BARRIER_THEY_ABORTED            = BARRIER_BIAS + 2LL,
45         BARRIER_I_ABORTED               = BARRIER_BIAS + 3LL,
46 };
47
48 enum {
49         BARRIER_PARENT,
50         BARRIER_CHILD,
51 };
52
53 struct Barrier {
54         int me;
55         int them;
56         int pipe[2];
57         int64_t barriers;
58 };
59
60 #define BARRIER_NULL {-1, -1, {-1, -1}, 0}
61
62 int barrier_create(Barrier *obj);
63 void barrier_destroy(Barrier *b);
64
65 DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
66
67 void barrier_set_role(Barrier *b, unsigned int role);
68
69 bool barrier_place(Barrier *b);
70 bool barrier_abort(Barrier *b);
71
72 bool barrier_wait_next(Barrier *b);
73 bool barrier_wait_abortion(Barrier *b);
74 bool barrier_sync_next(Barrier *b);
75 bool barrier_sync(Barrier *b);
76
77 static inline bool barrier_i_aborted(Barrier *b) {
78         return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_WE_ABORTED;
79 }
80
81 static inline bool barrier_they_aborted(Barrier *b) {
82         return b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
83 }
84
85 static inline bool barrier_we_aborted(Barrier *b) {
86         return b->barriers == BARRIER_WE_ABORTED;
87 }
88
89 static inline bool barrier_is_aborted(Barrier *b) {
90         return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
91 }
92
93 static inline bool barrier_place_and_sync(Barrier *b) {
94         (void)barrier_place(b);
95         return barrier_sync(b);
96 }