Debian Codegraph @

src/queue.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2022-2025 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "config.h"
22 #include <stdlib.h>
23 #include <stdbool.h>
24
25 #include <pthread.h>
26
27 #include "queue.h"
28 #include "log.h"
29 #include "util.h"
30
31 typedef struct qnode_s qnode_t;
32
33 struct qnode_s {
34 qnode_t *next;
35 qnode_t *prev;
36
37 void *data;
38 };
39
40 struct queue_s {
41 qnode_t *head;
42 qnode_t *tail;
43
44 pthread_mutex_t mutex;
45 pthread_cond_t cond;
46
47 bool release;
48 bool wait_for_consumers;
49 int active_consumers;
50 };
51
52 LOG_LEVEL queue_log_level = LOG_LEVEL_TRACE;
53
54 static bool queue_enqueue(queue_ts_t * const queue, void * const data) {
55 qnode_t *new;
56 new = checked_malloc(sizeof(qnode_t)); /* freed in queue_dequeue */
57 new->data = data;
58
59 bool new_head_tail = false;
60
61 if (queue->head == NULL) {
62 /* new node is first element in empty queue */
63 queue->head = new;
64 queue->tail = new;
65 new->next = NULL;
66 new->prev = NULL;
67 new_head_tail = true;
68 log_msg(queue_log_level, "queue(%p): add node %p with payload %p as new head and new tail", (void*) queue, (void*) new, (void*) new->data);
69 } else {
70 /* new node is new tail */
71 (queue->tail)->prev = new;
72 new->next = queue->tail;
73 new->prev = NULL;
74 queue->tail = new;
75 log_msg(queue_log_level, "queue(%p): add node %p with payload %p as new tail", (void*) queue, (void*) new, (void*) new->data);
76 }
77 return new_head_tail;
78 }
79
80 queue_ts_t *queue_ts_init(void) {
81 queue_ts_t *queue = checked_malloc (sizeof(queue_ts_t));
82
83 pthread_mutexattr_t attr;
84 pthread_mutexattr_init (&attr);
85 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
86 pthread_mutex_init(&queue->mutex, &attr);
87 pthread_cond_init(&queue->cond, NULL);
88
89 pthread_mutex_lock(&queue->mutex);
90
91 queue->release = false;
92 queue->wait_for_consumers = false;
93 queue->active_consumers = 0;
94
95 queue->head = NULL;
96 queue->tail = NULL;
97
98 pthread_mutex_unlock(&queue->mutex);
99
100 log_msg(queue_log_level, "queue(%p): create new queue", (void*) queue);
101 return queue;
102 }
103
104 void queue_ts_free(queue_ts_t *queue) {
105 if (queue) {
106 pthread_cond_destroy(&queue->cond);
107 pthread_mutex_destroy(&queue->mutex);
108 free(queue);
109 }
110 }
111
112 bool queue_ts_enqueue(queue_ts_t * const queue, void * const data, const char *whoami) {
113 pthread_mutex_lock(&queue->mutex);
114 bool new_head_tail = queue_enqueue(queue,data);
115 pthread_mutex_unlock(&queue->mutex);
116
117 if (new_head_tail) {
118 pthread_cond_broadcast(&queue->cond);
119 log_msg(LOG_LEVEL_THREAD, "%10s: queue(%p): broadcast waiting threads for new head node in queue", whoami, (void*) queue);
120 }
121 return new_head_tail;
122 }
123
124 void *queue_ts_dequeue_wait(queue_ts_t * const queue, const char *whoami) {
125 qnode_t *head;
126 void *data = NULL;
127 pthread_mutex_lock(&queue->mutex);
128
129 if (queue->wait_for_consumers) {
130 queue->active_consumers--;
131 if (queue->active_consumers == 0 && queue->release) {
132 pthread_cond_broadcast(&queue->cond);
133 }
134 }
135 while ((head = queue->head) == NULL && (queue->release == false || queue->active_consumers > 0)) {
136 log_msg(LOG_LEVEL_THREAD, "%10s: queue(%p): waiting for new node", whoami, (void*) queue);
137 pthread_cond_wait(&queue->cond, &queue->mutex);
138 log_msg(LOG_LEVEL_THREAD, "%10s: queue(%p): got broadcast (head: %p)", whoami, (void*) queue, (void*) queue->head);
139 }
140 if (head != NULL) {
141 if (queue->wait_for_consumers) { queue->active_consumers++; }
142 if ((queue->head = head->prev) == NULL) {
143 queue->tail = NULL;
144 } else {
145 (queue->head)->next = NULL;
146 }
147 data = head->data;
148 log_msg(queue_log_level, "queue(%p): return head node %p with payload %p", (void*) queue, (void*) head, (void*) head->data);
149 free(head);
150 } else {
151 log_msg(queue_log_level, "queue(%p): return NULL from empty, released queue", (void*) queue);
152 }
153 pthread_mutex_unlock(&queue->mutex);
154 return data;
155 }
156
157 void queue_ts_register(queue_ts_t * const queue, const char *whoami) {
158 pthread_mutex_lock(&queue->mutex);
159 queue->wait_for_consumers = true;
160 queue->active_consumers++;
161 pthread_mutex_unlock(&queue->mutex);
162 log_msg(LOG_LEVEL_THREAD, "%10s: queue(%p): register thread", whoami, (void*) queue);
163 }
164
165 void queue_ts_release(queue_ts_t * const queue, const char *whoami) {
166 pthread_mutex_lock(&queue->mutex);
167 queue->release = true;
168 pthread_mutex_unlock(&queue->mutex);
169 pthread_cond_broadcast(&queue->cond);
170 log_msg(LOG_LEVEL_THREAD, "%10s: queue(%p): release queue and broadcast waiting threads", whoami, (void*) queue);
171 }