Add job queue
This commit is contained in:
parent
1d36b38fb4
commit
c72c82484a
3 changed files with 133 additions and 1 deletions
4
Makefile
4
Makefile
|
@ -38,9 +38,11 @@ libdir = $(DESTDIR)$(PREFIX)/lib
|
||||||
|
|
||||||
hdrs += include/bytes.h
|
hdrs += include/bytes.h
|
||||||
hdrs += include/haggis.h
|
hdrs += include/haggis.h
|
||||||
|
hdrs += include/jobq.h
|
||||||
|
|
||||||
srcs += src/bytes.c
|
srcs += src/bytes.c
|
||||||
srcs += src/haggis.c
|
srcs += src/haggis.c
|
||||||
|
srcs += src/jobq.c
|
||||||
|
|
||||||
objs = $(srcs:.c=.o)
|
objs = $(srcs:.c=.o)
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ CFLAGS += -Wall -Werror
|
||||||
CFLAGS += -Iinclude
|
CFLAGS += -Iinclude
|
||||||
CFLAGS += -fPIC
|
CFLAGS += -fPIC
|
||||||
|
|
||||||
LIBS += -lmd
|
LIBS += -lmd -lpthread
|
||||||
|
|
||||||
all: libhaggis.a libhaggis.so
|
all: libhaggis.a libhaggis.so
|
||||||
|
|
||||||
|
|
58
include/jobq.h
Normal file
58
include/jobq.h
Normal file
|
@ -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 <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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
|
72
src/jobq.c
Normal file
72
src/jobq.c
Normal file
|
@ -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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue