-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocker_pthread.c
46 lines (37 loc) · 1.25 KB
/
locker_pthread.c
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
44
45
46
#include "pthread.h"
#include "stdio.h"
#include "malloc.h"
#include "Locker.h"
typedef struct _PrivInfo{
pthread_mutex_t mutex;
}PrivInfo;
static enum Ret locker_pthread_lock(Locker* thiz){
PrivInfo* priv = (PrivInfo*)thiz->priv;
int ret = pthread_mutex_lock(&priv->mutex);
return ret == 0 ? RET_OK : RET_FAIL;
}
static enum Ret locker_pthread_unlock(Locker* thiz){
PrivInfo* priv = (PrivInfo*)thiz->priv;
int ret = pthread_mutex_unlock(&priv->mutex);
return ret == 0 ? RET_OK : RET_FAIL;
}
static enum Ret locker_pthread_destroy(Locker* thiz){
PrivInfo* priv = (PrivInfo*)thiz->priv;
int ret = locker_pthread_destroy(&priv->mutex);
return ret == 0 ? RET_OK : RET_FAIL;
}
Locker* locker_pthread_create(void){
Locker* thiz = (Locker*)malloc(sizeof(Locker) + sizeof(PrivInfo));
if(thiz != NULL){
PrivInfo* priv = (PrivInfo*)thiz->priv;
thiz->lock = locker_pthread_lock;
thiz->unlock = locker_pthread_unlock;
thiz->destroy = locker_pthread_destroy;
pthread_mutex_init(&(priv->mutex), NULL);
}
return thiz;
}
//single thread
//DList* dlist = dlist_create(NULL, NULL, NULL);
//multi thread
//DList* dlist = dlist_create(NULL, NULL, locker_pthread_create());