Added message queue in mq.c
This commit is contained in:
parent
ec6efebd01
commit
13d7682861
4 changed files with 208 additions and 0 deletions
2
Makefile
2
Makefile
|
@ -42,12 +42,14 @@ CFLAGS += -fPIC
|
|||
hdrs += include/bytes.h
|
||||
hdrs += include/haggis.h
|
||||
hdrs += include/jobq.h
|
||||
hdrs += include/mq.h
|
||||
|
||||
mans += man/haggis_node.3
|
||||
|
||||
srcs += bytes.c
|
||||
srcs += haggis.c
|
||||
srcs += jobq.c
|
||||
srcs += mq.c
|
||||
srcs += linkmap.c
|
||||
|
||||
objs = $(srcs:.c=.o)
|
||||
|
|
54
haggis.c
54
haggis.c
|
@ -30,6 +30,7 @@
|
|||
* other than his own.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h> // PATH_MAX
|
||||
#include <stdint.h> // uint<x>_t
|
||||
|
@ -635,8 +636,61 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *
|
|||
return node;
|
||||
}
|
||||
|
||||
int haggis_extract_dev(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == block || node->filetype.tag == character);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_fifo(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == fifo);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_symlink(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == softlink);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_hardlink(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == hardlink);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_dir(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == directory);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_file(haggis_node *node, char *basedir) {
|
||||
// todo
|
||||
assert(node->filetype.tag == normal);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int haggis_extract_node(FILE *stream, char *basedir, haggis_node *node) {
|
||||
// todo
|
||||
switch (node->filetype.tag) {
|
||||
case block:
|
||||
case character:
|
||||
return haggis_extract_dev(node, basedir);
|
||||
case fifo:
|
||||
return haggis_extract_fifo(node, basedir);
|
||||
case softlink:
|
||||
return haggis_extract_symlink(node, basedir);
|
||||
case hardlink:
|
||||
return haggis_extract_hardlink(node, basedir);
|
||||
case directory:
|
||||
return haggis_extract_dir(node, basedir);
|
||||
case normal:
|
||||
return haggis_extract_file(node, basedir);
|
||||
case eof:
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
72
include/mq.h
Normal file
72
include/mq.h
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.
|
||||
*/
|
||||
|
||||
#ifndef MQ_H
|
||||
#define MQ_H 1
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "haggis.h"
|
||||
|
||||
typedef enum {
|
||||
NodeCreated,
|
||||
NodeExtracted,
|
||||
EndOfArchive,
|
||||
ArchiveError,
|
||||
} haggis_message_type;
|
||||
|
||||
typedef union {
|
||||
char *f_name;
|
||||
int errno;
|
||||
} haggis_message_body;
|
||||
|
||||
struct _mq_node {
|
||||
struct _mq_node *prev;
|
||||
struct _mq_node *next;
|
||||
haggis_message_type tag;
|
||||
haggis_message_body body;
|
||||
};
|
||||
|
||||
typedef struct _mq_node haggis_msg;
|
||||
|
||||
typedef struct {
|
||||
pthread_cond_t *cond;
|
||||
size_t count;
|
||||
pthread_mutex_t *mutex;
|
||||
haggis_msg *head;
|
||||
haggis_msg *tail;
|
||||
} haggis_mq;
|
||||
|
||||
int haggis_mq_push(haggis_mq *queue, haggis_msg *msg);
|
||||
haggis_msg* haggis_mq_pop(haggis_mq *queue);
|
||||
|
||||
#endif // !JOBQ_H
|
80
mq.c
Normal file
80
mq.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
/* _,.---._ .-._ .--.-. ,--.--------.
|
||||
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
||||
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
||||
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
||||
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
||||
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
||||
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
||||
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
||||
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
||||
* _ __ ,---. .-._ .=-.-. _,.----.
|
||||
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
||||
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
||||
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
||||
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
||||
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
||||
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
||||
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
||||
* `--`---' `--` `--`./ `--``--`-`
|
||||
*
|
||||
* @(#)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 <stdlib.h> // free, malloc
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
int haggis_mq_push(haggis_mq *queue, haggis_msg *msg) {
|
||||
msg->next = msg->prev = NULL;
|
||||
|
||||
pthread_mutex_lock(queue->mutex);
|
||||
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);
|
||||
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);
|
||||
queue->count--;
|
||||
msg = queue->head;
|
||||
if (msg->tag == EndOfArchive)
|
||||
return NULL;
|
||||
if (queue->tail == queue->head) {
|
||||
queue->tail = queue->head = NULL;
|
||||
} else {
|
||||
queue->head = queue->head->prev;
|
||||
}
|
||||
pthread_mutex_unlock(queue->mutex);
|
||||
msg->prev = msg->next = NULL;
|
||||
return msg;
|
||||
}
|
Loading…
Add table
Reference in a new issue