view src/c/queue.c @ 1268:236dc296c32d

Include new config.h in C source files
author Adam Chlipala <adamc@hcoop.net>
date Sun, 30 May 2010 10:39:25 -0400
parents 60240acd15b9
children 452b14d88a10
line wrap: on
line source
#include "config.h"

#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);
}