annotate src/c/queue.c @ 1327:1cc67fdac4d3

New argument to transactional free functions, to specify whether we are about to retry
author Adam Chlipala <adam@chlipala.net>
date Sat, 04 Dec 2010 11:15:20 -0500
parents 236dc296c32d
children 452b14d88a10
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adamc@859 3 #include <stdlib.h>
adamc@859 4
adamc@859 5 #include <pthread.h>
adamc@859 6
adamc@859 7 typedef struct node {
adamc@859 8 int fd;
adamc@859 9 struct node *next;
adamc@859 10 } *node;
adamc@859 11
adamc@859 12 static node front = NULL, back = NULL;
adamc@859 13
adamc@859 14 static int empty() {
adamc@859 15 return front == NULL;
adamc@859 16 }
adamc@859 17
adamc@859 18 static void enqueue(int fd) {
adamc@859 19 node n = malloc(sizeof(struct node));
adamc@859 20
adamc@859 21 n->fd = fd;
adamc@859 22 n->next = NULL;
adamc@859 23 if (back)
adamc@859 24 back->next = n;
adamc@859 25 else
adamc@859 26 front = n;
adamc@859 27 back = n;
adamc@859 28 }
adamc@859 29
adamc@859 30 static int dequeue() {
adamc@859 31 int ret = front->fd;
adamc@859 32
adamc@859 33 front = front->next;
adamc@859 34 if (!front)
adamc@859 35 back = NULL;
adamc@859 36
adamc@859 37 return ret;
adamc@859 38 }
adamc@859 39
adamc@859 40 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@859 41 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@859 42
adamc@859 43 int uw_dequeue() {
adamc@859 44 int sock;
adamc@859 45
adamc@859 46 pthread_mutex_lock(&queue_mutex);
adamc@859 47 while (empty())
adamc@859 48 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@859 49 sock = dequeue();
adamc@859 50 pthread_mutex_unlock(&queue_mutex);
adamc@859 51
adamc@859 52 return sock;
adamc@859 53 }
adamc@859 54
adamc@859 55 void uw_enqueue(int new_fd) {
adamc@859 56 pthread_mutex_lock(&queue_mutex);
adamc@859 57 enqueue(new_fd);
adamc@859 58 pthread_cond_broadcast(&queue_cond);
adamc@859 59 pthread_mutex_unlock(&queue_mutex);
adamc@859 60 }