-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwq.c
52 lines (38 loc) · 1.24 KB
/
wq.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
#include <stdlib.h>
#include "wq.h"
#include "utlist.h"
/* Initializes a work queue WQ. */
void wq_init(wq_t *wq) {
/* TODO: Make me thread-safe! */
wq->size = 0;
wq->head = NULL;
pthread_cond_init(&wq->work_cond, NULL);
pthread_mutex_init(&wq->work_mut, NULL);
}
/* Remove an item from the WQ. This function should block until there
* is at least one item on the queue. */
int wq_pop(wq_t *wq) {
/* TODO: Make me blocking and thread-safe! */
pthread_mutex_lock(&wq->work_mut); // lock
wq_item_t *wq_item = wq->head;
if (wq->head == NULL) {
pthread_cond_wait(&wq->work_cond, &wq->work_mut); // suspend until the queue is non-empty
}
int client_socket_fd = wq->head->client_socket_fd;
wq->size--;
DL_DELETE(wq->head, wq->head);
free(wq_item);
pthread_mutex_unlock(&wq->work_mut); // unlock
return client_socket_fd;
}
/* Add ITEM to WQ. */
void wq_push(wq_t *wq, int client_socket_fd) {
/* TODO: Make me thread-safe! */
pthread_mutex_lock(&wq->work_mut); // lock
wq_item_t *wq_item = calloc(1, sizeof(wq_item_t));
wq_item->client_socket_fd = client_socket_fd;
DL_APPEND(wq->head, wq_item);
wq->size++;
pthread_cond_signal(&wq->work_cond); // wake a thread up
pthread_mutex_unlock(&wq->work_mut); // unlock
}