blob: 645f69e32102b26ff3fa0a1beced00d1dd1764e5 (
plain)
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
|
#include "config.h"
#include <stdlib.h>
#include <pthread.h>
typedef struct node {
int fd;
struct node *next;
} *node;
static node front = NULL, back = NULL;
static int empty() {
return front == NULL;
}
static void enqueue(int fd) {
node n = malloc(sizeof(struct node));
n->fd = fd;
n->next = NULL;
if (back)
back->next = n;
else
front = n;
back = n;
}
static int dequeue() {
int ret = front->fd;
node n = front->next;
free(front);
front = n;
if (!front)
back = NULL;
return ret;
}
static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
int uw_dequeue() {
int sock;
pthread_mutex_lock(&queue_mutex);
while (empty())
pthread_cond_wait(&queue_cond, &queue_mutex);
sock = dequeue();
pthread_mutex_unlock(&queue_mutex);
return sock;
}
void uw_enqueue(int new_fd) {
pthread_mutex_lock(&queue_mutex);
enqueue(new_fd);
pthread_cond_broadcast(&queue_cond);
pthread_mutex_unlock(&queue_mutex);
}
|