Merge branch 'odin' of git.hitchhiker-linux.org:jeang3nie/seahag into odin

This commit is contained in:
Nathan Fisher 2023-07-29 13:52:17 -04:00
commit e3531fdb08
3 changed files with 133 additions and 1 deletions

View file

@ -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
View 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
View 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;
}