-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.c
More file actions
202 lines (164 loc) · 3.54 KB
/
list.c
File metadata and controls
202 lines (164 loc) · 3.54 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "list.h"
void* pop_front_c(struct list_t* restrict list, int* pass) {
struct node_t* ret = pop_front_n_c(list, pass);
if(ret) {
void* ret_data = ret->data;
free(ret->label);
free(ret);
return ret_data;
}
else
return NULL;
}
struct node_t* pop_front_n_c(struct list_t* restrict list, int* pass) {
assert(list);
pthread_mutex_lock(&(list->global_lock));
while(!list->size) {
pthread_cond_wait(&(list->work), &(list->global_lock));
if(list->stop) {
pthread_mutex_unlock(&(list->global_lock));
return NULL;
}
if(*pass) {
pthread_mutex_unlock(&(list->global_lock));
return NULL;
}
}
struct node_t *restrict ret = list->head;
switch(list->size) {
case 0:
break;
case 1:
list->tail = NULL;
list->head = NULL;
--list->size;
break;
default:
list->head = ret->next;
list->head->prev = NULL;
--list->size;
break;
}
pthread_mutex_unlock(&(list->global_lock));
return ret;
}
void* pop_front(struct list_t* restrict list) {
struct node_t* ret = pop_front_n(list);
if(ret) {
void* ret_data = ret->data;
free(ret->label);
free(ret);
return ret_data;
}
else
return NULL;
}
struct node_t* pop_front_n(struct list_t* restrict list) {
assert(list);
pthread_mutex_lock(&(list->global_lock));
while(!list->size) {
pthread_cond_wait(&(list->work), &(list->global_lock));
if(list->stop) {
pthread_mutex_unlock(&(list->global_lock));
return NULL;
}
}
struct node_t *restrict ret = list->head;
switch(list->size) {
case 0:
break;
case 1:
list->tail = NULL;
list->head = NULL;
--list->size;
break;
default:
list->head = ret->next;
list->head->prev = NULL;
--list->size;
break;
}
pthread_mutex_unlock(&(list->global_lock));
return ret;
}
void push_back(struct list_t* restrict list, void* restrict a, size_t s,
char * restrict label, int misc) {
assert(list);
struct node_t *restrict x = (struct node_t*) malloc( sizeof(struct node_t) );
assert(x);
x->data = (void * restrict) malloc(sizeof(char)*s);
assert(x->data);
memcpy(x->data, a, s);
x->size = s;
x->misc = misc;
if(label) {
size_t s = strlen(label) + 1;
x->label = (char* restrict) malloc(sizeof(char)*s);
memcpy(x->label, label, s);
}
else
x->label = NULL;
x->next = NULL;
// Take the lock as late as possible
pthread_mutex_lock(&(list->global_lock));
x->prev = list->tail;
if(list->tail != NULL)
list->tail->next = x;
list->tail = x;
if(!list->size) {
list->head = list->tail;
assert(list->head);
}
assert(list->tail);
++list->size;
pthread_mutex_unlock(&(list->global_lock));
pthread_cond_signal(&(list->work));
}
void destroy(struct list_t* restrict list) {
assert(list);
if(list->head) {
struct node_t *it = list->head,
*tmp = NULL;
for(; it != NULL; it = it->next) {
free(tmp);
if(it->data) {
free(it->data);
}
tmp = it;
}
if(tmp)
free(tmp);
}
}
void init(struct list_t* restrict list) {
list->head = NULL;
list->tail = NULL;
list->size = 0;
pthread_mutex_init(&(list->global_lock), NULL);
pthread_cond_init(&(list->work), NULL);
}
struct node_t* getnode(struct list_t* restrict list, size_t i) {
struct node_t *it;
size_t j = 0;
for(it = list->head; it != NULL; ++j) {
if( i == j ) {
if(it->data != NULL) {
return it;
}
else
--j;
}
it = it->next;
}
return NULL;
}
struct node_t* getval_n(struct list_t* restrict list, size_t i) {
assert(list);
assert(list->head);
assert(i < list->size);
if(i >= list->size) {
return NULL;
}
struct node_t *ptr = getnode(list,i);
return ptr;
}