annotate src/c/queue.c @ 922:d6a71f19a3d8

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