adamc@1268: #include "config.h"
adamc@1268: 
adamc@859: #include <stdlib.h>
adamc@859: 
adamc@859: #include <pthread.h>
adamc@859: 
adamc@859: typedef struct node {
adamc@859:   int fd;
adamc@859:   struct node *next;
adamc@859: } *node;
adamc@859: 
adamc@859: static node front = NULL, back = NULL;
adamc@859: 
adamc@859: static int empty() {
adamc@859:   return front == NULL;
adamc@859: }
adamc@859: 
adamc@859: static void enqueue(int fd) {
adamc@859:   node n = malloc(sizeof(struct node));
adamc@859: 
adamc@859:   n->fd = fd;
adamc@859:   n->next = NULL;
adamc@859:   if (back)
adamc@859:     back->next = n;
adamc@859:   else
adamc@859:     front = n;
adamc@859:   back = n;
adamc@859: }
adamc@859: 
adamc@859: static int dequeue() {
adamc@859:   int ret = front->fd;
marco-oweber@1330:   node n = front->next;
marco-oweber@1330:   free(front);
adamc@859: 
marco-oweber@1330:   front = n;
marco-oweber@1330: 
adamc@859:   if (!front)
adamc@859:     back = NULL;
adamc@859: 
adamc@859:   return ret;
adamc@859: }
adamc@859: 
adamc@859: static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@859: static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@859: 
adamc@859: int uw_dequeue() {
adamc@859:   int sock;
adamc@859: 
adamc@859:   pthread_mutex_lock(&queue_mutex);
adamc@859:   while (empty())
adamc@859:     pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@859:   sock = dequeue();
adamc@859:   pthread_mutex_unlock(&queue_mutex);
adamc@859: 
adamc@859:   return sock;
adamc@859: }
adamc@859: 
adamc@859: void uw_enqueue(int new_fd) {
adamc@859:   pthread_mutex_lock(&queue_mutex);
adamc@859:   enqueue(new_fd);
adamc@859:   pthread_cond_broadcast(&queue_cond);
adamc@859:   pthread_mutex_unlock(&queue_mutex);
adamc@859: }