summaryrefslogtreecommitdiff
path: root/wg.c
blob: 3208aa181362ea890393b8d15878681709a149e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h>
#include <error.h>
#include <errno.h>

#include "wg.h"

/* Thread management tools modeled on https://golang.org/pkg/sync/#WaitGroup */

/* pthread_cond_t is overly complicated. Just use a self-pipe. */

void wg_init(struct wg *wg) {
	wg->count = 0;
	pthread_mutex_init(&wg->lock, NULL);
	int fds[2];
	if (pipe(fds) != 0)
		error(1, errno, "pipe");
	wg->fd_wait = fds[0];
	wg->fd_signal = fds[1];
}

void wg_add(struct wg *wg, unsigned int n) {
	pthread_mutex_lock(&wg->lock);
	wg->count += n;
	pthread_mutex_unlock(&wg->lock);
}

void wg_sub(struct wg *wg, unsigned int n) {
	pthread_mutex_lock(&wg->lock);
	wg->count -= n;
	if (wg->count == 0)
		write(wg->fd_signal, " ", 1);
	pthread_mutex_unlock(&wg->lock);
}

void wg_wait(struct wg *wg) {
	char b;
 retry:
	if (read(wg->fd_wait, &b, 1) == -1) {
		if (errno == EINTR)
			goto retry;
		error(1, errno, "wg_wait");
	}
}