diff options
Diffstat (limited to 'src/messenger/testing_messenger_barrier.c')
-rw-r--r-- | src/messenger/testing_messenger_barrier.c | 170 |
1 files changed, 0 insertions, 170 deletions
diff --git a/src/messenger/testing_messenger_barrier.c b/src/messenger/testing_messenger_barrier.c deleted file mode 100644 index 618d255b7..000000000 --- a/src/messenger/testing_messenger_barrier.c +++ /dev/null | |||
@@ -1,170 +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 | |||
28 | struct 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 | |||
40 | struct GNUNET_BarrierHandle* | ||
41 | GNUNET_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 | |||
63 | static void | ||
64 | exit_status (struct GNUNET_BarrierHandle *barrier, int status); | ||
65 | |||
66 | static void | ||
67 | cancel_barrier (void *cls) | ||
68 | { | ||
69 | exit_status ((struct GNUNET_BarrierHandle*) cls, GNUNET_SYSERR); | ||
70 | } | ||
71 | |||
72 | static void | ||
73 | complete_barrier (void *cls) | ||
74 | { | ||
75 | exit_status ((struct GNUNET_BarrierHandle*) cls, GNUNET_OK); | ||
76 | } | ||
77 | |||
78 | void | ||
79 | GNUNET_cancel_barrier (struct GNUNET_BarrierHandle *barrier) | ||
80 | { | ||
81 | if ((!barrier) || (barrier->task)) | ||
82 | return; | ||
83 | |||
84 | barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier); | ||
85 | } | ||
86 | |||
87 | struct GNUNET_BarrierWaitHandle | ||
88 | { | ||
89 | GNUNET_BarrierWaitStatusCallback cb; | ||
90 | void *cls; | ||
91 | |||
92 | struct GNUNET_BarrierWaitHandle *prev; | ||
93 | struct GNUNET_BarrierWaitHandle *next; | ||
94 | |||
95 | struct GNUNET_BarrierHandle *barrier; | ||
96 | }; | ||
97 | |||
98 | static void | ||
99 | exit_status (struct GNUNET_BarrierHandle *barrier, int status) | ||
100 | { | ||
101 | struct GNUNET_BarrierWaitHandle *waiting = barrier->head; | ||
102 | while (waiting) | ||
103 | { | ||
104 | struct GNUNET_BarrierWaitHandle *current = waiting; | ||
105 | |||
106 | if (current->cb) | ||
107 | current->cb(current->cls, current, status); | ||
108 | |||
109 | waiting = waiting->next; | ||
110 | |||
111 | GNUNET_CONTAINER_DLL_remove(barrier->head, barrier->tail, current); | ||
112 | GNUNET_free(current); | ||
113 | } | ||
114 | |||
115 | if (barrier->cb) | ||
116 | barrier->cb(barrier->cls, barrier, status); | ||
117 | |||
118 | GNUNET_free(barrier); | ||
119 | } | ||
120 | |||
121 | struct GNUNET_BarrierWaitHandle* | ||
122 | GNUNET_wait_barrier (struct GNUNET_BarrierHandle *barrier, | ||
123 | GNUNET_BarrierWaitStatusCallback cb, | ||
124 | void *cb_cls) | ||
125 | { | ||
126 | if ((!barrier) || (0 == barrier->requirement)) | ||
127 | return NULL; | ||
128 | |||
129 | struct GNUNET_BarrierWaitHandle *waiting = GNUNET_new(struct GNUNET_BarrierWaitHandle); | ||
130 | |||
131 | if (!waiting) | ||
132 | return NULL; | ||
133 | |||
134 | waiting->cb = cb; | ||
135 | waiting->cls = cb_cls; | ||
136 | waiting->prev = NULL; | ||
137 | waiting->next = NULL; | ||
138 | waiting->barrier = barrier; | ||
139 | |||
140 | GNUNET_CONTAINER_DLL_insert_tail(barrier->head, barrier->tail, waiting); | ||
141 | barrier->requirement--; | ||
142 | |||
143 | if ((barrier->requirement == 0) && (!barrier->task)) | ||
144 | barrier->task = GNUNET_SCHEDULER_add_now(complete_barrier, barrier); | ||
145 | |||
146 | return waiting; | ||
147 | } | ||
148 | |||
149 | void | ||
150 | GNUNET_cancel_wait_barrier (struct GNUNET_BarrierWaitHandle *waiting) | ||
151 | { | ||
152 | if (!waiting) | ||
153 | return; | ||
154 | |||
155 | struct GNUNET_BarrierHandle *barrier = waiting->barrier; | ||
156 | |||
157 | if (!barrier) | ||
158 | return; | ||
159 | |||
160 | if ((barrier->requirement == 0) && (barrier->task)) | ||
161 | { | ||
162 | GNUNET_SCHEDULER_cancel(barrier->task); | ||
163 | barrier->task = NULL; | ||
164 | } | ||
165 | |||
166 | barrier->requirement++; | ||
167 | GNUNET_CONTAINER_DLL_remove(barrier->head, barrier->tail, waiting); | ||
168 | |||
169 | GNUNET_free(waiting); | ||
170 | } | ||