-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrct_locklist.c
126 lines (95 loc) · 2.03 KB
/
rct_locklist.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "rct_core.h"
locklist *locklist_create(void)
{
locklist *llist;
llist = rct_alloc(sizeof(*llist));
if(llist == NULL)
{
return NULL;
}
pthread_mutex_init(&llist->lmutex,NULL);
llist->l = listCreate();
if(llist->l == NULL)
{
locklist_free(llist);
return NULL;
}
return llist;
}
int locklist_push(void *l, void *value)
{
locklist *llist = l;
if(llist == NULL || llist->l == NULL)
{
return RCT_ERROR;
}
pthread_mutex_lock(&llist->lmutex);
listAddNodeTail(llist->l, value);
pthread_mutex_unlock(&llist->lmutex);
return RCT_OK;
}
void *locklist_pop(void *l)
{
locklist *llist = l;
listNode *node;
void *value;
if(llist == NULL || llist->l == NULL)
{
return NULL;
}
pthread_mutex_lock(&llist->lmutex);
node = listFirst(llist->l);
if(node == NULL)
{
pthread_mutex_unlock(&llist->lmutex);
return NULL;
}
value = listNodeValue(node);
listDelNode(llist->l, node);
pthread_mutex_unlock(&llist->lmutex);
return value;
}
void locklist_free(void *l)
{
locklist *llist = l;
if(llist == NULL)
{
return;
}
if(llist->l != NULL)
{
listRelease(llist->l);
}
pthread_mutex_destroy(&llist->lmutex);
rct_free(llist);
}
long long locklist_length(void *l)
{
locklist *llist = l;
long long length;
if(llist == NULL || llist->l == NULL)
{
return -1;
}
pthread_mutex_lock(&llist->lmutex);
length = listLength(llist->l);
pthread_mutex_unlock(&llist->lmutex);
return length;
}
int mttlist_init_with_locklist(mttlist *list)
{
if(list == NULL)
{
return RCT_ERROR;
}
list->l = locklist_create();
if(list->l == NULL)
{
return RCT_ERROR;
}
list->lock_push = locklist_push;
list->lock_pop = locklist_pop;
list->free = locklist_free;
list->length = locklist_length;
return RCT_OK;
}