summaryrefslogtreecommitdiff
path: root/mutex_helpers.c
blob: e390316152d3e9b472f72a7af5575bcbe86c6142 (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
#include <stdlib.h>
#include <pthread.h>

/* Creating a locked mutex */
pthread_mutex_t*
mutex_create() {
	pthread_mutex_t* mu;

	if(!(mu = malloc(sizeof(pthread_mutex_t)))) {
		return NULL;
	}

	if(pthread_mutex_init(mu, NULL) != 0) {
		free(mu);
		return NULL;
	}

	pthread_mutex_trylock(mu);

	return mu;
}

/* Destroy a mutex created by mutex_create */
void
mutex_destroy(pthread_mutex_t* mu) {
	if(mu) {
		pthread_mutex_unlock(mu);
		pthread_mutex_destroy(mu);
	}
}