Simple tools for multi threading / objects in plain C Snapshot
|
synchronization point for a fixed party of threads. More...
Classes | |
struct | CYCLIC_BARRIER |
Functions | |
void | CYCLIC_BARRIER_init (CYCLIC_BARRIER *cond, int num) |
initialises a new cyclic barrier | |
int | CYCLIC_BARRIER_free (CYCLIC_BARRIER *cond) |
Frees a cyclic barrier. | |
int | CYCLIC_BARRIER_await (CYCLIC_BARRIER *cond) |
All threads call this function, nobody continues untill all have called this function. | |
int | CYCLIC_BARRIER_reset (CYCLIC_BARRIER *cond) |
Returns cyclic barrier to it's initial state. |
synchronization point for a fixed party of threads.
Allows a fixed set of threads to wait for each other until they reach a common synchronization point. It is called cyclic becaus this object can be used several times (after calling CYCLIC_BARRIER_reset)
A common synchronization point means that all threads in this party must call CYCLIC_BARRIER_await, and all threads block untill the others have called this function
Idea is taken from java.util.concurrent.CyclicBarier - without waiting with timeouts that is.
int CYCLIC_BARRIER_await | ( | CYCLIC_BARRIER * | cond | ) |
All threads call this function, nobody continues untill all have called this function.
cond | - condition object |
Definition at line 76 of file cbarrier.c.
{ int rt = 0; if ((rt = pthread_mutex_lock(&cond->mutex)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_mutex lock failed"); return -1; } if (! cond->is_finished ) { --cond->tcount; if (cond->tcount > 0) { if ((rt = pthread_cond_wait(&cond->cond,&cond->mutex)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_cond_wait failed"); } } else { cond-> is_finished = 1; if ((rt = pthread_cond_broadcast(&cond->cond)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_cond_broadcast failed"); } } } cond->left--; if ((rt = pthread_mutex_unlock(&cond->mutex)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_mutex_unlock failed"); } return rt; }
int CYCLIC_BARRIER_free | ( | CYCLIC_BARRIER * | cond | ) |
Frees a cyclic barrier.
Precondition, all threads must have reached common synchronization point (all need to have called CYCLIC_BARRIER_await.
cond | - condition object |
Definition at line 57 of file cbarrier.c.
{ int rt = 0; if (CYCLIC_BARRIER_wait_for_all_to_finish(cond)) { return -1; } if ((rt = pthread_cond_destroy( &cond->cond )) != 0 ) { errorp(rt, "CYCLIC_BARRIER: pthread_cond_destroy failed"); } if ((rt = pthread_mutex_destroy( &cond->mutex )) != 0 ) { errorp(rt, "CYCLIC_BARRIER: pthread_mutext destroy failed"); } return rt; }
void CYCLIC_BARRIER_init | ( | CYCLIC_BARRIER * | cond, |
int | num | ||
) |
initialises a new cyclic barrier
cond | object to initialise |
num | number of parties (threads) to synchronze. |
Definition at line 13 of file cbarrier.c.
{ int rt; cond->initial_count = num; cond->tcount = num; cond->is_finished = 0; cond->left = num; if ((rt = pthread_mutex_init(&cond->mutex,0)) != 0) { errorp( rt, "pthread_mutex init failed"); } if ((rt = pthread_cond_init(&cond->cond, 0)) != 0) { errorp( rt, "pthread_cond init failed"); } }
int CYCLIC_BARRIER_reset | ( | CYCLIC_BARRIER * | cond | ) |
Returns cyclic barrier to it's initial state.
Precondition, all parties are part CYCLIC_BARRIER_await. Postcondition, can use this cyclic barrier again (i.e. set of parties can do CYCLIC_BARRIER_await again).
cond | - condition object |
Definition at line 32 of file cbarrier.c.
{ int rt; if (CYCLIC_BARRIER_wait_for_all_to_finish(cond)) { return -1; } if ((rt = pthread_mutex_lock(&cond->mutex)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_mutex lock failed"); return -1; } cond->tcount = cond->initial_count; cond->left = cond->initial_count; cond->is_finished = 0; if ((rt = pthread_mutex_unlock(&cond->mutex)) != 0) { errorp(rt, "CYCLIC_BARRIER: pthread_mutex_unlock failed"); return -1; } return 0; }