-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwqueue.c
55 lines (43 loc) · 1.08 KB
/
wqueue.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
/* wait queues, used to queue file descriptors with requests for a lock */
#include <stdlib.h>
#include "wqueue.h"
/* adds a fd to the given waitqueue; always to the tail */
struct wqentry *wq_add(struct wqentry *wq, int fd) {
struct wqentry *new;
struct wqentry *aux;
new = (struct wqentry *) malloc(sizeof(struct wqentry));
new->fd = fd;
new->next = NULL;
/* if wq is NULL, it means it's empty, so we just replace it with our
* new one */
if (wq == NULL) {
return new;
} else {
/* go to the last one */
for(aux = wq; aux->next != NULL; aux = aux->next)
;
aux->next = new;
}
return wq;
}
/* removes a given fd from the waitqueue */
struct wqentry *wq_del(struct wqentry *wq, int fd) {
struct wqentry *p, *prev;
prev = NULL;
/* go through the list looking */
for(p = wq; p != NULL; p = p->next) {
if (p->fd == fd) {
if (prev == NULL) {
/* the first one is the match, advance wq */
wq = p->next;
} else {
prev->next = p->next;
}
free(p);
return wq;
}
prev = p;
}
/* we couldn't find a match, return the same queue */
return wq;
}