GNUnet  0.19.2
testing_messenger_barrier.c
Go to the documentation of this file.
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  */
26 #include "platform.h"
28 
30 {
31  unsigned int requirement;
33  void *cls;
34 
37 
39 };
40 
44  void *cb_cls)
45 {
46  if (0 == requirement)
47  return NULL;
48 
49  struct GNUNET_BarrierHandle *barrier = GNUNET_new(struct GNUNET_BarrierHandle);
50 
51  if (!barrier)
52  return NULL;
53 
54  barrier->requirement = requirement;
55  barrier->cb = cb;
56  barrier->cls = cb_cls;
57  barrier->head = NULL;
58  barrier->tail = NULL;
59  barrier->task = NULL;
60 
61  return barrier;
62 }
63 
64 static void
65 exit_status (struct GNUNET_BarrierHandle *barrier,
66  int status);
67 
68 static void
70 {
72 }
73 
74 static void
76 {
78 }
79 
80 void
82 {
83  if ((!barrier) || (barrier->task))
84  return;
85 
86  barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier);
87 }
88 
90 {
92  void *cls;
93 
96 
98 };
99 
100 static void
102  int status)
103 {
104  struct GNUNET_BarrierWaitHandle *waiting = barrier->head;
105  while (waiting)
106  {
107  struct GNUNET_BarrierWaitHandle *current = waiting;
108 
109  if (current->cb)
110  current->cb(current->cls, current, status);
111 
112  waiting = waiting->next;
113 
115  GNUNET_free(current);
116  }
117 
118  if (barrier->cb)
120 
122 }
123 
127  void *cb_cls)
128 {
129  if ((!barrier) || (0 == barrier->requirement))
130  return NULL;
131 
133 
134  if (!waiting)
135  return NULL;
136 
137  waiting->cb = cb;
138  waiting->cls = cb_cls;
139  waiting->prev = NULL;
140  waiting->next = NULL;
141  waiting->barrier = barrier;
142 
144  barrier->requirement--;
145 
146  if ((barrier->requirement == 0) && (!barrier->task))
148 
149  return waiting;
150 }
151 
152 void
154 {
155  if (!waiting)
156  return;
157 
158  struct GNUNET_BarrierHandle *barrier = waiting->barrier;
159 
160  if (!barrier)
161  return;
162 
163  if ((barrier->requirement == 0) && (barrier->task))
164  {
165  GNUNET_SCHEDULER_cancel(barrier->task);
166  barrier->task = NULL;
167  }
168 
169  barrier->requirement++;
170  GNUNET_CONTAINER_DLL_remove(barrier->head, barrier->tail, waiting);
171 
172  GNUNET_free(waiting);
173 }
uint16_t status
See PRISM_STATUS_*-constants.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1268
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:944
struct GNUNET_BarrierWaitHandle * head
struct GNUNET_BarrierWaitHandle * tail
GNUNET_BarrierStatusCallback cb
struct GNUNET_SCHEDULER_Task * task
GNUNET_BarrierWaitStatusCallback cb
struct GNUNET_BarrierHandle * barrier
struct GNUNET_BarrierWaitHandle * prev
struct GNUNET_BarrierWaitHandle * next
Entry in list of pending tasks.
Definition: scheduler.c:136
static void exit_status(struct GNUNET_BarrierHandle *barrier, int status)
void GNUNET_cancel_barrier(struct GNUNET_BarrierHandle *barrier)
Cancel a pseudo-barrier.
void GNUNET_cancel_wait_barrier(struct GNUNET_BarrierWaitHandle *waiting)
Cancel a pseudo-barrier wait handle.
struct GNUNET_BarrierHandle * GNUNET_init_barrier(unsigned int requirement, GNUNET_BarrierStatusCallback cb, void *cb_cls)
Initialise a pseudo-barrier and call the given callback when the required amount of peers (requiremen...
static void complete_barrier(void *cls)
static void cancel_barrier(void *cls)
struct GNUNET_BarrierWaitHandle * GNUNET_wait_barrier(struct GNUNET_BarrierHandle *barrier, GNUNET_BarrierWaitStatusCallback cb, void *cb_cls)
Wait for a pseudo-barrier to be crossed.
Pseudo-barriers for simple event handling.
void(* GNUNET_BarrierWaitStatusCallback)(void *cls, struct GNUNET_BarrierWaitHandle *waiting, int status)
Functions of this type are to be given as acallback argument to GNUNET_wait_barrier().
void(* GNUNET_BarrierStatusCallback)(void *cls, struct GNUNET_BarrierHandle *barrier, int status)
Functions of this type are to be given as callback argument to GNUNET_init_barrier().