GNUnet  0.11.x
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  */
27 
29 {
30  unsigned int requirement;
32  void *cls;
33 
36 
38 };
39 
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,
65  int status);
66 
67 static void
69 {
71 }
72 
73 static void
75 {
77 }
78 
79 void
81 {
82  if ((!barrier) || (barrier->task))
83  return;
84 
85  barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier);
86 }
87 
89 {
91  void *cls;
92 
95 
97 };
98 
99 static void
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 
114  GNUNET_free(current);
115  }
116 
117  if (barrier->cb)
119 
121 }
122 
126  void *cb_cls)
127 {
128  if ((!barrier) || (0 == barrier->requirement))
129  return NULL;
130 
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 
143  barrier->requirement--;
144 
145  if ((barrier->requirement == 0) && (!barrier->task))
147 
148  return waiting;
149 }
150 
151 void
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 }
uint16_t status
See PRISM_STATUS_*-constants.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#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.
#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:1296
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
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:135
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().