seahag/mq.c

106 lines
3.5 KiB
C
Raw Normal View History

2023-09-17 19:09:44 -04:00
/* _,.---._ .-._ .--.-. ,--.--------.
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)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 <pthread.h>
#include <stdio.h>
2023-09-17 19:09:44 -04:00
#include <stdlib.h> // free, malloc
#include "haggis.h"
2023-09-17 19:09:44 -04:00
#include "mq.h"
haggis_msg* haggis_msg_init(haggis_message_type tag, haggis_message_body body) {
haggis_msg *msg = calloc(1, sizeof(haggis_msg));
if (msg == NULL)
return NULL;
msg->next = msg->prev = NULL;
msg->tag = tag;
msg->body = body;
return msg;
}
void haggis_msg_deinit(haggis_msg *msg) {
switch (msg->tag) {
case NodeCreated:
case NodeExtracted:
free(msg->body.f_name);
break;
case EndOfArchive:
case ArchiveError:
break;
}
free(msg);
}
int haggis_mq_init(haggis_mq *mq) {
int ret;
2023-09-17 19:09:44 -04:00
mq->head = NULL;
mq->tail = NULL;
ret = pthread_mutex_init(&mq->mutex, NULL);
if (ret != 0)
return ret;
return pthread_cond_init(&mq->cond, NULL);
}
int haggis_mq_push(haggis_mq *queue, haggis_msg *msg) {
pthread_mutex_lock(&queue->mutex);
2023-09-17 19:09:44 -04:00
if (queue->tail == NULL) {
queue->tail = queue->head = msg;
} else {
msg->next = queue->tail;
queue->tail->prev = msg;
queue->tail = msg;
}
queue->count++;
pthread_mutex_unlock(&queue->mutex);
2023-09-17 19:09:44 -04:00
return 0;
}
haggis_msg* haggis_mq_pop(haggis_mq *queue) {
haggis_msg *msg;
while (queue->count == 0)
pthread_cond_wait(&queue->cond, &queue->mutex);
pthread_mutex_lock(&queue->mutex);
2023-09-17 19:09:44 -04:00
queue->count--;
msg = queue->head;
if (msg->tag == EndOfArchive)
return msg;
2023-09-17 19:09:44 -04:00
if (queue->tail == queue->head) {
queue->tail = queue->head = NULL;
} else {
queue->head = queue->head->prev;
}
pthread_mutex_unlock(&queue->mutex);
2023-09-17 19:09:44 -04:00
msg->prev = msg->next = NULL;
return msg;
}