From c72c82484a8ca39e6ca34c890b691c3cfab43105 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sat, 29 Jul 2023 06:28:33 -0400 Subject: [PATCH] Add job queue --- Makefile | 4 ++- include/jobq.h | 58 ++++++++++++++++++++++++++++++++++++++++ src/jobq.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 include/jobq.h create mode 100644 src/jobq.c diff --git a/Makefile b/Makefile index f2313c2..26ad343 100644 --- a/Makefile +++ b/Makefile @@ -38,9 +38,11 @@ libdir = $(DESTDIR)$(PREFIX)/lib hdrs += include/bytes.h hdrs += include/haggis.h +hdrs += include/jobq.h srcs += src/bytes.c srcs += src/haggis.c +srcs += src/jobq.c objs = $(srcs:.c=.o) @@ -48,7 +50,7 @@ CFLAGS += -Wall -Werror CFLAGS += -Iinclude CFLAGS += -fPIC -LIBS += -lmd +LIBS += -lmd -lpthread all: libhaggis.a libhaggis.so diff --git a/include/jobq.h b/include/jobq.h new file mode 100644 index 0000000..056b33f --- /dev/null +++ b/include/jobq.h @@ -0,0 +1,58 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)Copyright (c) 2023, Nathan D. Fisher. + * + * This is free software. It comes with NO WARRANTY. + * Permission to use, modify and distribute this source code + * is granted subject to the following conditions. + * 1/ that the above copyright notice and this notice + * are preserved in all copies and that due credit be given + * to the author. + * 2/ that any changes to this code are clearly commented + * as such so that the author does not get blamed for bugs + * other than his own. +*/ + +#ifndef JOBQ_H +#define JOBQ_H + +#include +#include +#include + +struct _job { + struct _job *prev; + char *fname; + struct _job *next; +}; + +typedef struct _job haggis_jobq_node; + +typedef struct { + sem_t *sem; + pthread_mutex_t *mutex; + haggis_jobq_node *head; + haggis_jobq_node *tail; +} haggis_jobq; + +int haggis_jobq_push(haggis_jobq *queue, char *fname); +char* haggis_jobq_pop(haggis_jobq *queue); + +#endif // !JOBQ_H diff --git a/src/jobq.c b/src/jobq.c new file mode 100644 index 0000000..23817b1 --- /dev/null +++ b/src/jobq.c @@ -0,0 +1,72 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)Copyright (c) 2023, Nathan D. Fisher. + * + * This is free software. It comes with NO WARRANTY. + * Permission to use, modify and distribute this source code + * is granted subject to the following conditions. + * 1/ that the above copyright notice and this notice + * are preserved in all copies and that due credit be given + * to the author. + * 2/ that any changes to this code are clearly commented + * as such so that the author does not get blamed for bugs + * other than his own. +*/ + +#include "jobq.h" + +int haggis_jobq_push(haggis_jobq *queue, char *fname) { + haggis_jobq_node *new = malloc(sizeof(haggis_jobq_node)); + if (new == NULL) + return 1; + new->fname = fname; + new->next = new->prev = NULL; + + pthread_mutex_lock(queue->mutex); + if (queue->tail == NULL) { + queue->tail = queue->head = new; + } else { + new->next = queue->tail; + queue->tail->prev = new; + queue->tail = new; + } + pthread_mutex_unlock(queue->mutex); + return sem_post(queue->sem); +} + +char* haggis_jobq_pop(haggis_jobq *queue) { + haggis_jobq_node *j; + char *fname; + + sem_wait(queue->sem); + pthread_mutex_lock(queue->mutex); + j = queue->head; + fname = j->fname; + if (queue->tail == queue->head) { + queue->tail = queue->head = NULL; + } else { + queue->head = queue->head->prev; + } + free(j); + pthread_mutex_unlock(queue->mutex); + if (sem_post(queue->sem) != 0) + return NULL; + return fname; +}