Line data Source code
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 <sys/types.h>
25 :
26 : #include "macro.h"
27 :
28 : /* See source file for an API description. */
29 :
30 : typedef struct Barrier Barrier;
31 :
32 : enum {
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 :
43 : enum {
44 : BARRIER_PARENT,
45 : BARRIER_CHILD,
46 : };
47 :
48 : struct 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 :
57 : int barrier_create(Barrier *obj);
58 : void barrier_destroy(Barrier *b);
59 :
60 1000 : DEFINE_TRIVIAL_CLEANUP_FUNC(Barrier*, barrier_destroy);
61 :
62 : void barrier_set_role(Barrier *b, unsigned int role);
63 :
64 : bool barrier_place(Barrier *b);
65 : bool barrier_abort(Barrier *b);
66 :
67 : bool barrier_wait_next(Barrier *b);
68 : bool barrier_wait_abortion(Barrier *b);
69 : bool barrier_sync_next(Barrier *b);
70 : bool barrier_sync(Barrier *b);
71 :
72 4000 : static inline bool barrier_i_aborted(Barrier *b) {
73 4000 : return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_WE_ABORTED;
74 : }
75 :
76 4000 : static inline bool barrier_they_aborted(Barrier *b) {
77 4000 : return b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
78 : }
79 :
80 : static inline bool barrier_we_aborted(Barrier *b) {
81 : return b->barriers == BARRIER_WE_ABORTED;
82 : }
83 :
84 10000 : static inline bool barrier_is_aborted(Barrier *b) {
85 10000 : return b->barriers == BARRIER_I_ABORTED || b->barriers == BARRIER_THEY_ABORTED || b->barriers == BARRIER_WE_ABORTED;
86 : }
87 :
88 2000 : static inline bool barrier_place_and_sync(Barrier *b) {
89 2000 : (void) barrier_place(b);
90 2000 : return barrier_sync(b);
91 : }
|