summaryrefslogtreecommitdiff
path: root/src/c/queue.c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-06-27 12:38:23 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-06-27 12:38:23 -0400
commit3f68c6c7c7a9d940624b594a02383e1f2d09643b (patch)
treeb3a424dbabd630f63bba97a1f80919800ee093fc /src/c/queue.c
parent1f9a8668b26293202f7d0511e160032a2f1c0634 (diff)
Successfully starting FastCGI sessions with Apache
Diffstat (limited to 'src/c/queue.c')
-rw-r--r--src/c/queue.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/c/queue.c b/src/c/queue.c
new file mode 100644
index 00000000..1c82e0dc
--- /dev/null
+++ b/src/c/queue.c
@@ -0,0 +1,58 @@
+#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;
+
+ front = front->next;
+ 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);
+}