GNUnet  0.20.0
testing_messenger_barrier.h File Reference

Pseudo-barriers for simple event handling. More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for testing_messenger_barrier.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef 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(). More...
 
typedef 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(). More...
 

Functions

struct GNUNET_BarrierHandleGNUNET_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 (requirement) reach the pseudo-barrier OR upon error. More...
 
void GNUNET_cancel_barrier (struct GNUNET_BarrierHandle *barrier)
 Cancel a pseudo-barrier. More...
 
struct GNUNET_BarrierWaitHandleGNUNET_wait_barrier (struct GNUNET_BarrierHandle *barrier, GNUNET_BarrierWaitStatusCallback cb, void *cb_cls)
 Wait for a pseudo-barrier to be crossed. More...
 
void GNUNET_cancel_wait_barrier (struct GNUNET_BarrierWaitHandle *waiting)
 Cancel a pseudo-barrier wait handle. More...
 

Detailed Description

Pseudo-barriers for simple event handling.

Author
Tobias Frisch

Definition in file testing_messenger_barrier.h.

Typedef Documentation

◆ GNUNET_BarrierStatusCallback

typedef 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().

The callback will be called when status information is available for the pseudo-barrier.

Parameters
clsthe closure given to GNUNET_init_barrier()
barrierthe pseudo-barrier handle
statusstatus of the pseudo-barrier. The pseudo-barrier is removed once it has been crossed or an error occurs while processing it. Therefore it is invalid to call GNUNET_cancel_barrier() on a crossed or errored pseudo-barrier.

Definition at line 50 of file testing_messenger_barrier.h.

◆ GNUNET_BarrierWaitStatusCallback

typedef 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().

The callback will be called when the pseudo-barrier corresponding given in GNUNET_wait_barrier() is crossed or cancelled.

Parameters
clsclosure pointer given to GNUNET_wait_barrier()
waitingthe pseudo-barrier wait handle
statusGNUNET_SYSERR in case of error while waiting for the pseudo-barrier; GNUNET_OK if the pseudo-barrier is crossed

Definition at line 99 of file testing_messenger_barrier.h.

Function Documentation

◆ GNUNET_init_barrier()

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 (requirement) reach the pseudo-barrier OR upon error.

Parameters
requirementthe amount of peers that is required to reach the pseudo-barrier. Peers signal reaching a pseudo-barrier by calling GNUNET_wait_barrier().
cbthe callback to call when the pseudo-barrier is reached or upon error. Can be NULL.
cb_clsclosure for the above callback
Returns
pseudo-barrier handle; NULL upon error

Definition at line 42 of file testing_messenger_barrier.c.

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 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_BarrierWaitHandle * head
struct GNUNET_BarrierWaitHandle * tail
GNUNET_BarrierStatusCallback cb
struct GNUNET_SCHEDULER_Task * task

References GNUNET_BarrierHandle::cb, GNUNET_BarrierHandle::cls, GNUNET_new, GNUNET_BarrierHandle::head, GNUNET_BarrierHandle::requirement, GNUNET_BarrierHandle::tail, and GNUNET_BarrierHandle::task.

Referenced by barrier_cb(), and GNUNET_run_messenger_setup().

Here is the caller graph for this function:

◆ GNUNET_cancel_barrier()

void GNUNET_cancel_barrier ( struct GNUNET_BarrierHandle barrier)

Cancel a pseudo-barrier.

Parameters
barrierthe pseudo-barrier handle

Definition at line 81 of file testing_messenger_barrier.c.

82 {
83  if ((!barrier) || (barrier->task))
84  return;
85 
86  barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier);
87 }
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:1299
static void cancel_barrier(void *cls)

References cancel_barrier(), GNUNET_SCHEDULER_add_now(), and GNUNET_BarrierHandle::task.

Referenced by shutdown_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_wait_barrier()

struct GNUNET_BarrierWaitHandle* GNUNET_wait_barrier ( struct GNUNET_BarrierHandle barrier,
GNUNET_BarrierWaitStatusCallback  cb,
void *  cb_cls 
)

Wait for a pseudo-barrier to be crossed.

This function should be called for the peers which have been started by the testbed.

Parameters
barrierthe pseudo-barrier handle
cbthe pseudo-barrier wait callback
cb_clsthe closure for the above callback
Returns
pseudo-barrier wait handle which can be used to cancel the waiting at anytime before the callback is called. NULL upon error.

Definition at line 125 of file testing_messenger_barrier.c.

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 }
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
GNUNET_BarrierWaitStatusCallback cb
struct GNUNET_BarrierHandle * barrier
struct GNUNET_BarrierWaitHandle * prev
struct GNUNET_BarrierWaitHandle * next
static void complete_barrier(void *cls)

References GNUNET_BarrierWaitHandle::barrier, GNUNET_BarrierWaitHandle::cb, GNUNET_BarrierWaitHandle::cls, complete_barrier(), GNUNET_CONTAINER_DLL_insert_tail, GNUNET_new, GNUNET_SCHEDULER_add_now(), GNUNET_BarrierHandle::head, GNUNET_BarrierWaitHandle::next, GNUNET_BarrierWaitHandle::prev, GNUNET_BarrierHandle::requirement, GNUNET_BarrierHandle::tail, and GNUNET_BarrierHandle::task.

Referenced by on_message(), and on_peer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_cancel_wait_barrier()

void GNUNET_cancel_wait_barrier ( struct GNUNET_BarrierWaitHandle waiting)

Cancel a pseudo-barrier wait handle.

Should not be called in or after the callback given to GNUNET_wait_barrier() has been called.

Parameters
waitingthe pseudo-barrier wait handle

Definition at line 153 of file testing_messenger_barrier.c.

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 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975

References GNUNET_BarrierWaitHandle::barrier, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_SCHEDULER_cancel(), GNUNET_BarrierHandle::head, GNUNET_BarrierHandle::requirement, GNUNET_BarrierHandle::tail, and GNUNET_BarrierHandle::task.

Referenced by on_message(), and shutdown_cb().

Here is the call graph for this function:
Here is the caller graph for this function: