-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutex.h
37 lines (35 loc) · 959 Bytes
/
mutex.h
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
#ifndef _MUTEX_H
#define _MUTEX_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#define MT(ptr) ((pthread_mutex_t *)ptr)
#define MUTEX_INIT(mlock) \
do{ \
if((mlock = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t)))) \
pthread_mutex_init(MT(mlock), NULL); \
}while(0)
#define MUTEX_LOCK(mlock) ((mlock) ? pthread_mutex_lock(MT(mlock)): -1)
#define MUTEX_UNLOCK(mlock) ((mlock) ? pthread_mutex_unlock(MT(mlock)): -1)
#define MUTEX_DESTROY(mlock) \
do{ \
if(mlock) \
{ \
pthread_mutex_destroy(MT(mlock)); \
free(MT(mlock)); \
mlock = NULL; \
} \
}while(0)
#else
#define __MUTEX__WAIT__ 100000
#define MUTEX_INIT(mlock)
#define MUTEX_LOCK(mlock) (0)
#define MUTEX_UNLOCK(mlock) (0)
#define MUTEX_DESTROY(mlock)
#endif
#ifdef __cplusplus
}
#endif
#endif