comparison src/c/queue.c @ 859:60240acd15b9

Successfully starting FastCGI sessions with Apache
author Adam Chlipala <adamc@hcoop.net>
date Sat, 27 Jun 2009 12:38:23 -0400
parents
children 236dc296c32d
comparison
equal deleted inserted replaced
858:346cf1908a17 859:60240acd15b9
1 #include <stdlib.h>
2
3 #include <pthread.h>
4
5 typedef struct node {
6 int fd;
7 struct node *next;
8 } *node;
9
10 static node front = NULL, back = NULL;
11
12 static int empty() {
13 return front == NULL;
14 }
15
16 static void enqueue(int fd) {
17 node n = malloc(sizeof(struct node));
18
19 n->fd = fd;
20 n->next = NULL;
21 if (back)
22 back->next = n;
23 else
24 front = n;
25 back = n;
26 }
27
28 static int dequeue() {
29 int ret = front->fd;
30
31 front = front->next;
32 if (!front)
33 back = NULL;
34
35 return ret;
36 }
37
38 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
39 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
40
41 int uw_dequeue() {
42 int sock;
43
44 pthread_mutex_lock(&queue_mutex);
45 while (empty())
46 pthread_cond_wait(&queue_cond, &queue_mutex);
47 sock = dequeue();
48 pthread_mutex_unlock(&queue_mutex);
49
50 return sock;
51 }
52
53 void uw_enqueue(int new_fd) {
54 pthread_mutex_lock(&queue_mutex);
55 enqueue(new_fd);
56 pthread_cond_broadcast(&queue_cond);
57 pthread_mutex_unlock(&queue_mutex);
58 }