diff options
Diffstat (limited to 'mutex_helpers.c')
-rw-r--r-- | mutex_helpers.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/mutex_helpers.c b/mutex_helpers.c new file mode 100644 index 0000000..e390316 --- /dev/null +++ b/mutex_helpers.c @@ -0,0 +1,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); + } +} |