GNUnet  0.11.x
Typedefs | Functions
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 51 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 100 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.
clsclosure for the above callback
Returns
pseudo-barrier handle; NULL upon error

Definition at line 41 of file testing_messenger_barrier.c.

References GNUNET_BarrierHandle::cb, GNUNET_BarrierHandle::cls, exit_status(), GNUNET_new, GNUNET_BarrierHandle::head, GNUNET_BarrierHandle::requirement, status, GNUNET_BarrierHandle::tail, and GNUNET_BarrierHandle::task.

Referenced by barrier_cb(), and GNUNET_run_messenger_setup().

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 }
GNUNET_BarrierStatusCallback cb
struct GNUNET_BarrierWaitHandle * tail
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_BarrierWaitHandle * head
struct GNUNET_SCHEDULER_Task * task
Here is the call graph for this function:
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 79 of file testing_messenger_barrier.c.

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

Referenced by shutdown_cb().

80 {
81  if ((!barrier) || (barrier->task))
82  return;
83 
84  barrier->task = GNUNET_SCHEDULER_add_now(cancel_barrier, barrier);
85 }
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
struct GNUNET_SCHEDULER_Task * task
static void cancel_barrier(void *cls)
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
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 122 of file testing_messenger_barrier.c.

References GNUNET_BarrierWaitHandle::barrier, GNUNET_BarrierHandle::cb, 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().

125 {
126  if ((!barrier) || (0 == barrier->requirement))
127  return NULL;
128 
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 }
struct GNUNET_BarrierWaitHandle * next
struct GNUNET_BarrierWaitHandle * prev
static void complete_barrier(void *cls)
struct GNUNET_BarrierWaitHandle * tail
GNUNET_BarrierWaitStatusCallback cb
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_BarrierWaitHandle * head
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
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
struct GNUNET_SCHEDULER_Task * task
struct GNUNET_BarrierHandle * barrier
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 150 of file testing_messenger_barrier.c.

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

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 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_BarrierWaitHandle * tail
struct GNUNET_BarrierWaitHandle * head
struct GNUNET_SCHEDULER_Task * task
struct GNUNET_BarrierHandle * barrier
#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:972
Here is the call graph for this function:
Here is the caller graph for this function: