Simple tools for multi threading / objects in plain C Snapshot
cbarrier.h
Go to the documentation of this file.
00001 /* Copyright (c) Michael Moser (2011) . 3-clause BSD License applies */
00002 
00003 #ifndef __CBARRIER_UTIL_X_Y_Z_
00004 #define __CBARRIER_UTIL_X_Y_Z_
00005 
00006 #include <pthread.h>
00007 
00008 /**
00009  @defgroup CBARRIER
00010  @ingroup  THREADPOOL
00011 
00012  @brief synchronization point for a fixed party of threads.
00013 
00014  Allows a fixed set of threads to wait for each other until they reach
00015  a common synchronization point. It is called cyclic becaus this object
00016  can be used several times (after calling CYCLIC_BARRIER_reset)
00017 
00018  A common synchronization point means that all threads in this party
00019  must call CYCLIC_BARRIER_await, and all threads block untill the others have called this function
00020 
00021  Idea is taken from java.util.concurrent.CyclicBarier - without waiting with timeouts that is.
00022  
00023  @{
00024  */
00025 typedef struct {
00026   pthread_mutex_t mutex;
00027   pthread_cond_t cond;
00028   int initial_count;
00029   int tcount;
00030   int is_finished;
00031   int left;
00032 } CYCLIC_BARRIER;
00033 
00034 
00035 /** 
00036 
00037   @brief initialises a new cyclic barrier
00038 
00039   @param cond object to initialise
00040   @param num number of parties (threads) to synchronze.
00041  */
00042 void CYCLIC_BARRIER_init(CYCLIC_BARRIER *cond, int num);
00043 
00044 /**
00045 
00046   @brief Frees a cyclic barrier.
00047 
00048   Precondition, all threads must have reached common synchronization point (all need to
00049   have called CYCLIC_BARRIER_await.
00050  
00051   @param cond - condition object
00052  
00053  */
00054 int  CYCLIC_BARRIER_free(CYCLIC_BARRIER *cond);
00055 
00056 /**
00057   @brief All threads call this function, nobody continues untill all have called this function
00058  
00059   @param cond - condition object
00060  */
00061 int  CYCLIC_BARRIER_await(CYCLIC_BARRIER *cond);
00062 
00063 
00064 /**
00065  @brief Returns cyclic barrier to it's initial state.
00066 
00067  Precondition, all parties are part CYCLIC_BARRIER_await.
00068  Postcondition, can use this cyclic barrier again (i.e. set of parties can do CYCLIC_BARRIER_await again).
00069 
00070  @param cond - condition object
00071 
00072  */
00073 int  CYCLIC_BARRIER_reset(CYCLIC_BARRIER *cond);
00074 
00075 /**
00076  @}
00077  */
00078 
00079 //------------------------------------------------------------------
00080 /*
00081 #ifdef _DO_DEBUG
00082 #include <stdio.h>
00083 #define DEBUGT(format, ... ) fprintf( stderr, "\t[%d] " format, getpid(), __VA_ARGS__ );
00084 #else
00085 #define DEBUGT(format, ... )
00086 #endif
00087 */
00088 
00089 #endif
00090