aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-06-12 21:18:28 +0530
committersinanmohd <sinan@sinanmohd.com>2024-06-12 21:22:53 +0530
commitadd3c9f6d8464c29d729606fbf0e3311ec759e20 (patch)
tree96347c3e36f085651c6009d7d7457c1a9e3a40cc
parent4c7093f68e8c576e1e10dbb31597c810691fd6e2 (diff)
src/queue: init
-rw-r--r--.clang-format1
-rw-r--r--include/jobs.h5
-rw-r--r--include/queue.h19
-rw-r--r--src/evanix.c42
-rw-r--r--src/meson.build1
-rw-r--r--src/queue.c78
6 files changed, 144 insertions, 2 deletions
diff --git a/.clang-format b/.clang-format
index 5046235..2c3804a 100644
--- a/.clang-format
+++ b/.clang-format
@@ -12,3 +12,4 @@ UseTab: Always
ForEachMacros:
- 'LIST_FOREACH'
+ - 'CIRCLEQ_FOREACH'
diff --git a/include/jobs.h b/include/jobs.h
index 41bfa57..be95c57 100644
--- a/include/jobs.h
+++ b/include/jobs.h
@@ -8,15 +8,18 @@ struct output {
};
LIST_HEAD(job_dlist, job);
+CIRCLEQ_HEAD(job_clist, job);
struct job {
char *name, *drv_path;
struct output_dlist outputs;
struct job_dlist deps;
+ /* TODO: replace dlist with clist jobs.c */
LIST_ENTRY(job) dlist;
+ CIRCLEQ_ENTRY(job) clist;
};
int jobs_init(FILE **stream);
int job_new(struct job **j, char *name, char *drv_path);
void job_free(struct job *j);
-int jobs_read(FILE *stream, struct job *jobs);
+int job_read(FILE *stream, struct job **jobs);
diff --git a/include/queue.h b/include/queue.h
new file mode 100644
index 0000000..ca11614
--- /dev/null
+++ b/include/queue.h
@@ -0,0 +1,19 @@
+#include <pthread.h>
+#include <sys/queue.h>
+
+#include "jobs.h"
+
+struct queue {
+ struct job_clist jobs;
+ pthread_mutex_t mutex;
+};
+
+struct queue_thread {
+ pthread_t tid;
+ struct queue *queue;
+ FILE *stream;
+};
+
+int queue_thread_new(struct queue_thread **queue_thread, FILE *stream);
+void queue_thread_free(struct queue_thread *queue_thread);
+void *queue_thread_entry(void *queue_thread);
diff --git a/src/evanix.c b/src/evanix.c
index b552c8e..dcac070 100644
--- a/src/evanix.c
+++ b/src/evanix.c
@@ -1 +1,41 @@
-int main(void) {}
+#include <stdlib.h>
+#include <string.h>
+
+#include "queue.h"
+#include "util.h"
+
+int main(void)
+{
+ struct queue_thread *queue_thread = NULL;
+ FILE *stream = NULL;
+ int ret = 0;
+
+ ret = jobs_init(&stream);
+ if (ret < 0)
+ goto out_free;
+
+ ret = queue_thread_new(&queue_thread, stream);
+ if (ret < 0) {
+ free(stream);
+ goto out_free;
+ }
+
+ ret = pthread_create(&queue_thread->tid, NULL, queue_thread_entry,
+ queue_thread);
+ if (ret < 0) {
+ print_err("%s", strerror(ret));
+ ret = -ret;
+ goto out_free;
+ }
+
+ ret = pthread_join(queue_thread->tid, NULL);
+ if (ret < 0) {
+ print_err("%s", strerror(ret));
+ ret = -ret;
+ goto out_free;
+ }
+
+out_free:
+ queue_thread_free(queue_thread);
+ exit(ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
+}
diff --git a/src/meson.build b/src/meson.build
index 5fe292f..bb68e18 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -4,6 +4,7 @@ e = executable(
'evanix.c',
'jobs.c',
'util.c',
+ 'queue.c',
],
include_directories: evanix_inc,
diff --git a/src/queue.c b/src/queue.c
new file mode 100644
index 0000000..5c196aa
--- /dev/null
+++ b/src/queue.c
@@ -0,0 +1,78 @@
+#include <errno.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/queue.h>
+
+#include "queue.h"
+#include "util.h"
+
+void *queue_thread_entry(void *queue_thread)
+{
+ struct queue_thread *qt = queue_thread;
+ struct job *job = NULL;
+ int ret = 0;
+
+ while (true) {
+ ret = job_read(qt->stream, &job);
+ if (ret < 0) {
+ if (ret == -EOF)
+ ret = 0;
+ break;
+ }
+
+ pthread_mutex_lock(&qt->queue->mutex);
+ CIRCLEQ_INSERT_TAIL(&qt->queue->jobs, job, clist);
+ pthread_mutex_unlock(&qt->queue->mutex);
+ }
+
+ pthread_exit(NULL);
+}
+
+void queue_thread_free(struct queue_thread *queue_thread)
+{
+ struct job *job;
+
+ if (queue_thread == NULL)
+ return;
+
+ CIRCLEQ_FOREACH (job, &queue_thread->queue->jobs, clist) {
+ CIRCLEQ_REMOVE(&queue_thread->queue->jobs, job, clist);
+ free(job);
+ }
+
+ free(queue_thread->queue);
+ fclose(queue_thread->stream);
+}
+
+int queue_thread_new(struct queue_thread **queue_thread, FILE *stream)
+{
+ int ret = 0;
+ struct queue_thread *qt = NULL;
+
+ qt = malloc(sizeof(*qt));
+ if (qt == NULL) {
+ print_err("%s", strerror(errno));
+ return -errno;
+ }
+
+ qt->stream = stream;
+
+ qt->queue = malloc(sizeof(*qt->queue));
+ if (qt->queue == NULL) {
+ print_err("%s", strerror(errno));
+ ret = -errno;
+ goto out_free;
+ }
+ CIRCLEQ_INIT(&qt->queue->jobs);
+ pthread_mutex_init(&qt->queue->mutex, NULL);
+
+out_free:
+ if (ret < 0)
+ free(qt);
+ else
+ *queue_thread = qt;
+
+ return ret;
+}