Adjust job queue to take a union as payload

This commit is contained in:
Nathan Fisher 2023-07-29 18:18:11 -04:00
parent c72c82484a
commit 2db9b2df53
3 changed files with 26 additions and 22 deletions

View file

@ -38,23 +38,20 @@
typedef uint8_t u8; typedef uint8_t u8;
union _u16 { typedef union {
uint16_t val; uint16_t val;
u8 bytes[2]; u8 bytes[2];
}; } u16;
typedef union _u16 u16;
union _u32 { typedef union {
uint32_t val; uint32_t val;
u8 bytes[4]; u8 bytes[4];
}; } u32;
typedef union _u32 u32;
union _u64 { typedef union {
uint64_t val; uint64_t val;
u8 bytes[8]; u8 bytes[8];
}; } u64;
typedef union _u64 u64;
typedef struct { typedef struct {
u32 major; u32 major;

View file

@ -37,13 +37,20 @@
#include <semaphore.h> #include <semaphore.h>
#include <stdlib.h> #include <stdlib.h>
struct _job { #include "haggis.h"
struct _job *prev;
char *fname; typedef union {
struct _job *next; haggis_node *node;
char *f_name;
} haggis_job;
struct _job_node {
struct _job_node *prev;
haggis_job *job;
struct _job_node *next;
}; };
typedef struct _job haggis_jobq_node; typedef struct _job_node haggis_jobq_node;
typedef struct { typedef struct {
sem_t *sem; sem_t *sem;
@ -52,7 +59,7 @@ typedef struct {
haggis_jobq_node *tail; haggis_jobq_node *tail;
} haggis_jobq; } haggis_jobq;
int haggis_jobq_push(haggis_jobq *queue, char *fname); int haggis_jobq_push(haggis_jobq *queue, haggis_job *job);
char* haggis_jobq_pop(haggis_jobq *queue); haggis_job* haggis_jobq_pop(haggis_jobq *queue);
#endif // !JOBQ_H #endif // !JOBQ_H

View file

@ -32,11 +32,11 @@
#include "jobq.h" #include "jobq.h"
int haggis_jobq_push(haggis_jobq *queue, char *fname) { int haggis_jobq_push(haggis_jobq *queue, haggis_job *job) {
haggis_jobq_node *new = malloc(sizeof(haggis_jobq_node)); haggis_jobq_node *new = malloc(sizeof(haggis_jobq_node));
if (new == NULL) if (new == NULL)
return 1; return 1;
new->fname = fname; new->job = job;
new->next = new->prev = NULL; new->next = new->prev = NULL;
pthread_mutex_lock(queue->mutex); pthread_mutex_lock(queue->mutex);
@ -51,14 +51,14 @@ int haggis_jobq_push(haggis_jobq *queue, char *fname) {
return sem_post(queue->sem); return sem_post(queue->sem);
} }
char* haggis_jobq_pop(haggis_jobq *queue) { haggis_job* haggis_jobq_pop(haggis_jobq *queue) {
haggis_jobq_node *j; haggis_jobq_node *j;
char *fname; haggis_job *job;
sem_wait(queue->sem); sem_wait(queue->sem);
pthread_mutex_lock(queue->mutex); pthread_mutex_lock(queue->mutex);
j = queue->head; j = queue->head;
fname = j->fname; job = j->job;
if (queue->tail == queue->head) { if (queue->tail == queue->head) {
queue->tail = queue->head = NULL; queue->tail = queue->head = NULL;
} else { } else {
@ -68,5 +68,5 @@ char* haggis_jobq_pop(haggis_jobq *queue) {
pthread_mutex_unlock(queue->mutex); pthread_mutex_unlock(queue->mutex);
if (sem_post(queue->sem) != 0) if (sem_post(queue->sem) != 0)
return NULL; return NULL;
return fname; return job;
} }