Added thread safe work queue
This commit is contained in:
@@ -43,6 +43,7 @@ OBJECTFILES= \
|
||||
${OBJECTDIR}/src/http.o \
|
||||
${OBJECTDIR}/src/main.o \
|
||||
${OBJECTDIR}/src/mime.o \
|
||||
${OBJECTDIR}/src/queue.o \
|
||||
${OBJECTDIR}/src/socket.o \
|
||||
${OBJECTDIR}/src/util.o
|
||||
|
||||
@@ -61,7 +62,7 @@ FFLAGS=
|
||||
ASFLAGS=
|
||||
|
||||
# Link Libraries and Options
|
||||
LDLIBSOPTIONS=-lmagic
|
||||
LDLIBSOPTIONS=-lmagic -lpthread
|
||||
|
||||
# Build Targets
|
||||
.build-conf: ${BUILD_SUBPROJECTS}
|
||||
@@ -111,6 +112,11 @@ ${OBJECTDIR}/src/mime.o: nbproject/Makefile-${CND_CONF}.mk src/mime.c
|
||||
${RM} "$@.d"
|
||||
$(COMPILE.c) -g -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/mime.o src/mime.c
|
||||
|
||||
${OBJECTDIR}/src/queue.o: nbproject/Makefile-${CND_CONF}.mk src/queue.c
|
||||
${MKDIR} -p ${OBJECTDIR}/src
|
||||
${RM} "$@.d"
|
||||
$(COMPILE.c) -g -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/queue.o src/queue.c
|
||||
|
||||
${OBJECTDIR}/src/socket.o: nbproject/Makefile-${CND_CONF}.mk src/socket.c
|
||||
${MKDIR} -p ${OBJECTDIR}/src
|
||||
${RM} "$@.d"
|
||||
|
||||
@@ -43,6 +43,7 @@ OBJECTFILES= \
|
||||
${OBJECTDIR}/src/http.o \
|
||||
${OBJECTDIR}/src/main.o \
|
||||
${OBJECTDIR}/src/mime.o \
|
||||
${OBJECTDIR}/src/queue.o \
|
||||
${OBJECTDIR}/src/socket.o \
|
||||
${OBJECTDIR}/src/util.o
|
||||
|
||||
@@ -61,7 +62,7 @@ FFLAGS=
|
||||
ASFLAGS=
|
||||
|
||||
# Link Libraries and Options
|
||||
LDLIBSOPTIONS=-lmagic
|
||||
LDLIBSOPTIONS=-lmagic -lpthread
|
||||
|
||||
# Build Targets
|
||||
.build-conf: ${BUILD_SUBPROJECTS}
|
||||
@@ -111,6 +112,11 @@ ${OBJECTDIR}/src/mime.o: nbproject/Makefile-${CND_CONF}.mk src/mime.c
|
||||
${RM} "$@.d"
|
||||
$(COMPILE.c) -O2 -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/mime.o src/mime.c
|
||||
|
||||
${OBJECTDIR}/src/queue.o: nbproject/Makefile-${CND_CONF}.mk src/queue.c
|
||||
${MKDIR} -p ${OBJECTDIR}/src
|
||||
${RM} "$@.d"
|
||||
$(COMPILE.c) -O2 -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/queue.o src/queue.c
|
||||
|
||||
${OBJECTDIR}/src/socket.o: nbproject/Makefile-${CND_CONF}.mk src/socket.c
|
||||
${MKDIR} -p ${OBJECTDIR}/src
|
||||
${RM} "$@.d"
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<itemPath>lib/ini.h</itemPath>
|
||||
<itemPath>src/main.h</itemPath>
|
||||
<itemPath>src/mime.h</itemPath>
|
||||
<itemPath>src/queue.h</itemPath>
|
||||
<itemPath>src/socket.h</itemPath>
|
||||
<itemPath>src/util.h</itemPath>
|
||||
</logicalFolder>
|
||||
@@ -30,6 +31,7 @@
|
||||
<itemPath>lib/ini.c</itemPath>
|
||||
<itemPath>src/main.c</itemPath>
|
||||
<itemPath>src/mime.c</itemPath>
|
||||
<itemPath>src/queue.c</itemPath>
|
||||
<itemPath>src/socket.c</itemPath>
|
||||
<itemPath>src/util.c</itemPath>
|
||||
</logicalFolder>
|
||||
@@ -75,6 +77,7 @@
|
||||
<linkerTool>
|
||||
<linkerLibItems>
|
||||
<linkerLibLibItem>magic</linkerLibLibItem>
|
||||
<linkerLibStdlibItem>PosixThreads</linkerLibStdlibItem>
|
||||
</linkerLibItems>
|
||||
</linkerTool>
|
||||
</compileType>
|
||||
@@ -118,6 +121,10 @@
|
||||
</item>
|
||||
<item path="src/mime.h" ex="false" tool="3" flavor2="0">
|
||||
</item>
|
||||
<item path="src/queue.c" ex="false" tool="0" flavor2="0">
|
||||
</item>
|
||||
<item path="src/queue.h" ex="false" tool="3" flavor2="0">
|
||||
</item>
|
||||
<item path="src/socket.c" ex="false" tool="0" flavor2="0">
|
||||
</item>
|
||||
<item path="src/socket.h" ex="false" tool="3" flavor2="0">
|
||||
@@ -152,6 +159,7 @@
|
||||
<linkerTool>
|
||||
<linkerLibItems>
|
||||
<linkerLibLibItem>magic</linkerLibLibItem>
|
||||
<linkerLibStdlibItem>PosixThreads</linkerLibStdlibItem>
|
||||
</linkerLibItems>
|
||||
</linkerTool>
|
||||
</compileType>
|
||||
@@ -195,6 +203,10 @@
|
||||
</item>
|
||||
<item path="src/mime.h" ex="false" tool="3" flavor2="0">
|
||||
</item>
|
||||
<item path="src/queue.c" ex="false" tool="0" flavor2="0">
|
||||
</item>
|
||||
<item path="src/queue.h" ex="false" tool="3" flavor2="0">
|
||||
</item>
|
||||
<item path="src/socket.c" ex="false" tool="0" flavor2="0">
|
||||
</item>
|
||||
<item path="src/socket.h" ex="false" tool="3" flavor2="0">
|
||||
|
||||
@@ -8,20 +8,11 @@
|
||||
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
||||
<group>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/socket.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/util.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/config.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/content/dirindex.html</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/http-server.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/http.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/lib/ut/utstring.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/main.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/lib/http_parser.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/Makefile</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/main.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/socket.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/http-server.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/lib/ini.c</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/http.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/config.h</file>
|
||||
<file>file:/home/sam/NetBeansProjects/KHttp/src/util.c</file>
|
||||
</group>
|
||||
</open-files>
|
||||
</project-private>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <sys/mman.h>
|
||||
#include <ctype.h>
|
||||
#include <signal.h>
|
||||
#include <bits/stdio2.h>
|
||||
|
||||
#include "http_parser.h"
|
||||
|
||||
@@ -27,6 +28,7 @@
|
||||
#include "config.h"
|
||||
#include "http-server.h"
|
||||
#include "mime.h"
|
||||
#include "queue.h"
|
||||
|
||||
int serverfd = 0;
|
||||
volatile static bool stop = false;
|
||||
|
||||
78
src/queue.c
Normal file
78
src/queue.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "queue.h"
|
||||
#include "util.h"
|
||||
#include "ut/utlist.h"
|
||||
|
||||
queue_item* queue_item_new() {
|
||||
queue_item *item = calloc(1, sizeof(queue_item));
|
||||
return item;
|
||||
}
|
||||
void queue_item_delete(queue_item *item) {
|
||||
free(item);
|
||||
}
|
||||
|
||||
queue* queue_new() {
|
||||
queue *q = calloc(1, sizeof(queue));
|
||||
q->count = 0;
|
||||
q->list = NULL;
|
||||
q->mutex = calloc(1, sizeof(pthread_mutex_t));
|
||||
if (pthread_mutex_init(q->mutex, NULL) != 0) {
|
||||
fatal("Failed to init queue mutex");
|
||||
}
|
||||
q->cond = calloc(1, sizeof(pthread_cond_t));
|
||||
if (pthread_cond_init(q->cond, NULL) != 0) {
|
||||
fatal("Failed to init queue cond");
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
void queue_delete(queue *q) {
|
||||
queue_item *elem, *tmp;
|
||||
DL_FOREACH_SAFE(q->list, elem, tmp) {
|
||||
queue_item_delete(elem);
|
||||
DL_DELETE(q->list, elem);
|
||||
}
|
||||
pthread_mutex_destroy(q->mutex);
|
||||
free(q->mutex);
|
||||
pthread_cond_destroy(q->cond);
|
||||
free(q->cond);
|
||||
free(q);
|
||||
}
|
||||
int queue_add(queue *q, queue_item *item) {
|
||||
QUEUE_LOCK(q);
|
||||
DL_APPEND(q->list, item);
|
||||
q->count++;
|
||||
pthread_cond_signal(q->cond);
|
||||
QUEUE_UNLOCK(q);
|
||||
return 0;
|
||||
}
|
||||
int queue_remove(queue *q, queue_item *item) {
|
||||
QUEUE_LOCK(q);
|
||||
queue_item *elem, *tmp;
|
||||
DL_FOREACH_SAFE(q->list, elem, tmp) {
|
||||
if (elem == item) {
|
||||
DL_DELETE(q->list, elem);
|
||||
queue_item_delete(elem);
|
||||
break;
|
||||
}
|
||||
}
|
||||
q->count--;
|
||||
QUEUE_UNLOCK(q);
|
||||
}
|
||||
queue_item* queue_fetchone(queue *q, bool blocking) {
|
||||
queue_item *item = NULL;
|
||||
QUEUE_LOCK(q);
|
||||
if (q->count == 0 && blocking == true) {
|
||||
pthread_cond_wait(q->cond, q->mutex);
|
||||
}
|
||||
if (q->count > 0) {
|
||||
item = q->list;
|
||||
DL_DELETE(q->list, q->list);
|
||||
q->count--;
|
||||
}
|
||||
QUEUE_UNLOCK(q);
|
||||
return item;
|
||||
}
|
||||
60
src/queue.h
Normal file
60
src/queue.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* File: queue.h
|
||||
* Author: sam
|
||||
*
|
||||
* Created on 04 August 2014, 14:27
|
||||
*/
|
||||
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct queue_item {
|
||||
struct queue_item *prev;
|
||||
struct queue_item *next;
|
||||
char tag[16];
|
||||
void *data;
|
||||
} queue_item;
|
||||
|
||||
queue_item* queue_item_new();
|
||||
void queue_item_delete(queue_item *item);
|
||||
|
||||
typedef struct queue {
|
||||
queue_item *list;
|
||||
uint64_t count;
|
||||
pthread_mutex_t *mutex;
|
||||
pthread_cond_t *cond;
|
||||
} queue;
|
||||
|
||||
#define QUEUE_LOCK(q) \
|
||||
do { \
|
||||
if (pthread_mutex_lock(q->mutex)!=0) { \
|
||||
fatal("Could not lock queue"); \
|
||||
} \
|
||||
} while(0)
|
||||
#define QUEUE_UNLOCK(q) \
|
||||
do { \
|
||||
if (pthread_mutex_unlock(q->mutex)!=0) {\
|
||||
fatal("Could not unlock queue"); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
queue* queue_new();
|
||||
void queue_delete(queue *q);
|
||||
int queue_add(queue *q, queue_item *item);
|
||||
int queue_remove(queue *q, queue_item *item);
|
||||
queue_item* queue_fetchone(queue *q, bool blocking);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* QUEUE_H */
|
||||
|
||||
Reference in New Issue
Block a user