c-template
wait_group.h
Go to the documentation of this file.
1 /*! @file wait_group.h
2  * @author Bonedaddy
3  * @brief provides C equivalent of Golang sync.Wait
4  * enables "signalling" multiple threads
5  * allows management of multiple threads, and enabling clean exits or cleanups
6  * it is a bit different than joining pthreads to wait for processes to exit
7  * as it doesn't require attached pthreads (can be used with detached) for synchronizatiion
8  * @todo:
9  * - determine if this is more efficient than using thread joining
10  * - expand signalling capabilities to enable a channel
11 */
12 
13 #pragma once
14 
15 #include <stdio.h>
16 #include <pthread.h>
17 
18 /*! @typedef wait_group_t
19  * @brief a simple structure of a pthread mutex and integer counter
20 */
21 typedef struct wait_group_t {
23  pthread_mutex_t mutex;
24  pthread_mutex_t cond_mutex;
25  pthread_cond_t cond_var;
26 } wait_group_t;
27 
28 /*! @brief incremements the total number of active processes managed by this wait group
29  * @param wg the waitgroup to manipulate
30  * @param count the number of new active processes
31 */
32 void wait_group_add(wait_group_t *wg, int count);
33 
34 /*! @brief used by a process to indicate it is done, decreasing the active process counter
35  * we include a return code here to catch errors when trying to decrement count below 0
36  * @pre wg->active_process must be greater than or equal to 1 otherwise return -1
37  * @warning in the future this may be changed so that calling wait_group_done with a 0 value active_processes will cause a program exit
38  * @param wg the waitgroup to manipulate a runtime error will occur and program will exit
39  * @return Failure: -1
40  * @return Success: 0
41  * @todo:
42  * - (bonedaddy) decide if we should do a runtime exit on pre condition failure
43 */
45 
46 /*! @brief used to wait until current active_processes reaches 0, polling every 0.75 seconds
47  * will run free on the wait_group_t pointer to clear up resources
48  * @param wg the waitgroup to wait on
49 */
51 
52 /*! @brief returns a new and initialized wait_group_t pointer
53 */
55 
56 
58 
wait_group_new
wait_group_t * wait_group_new()
returns a new and initialized wait_group_t pointer
Definition: wait_group.c:9
wait_group_done
int wait_group_done(wait_group_t *wg)
used by a process to indicate it is done, decreasing the active process counter we include a return c...
Definition: wait_group.c:70
wait_group_add
void wait_group_add(wait_group_t *wg, int count)
incremements the total number of active processes managed by this wait group
Definition: wait_group.c:64
wait_group_recv_signal
void wait_group_recv_signal(wait_group_t *wg)
Definition: wait_group.c:21
wait_group_t::active_processes
int active_processes
Definition: wait_group.h:22
wait_group_t::cond_mutex
pthread_mutex_t cond_mutex
Definition: wait_group.h:24
wait_group_listen_signal
void wait_group_listen_signal(wait_group_t *wg)
wait_group_t::mutex
pthread_mutex_t mutex
Definition: wait_group.h:23
wait_group_t
Definition: wait_group.h:21
wait_group_t
struct wait_group_t wait_group_t
a simple structure of a pthread mutex and integer counter
wait_group_t::cond_var
pthread_cond_t cond_var
Definition: wait_group.h:25
wait_group_send_signal
void wait_group_send_signal(wait_group_t *wg)
Definition: wait_group.c:27
wait_group_wait
void wait_group_wait(wait_group_t *wg)
used to wait until current active_processes reaches 0, polling every 0.75 seconds will run free on th...
Definition: wait_group.c:45