comparison src/c/http.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 86ec89baee01
children 305bc0a431de
comparison
equal deleted inserted replaced
858:346cf1908a17 859:60240acd15b9
12 12
13 #include <pthread.h> 13 #include <pthread.h>
14 14
15 #include "urweb.h" 15 #include "urweb.h"
16 #include "request.h" 16 #include "request.h"
17 #include "queue.h"
17 18
18 int uw_backlog = 10; 19 int uw_backlog = 10;
19
20 typedef struct node {
21 int fd;
22 struct node *next;
23 } *node;
24
25 static node front = NULL, back = NULL;
26
27 static int empty() {
28 return front == NULL;
29 }
30
31 static void enqueue(int fd) {
32 node n = malloc(sizeof(struct node));
33
34 n->fd = fd;
35 n->next = NULL;
36 if (back)
37 back->next = n;
38 else
39 front = n;
40 back = n;
41 }
42
43 static int dequeue() {
44 int ret = front->fd;
45
46 front = front->next;
47 if (!front)
48 back = NULL;
49
50 return ret;
51 }
52
53 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
55 20
56 static char *get_header(void *data, const char *h) { 21 static char *get_header(void *data, const char *h) {
57 char *s = data; 22 char *s = data;
58 int len = strlen(h); 23 int len = strlen(h);
59 char *p; 24 char *p;
101 char *buf = malloc(buf_size); 66 char *buf = malloc(buf_size);
102 uw_request_context rc = uw_new_request_context(); 67 uw_request_context rc = uw_new_request_context();
103 68
104 while (1) { 69 while (1) {
105 char *back = buf; 70 char *back = buf;
106 int sock; 71 int sock = uw_dequeue();
107
108 pthread_mutex_lock(&queue_mutex);
109 while (empty())
110 pthread_cond_wait(&queue_cond, &queue_mutex);
111 sock = dequeue();
112 pthread_mutex_unlock(&queue_mutex);
113 72
114 printf("Handling connection with thread #%d.\n", me); 73 printf("Handling connection with thread #%d.\n", me);
115 74
116 while (1) { 75 while (1) {
117 int r; 76 int r;
365 return 1; 324 return 1;
366 } 325 }
367 326
368 printf("Accepted connection.\n"); 327 printf("Accepted connection.\n");
369 328
370 pthread_mutex_lock(&queue_mutex); 329 uw_enqueue(new_fd);
371 enqueue(new_fd); 330 }
372 pthread_cond_broadcast(&queue_cond); 331 }
373 pthread_mutex_unlock(&queue_mutex);
374 }
375 }