aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/testing_messenger_barrier.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/testing_messenger_barrier.c')
-rw-r--r--src/messenger/testing_messenger_barrier.c172
1 files changed, 0 insertions, 172 deletions
diff --git a/src/messenger/testing_messenger_barrier.c b/src/messenger/testing_messenger_barrier.c
deleted file mode 100644
index a95ea9ef1..000000000
--- a/src/messenger/testing_messenger_barrier.c
+++ /dev/null
@@ -1,172 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file messenger/testing_messenger_barrier.c
22 * @author Tobias Frisch
23 * @brief Pseudo-barriers for simple event handling
24 */
25
26#include "testing_messenger_barrier.h"
27
28struct GNUNET_BarrierHandle
29{
30 unsigned int requirement;
31 GNUNET_BarrierStatusCallback cb;
32 void *cls;
33
34 struct GNUNET_BarrierWaitHandle *head;
35 struct GNUNET_BarrierWaitHandle *tail;
36
37 struct GNUNET_SCHEDULER_Task* task;
38};
39
40struct GNUNET_BarrierHandle*
41GNUNET_init_barrier (unsigned int requirement,
42 GNUNET_BarrierStatusCallback cb,
43 void *cb_cls)
44{
45 if (0 == requirement)
46 return NULL;
47
48 struct GNUNET_BarrierHandle *barrier = GNUNET_new(struct GNUNET_BarrierHandle);
49
50 if (!barrier)
51 return NULL;
52
53 barrier->requirement = requirement;
54 barrier->cb = cb;
55 barrier->cls = cb_cls;
56 barrier->head = NULL;
57 barrier->tail = NULL;
58 barrier->task = NULL;
59
60 return barrier;
61}
62
63static void
64exit_status (struct GNUNET_BarrierHandle *barrier,
65 int status);
66
67static void
68cancel_barrier (void *cls)
69{
70 exit_status ((struct GNUNET_BarrierHandle*) cls, GNUNET_SYSERR);
71}
72
73static void
74complete_barrier (void *cls)
75{
76 exit_status ((struct GNUNET_BarrierHandle*) cls, GNUNET_OK);
77}
78
79void
80GNUNET_cancel_barrier (struct GNUNET_BarrierHandle *barrier)
81{
82 if ((!barrier) || (barrier->task))
83 return;
84
85 barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier);
86}
87
88struct GNUNET_BarrierWaitHandle
89{
90 GNUNET_BarrierWaitStatusCallback cb;
91 void *cls;
92
93 struct GNUNET_BarrierWaitHandle *prev;
94 struct GNUNET_BarrierWaitHandle *next;
95
96 struct GNUNET_BarrierHandle *barrier;
97};
98
99static void
100exit_status (struct GNUNET_BarrierHandle *barrier,
101 int status)
102{
103 struct GNUNET_BarrierWaitHandle *waiting = barrier->head;
104 while (waiting)
105 {
106 struct GNUNET_BarrierWaitHandle *current = waiting;
107
108 if (current->cb)
109 current->cb(current->cls, current, status);
110
111 waiting = waiting->next;
112
113 GNUNET_CONTAINER_DLL_remove(barrier->head, barrier->tail, current);
114 GNUNET_free(current);
115 }
116
117 if (barrier->cb)
118 barrier->cb(barrier->cls, barrier, status);
119
120 GNUNET_free(barrier);
121}
122
123struct GNUNET_BarrierWaitHandle*
124GNUNET_wait_barrier (struct GNUNET_BarrierHandle *barrier,
125 GNUNET_BarrierWaitStatusCallback cb,
126 void *cb_cls)
127{
128 if ((!barrier) || (0 == barrier->requirement))
129 return NULL;
130
131 struct GNUNET_BarrierWaitHandle *waiting = GNUNET_new(struct GNUNET_BarrierWaitHandle);
132
133 if (!waiting)
134 return NULL;
135
136 waiting->cb = cb;
137 waiting->cls = cb_cls;
138 waiting->prev = NULL;
139 waiting->next = NULL;
140 waiting->barrier = barrier;
141
142 GNUNET_CONTAINER_DLL_insert_tail(barrier->head, barrier->tail, waiting);
143 barrier->requirement--;
144
145 if ((barrier->requirement == 0) && (!barrier->task))
146 barrier->task = GNUNET_SCHEDULER_add_now(complete_barrier, barrier);
147
148 return waiting;
149}
150
151void
152GNUNET_cancel_wait_barrier (struct GNUNET_BarrierWaitHandle *waiting)
153{
154 if (!waiting)
155 return;
156
157 struct GNUNET_BarrierHandle *barrier = waiting->barrier;
158
159 if (!barrier)
160 return;
161
162 if ((barrier->requirement == 0) && (barrier->task))
163 {
164 GNUNET_SCHEDULER_cancel(barrier->task);
165 barrier->task = NULL;
166 }
167
168 barrier->requirement++;
169 GNUNET_CONTAINER_DLL_remove(barrier->head, barrier->tail, waiting);
170
171 GNUNET_free(waiting);
172}