| 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"cxx . . $ `<file>/include/config.h`/ |
| 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | |
| 25 | #include <pthread.h> |
| 26 | |
| 27 | #include "queue.h"cxx . . $ `<file>/include/queue.h`/ |
| 28 | #include "log.h"cxx . . $ `<file>/include/log.h`/ |
| 29 | #include "util.h"cxx . . $ `<file>/include/util.h`/ |
| 30 | |
| 31 | typedef struct qnode_scxx . . $ qnode_s# qnode_tcxx . . $ qnode_t#; |
| 32 | |
| 33 | struct qnode_scxx . . $ qnode_s# { |
| 34 | qnode_tcxx . . $ qnode_t# *nextcxx . . $ qnode_s#next.; |
| 35 | qnode_tcxx . . $ qnode_t# *prevcxx . . $ qnode_s#prev.; |
| 36 | |
| 37 | void *datacxx . . $ qnode_s#data.; |
| 38 | }; |
| 39 | |
| 40 | struct queue_scxx . . $ queue_s# { |
| 41 | qnode_tcxx . . $ qnode_t# *headcxx . . $ queue_s#head.; |
| 42 | qnode_tcxx . . $ qnode_t# *tailcxx . . $ queue_s#tail.; |
| 43 | |
| 44 | pthread_mutex_tcxx . . $ pthread_mutex_t# mutexcxx . . $ queue_s#mutex.; |
| 45 | pthread_cond_tcxx . . $ pthread_cond_t# condcxx . . $ queue_s#cond.; |
| 46 | |
| 47 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! releasecxx . . $ queue_s#release.; |
| 48 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! wait_for_consumerscxx . . $ queue_s#wait_for_consumers.; |
| 49 | int active_consumerscxx . . $ queue_s#active_consumers.; |
| 50 | }; |
| 51 | |
| 52 | LOG_LEVELcxx . . $ LOG_LEVEL# queue_log_levelcxx . . $ queue_log_level. = LOG_LEVEL_TRACEcxx . . $ LOG_LEVEL_TRACE.; |
| 53 | |
| 54 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! queue_enqueuecxx . . $ queue_enqueue(93d9b74160b271bd).(queue_ts_tcxx . . $ queue_ts_t# * const queue, void * const data) { |
| 55 | qnode_tcxx . . $ qnode_t# *new; |
| 56 | new = checked_malloccxx . . $ checked_malloc(bfcfa4d6b7f7ef64).(sizeof(qnode_tcxx . . $ qnode_t#)); /* freed in queue_dequeue */ |
| 57 | new->datacxx . . $ qnode_s#data. = data; |
| 58 | |
| 59 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! new_head_tail = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 60 | |
| 61 | if (queue->headcxx . . $ queue_s#head. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 62 | /* new node is first element in empty queue */ |
| 63 | queue->headcxx . . $ queue_s#head. = new; |
| 64 | queue->tailcxx . . $ queue_s#tail. = new; |
| 65 | new->nextcxx . . $ qnode_s#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 66 | new->prevcxx . . $ qnode_s#prev. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 67 | new_head_tail = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 68 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(queue_log_levelcxx . . $ queue_log_level., "queue(%p): add node %p with payload %p as new head and new tail", (void*) queue, (void*) new, (void*) new->datacxx . . $ qnode_s#data.); |
| 69 | } else { |
| 70 | /* new node is new tail */ |
| 71 | (queue->tailcxx . . $ queue_s#tail.)->prevcxx . . $ qnode_s#prev. = new; |
| 72 | new->nextcxx . . $ qnode_s#next. = queue->tailcxx . . $ queue_s#tail.; |
| 73 | new->prevcxx . . $ qnode_s#prev. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 74 | queue->tailcxx . . $ queue_s#tail. = new; |
| 75 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(queue_log_levelcxx . . $ queue_log_level., "queue(%p): add node %p with payload %p as new tail", (void*) queue, (void*) new, (void*) new->datacxx . . $ qnode_s#data.); |
| 76 | } |
| 77 | return new_head_tail; |
| 78 | } |
| 79 | |
| 80 | queue_ts_tcxx . . $ queue_ts_t# *queue_ts_initcxx . . $ queue_ts_init(26178f4c59431c2a).(void) { |
| 81 | queue_ts_tcxx . . $ queue_ts_t# *queue = checked_malloccxx . . $ checked_malloc(bfcfa4d6b7f7ef64). (sizeof(queue_ts_tcxx . . $ queue_ts_t#)); |
| 82 | |
| 83 | pthread_mutexattr_tcxx . . $ pthread_mutexattr_t# attr; |
| 84 | pthread_mutexattr_initcxx . . $ pthread_mutexattr_init(7099fa328d29ec1). (&attr); |
| 85 | pthread_mutexattr_settypecxx . . $ pthread_mutexattr_settype(490643a50482da1c). (&attr, PTHREAD_MUTEX_RECURSIVEcxx . . $ PTHREAD_MUTEX_RECURSIVE.); |
| 86 | pthread_mutex_initcxx . . $ pthread_mutex_init(db9d29dab44ea1aa).(&queue->mutexcxx . . $ queue_s#mutex., &attr); |
| 87 | pthread_cond_initcxx . . $ pthread_cond_init(fc876bc1ec1249fc).(&queue->condcxx . . $ queue_s#cond., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 88 | |
| 89 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 90 | |
| 91 | queue->releasecxx . . $ queue_s#release. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 92 | queue->wait_for_consumerscxx . . $ queue_s#wait_for_consumers. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 93 | queue->active_consumerscxx . . $ queue_s#active_consumers. = 0; |
| 94 | |
| 95 | queue->headcxx . . $ queue_s#head. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 96 | queue->tailcxx . . $ queue_s#tail. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 97 | |
| 98 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 99 | |
| 100 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(queue_log_levelcxx . . $ queue_log_level., "queue(%p): create new queue", (void*) queue); |
| 101 | return queue; |
| 102 | } |
| 103 | |
| 104 | void queue_ts_freecxx . . $ queue_ts_free(b1ab4594e88bd087).(queue_ts_tcxx . . $ queue_ts_t# *queue) { |
| 105 | if (queue) { |
| 106 | pthread_cond_destroycxx . . $ pthread_cond_destroy(98095c34a2b0149d).(&queue->condcxx . . $ queue_s#cond.); |
| 107 | pthread_mutex_destroycxx . . $ pthread_mutex_destroy(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 108 | freecxx . . $ free(5d22bdacc48458e8).(queue); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! queue_ts_enqueuecxx . . $ queue_ts_enqueue(b12881b4bc859e3c).(queue_ts_tcxx . . $ queue_ts_t# * const queue, void * const data, const char *whoami) { |
| 113 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 114 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! new_head_tail = queue_enqueuecxx . . $ queue_enqueue(93d9b74160b271bd).(queue,data); |
| 115 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 116 | |
| 117 | if (new_head_tail) { |
| 118 | pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&queue->condcxx . . $ queue_s#cond.); |
| 119 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ 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_waitcxx . . $ queue_ts_dequeue_wait(45dab9fcfc1afe1).(queue_ts_tcxx . . $ queue_ts_t# * const queue, const char *whoami) { |
| 125 | qnode_tcxx . . $ qnode_t# *head; |
| 126 | void *data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 127 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 128 | |
| 129 | if (queue->wait_for_consumerscxx . . $ queue_s#wait_for_consumers.) { |
| 130 | queue->active_consumerscxx . . $ queue_s#active_consumers.--; |
| 131 | if (queue->active_consumerscxx . . $ queue_s#active_consumers. == 0 && queue->releasecxx . . $ queue_s#release.) { |
| 132 | pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&queue->condcxx . . $ queue_s#cond.); |
| 133 | } |
| 134 | } |
| 135 | while ((head = queue->headcxx . . $ queue_s#head.) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && (queue->releasecxx . . $ queue_s#release. == falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`! || queue->active_consumerscxx . . $ queue_s#active_consumers. > 0)) { |
| 136 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: queue(%p): waiting for new node", whoami, (void*) queue); |
| 137 | pthread_cond_waitcxx . . $ pthread_cond_wait(6145be15a5d9db5d).(&queue->condcxx . . $ queue_s#cond., &queue->mutexcxx . . $ queue_s#mutex.); |
| 138 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: queue(%p): got broadcast (head: %p)", whoami, (void*) queue, (void*) queue->headcxx . . $ queue_s#head.); |
| 139 | } |
| 140 | if (head != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 141 | if (queue->wait_for_consumerscxx . . $ queue_s#wait_for_consumers.) { queue->active_consumerscxx . . $ queue_s#active_consumers.++; } |
| 142 | if ((queue->headcxx . . $ queue_s#head. = head->prevcxx . . $ qnode_s#prev.) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 143 | queue->tailcxx . . $ queue_s#tail. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 144 | } else { |
| 145 | (queue->headcxx . . $ queue_s#head.)->nextcxx . . $ qnode_s#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 146 | } |
| 147 | data = head->datacxx . . $ qnode_s#data.; |
| 148 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(queue_log_levelcxx . . $ queue_log_level., "queue(%p): return head node %p with payload %p", (void*) queue, (void*) head, (void*) head->datacxx . . $ qnode_s#data.); |
| 149 | freecxx . . $ free(5d22bdacc48458e8).(head); |
| 150 | } else { |
| 151 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(queue_log_levelcxx . . $ queue_log_level., "queue(%p): return NULL from empty, released queue", (void*) queue); |
| 152 | } |
| 153 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 154 | return data; |
| 155 | } |
| 156 | |
| 157 | void queue_ts_registercxx . . $ queue_ts_register(cf5a74e7b45a2e30).(queue_ts_tcxx . . $ queue_ts_t# * const queue, const char *whoami) { |
| 158 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 159 | queue->wait_for_consumerscxx . . $ queue_s#wait_for_consumers. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 160 | queue->active_consumerscxx . . $ queue_s#active_consumers.++; |
| 161 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 162 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: queue(%p): register thread", whoami, (void*) queue); |
| 163 | } |
| 164 | |
| 165 | void queue_ts_releasecxx . . $ queue_ts_release(cf5a74e7b45a2e30).(queue_ts_tcxx . . $ queue_ts_t# * const queue, const char *whoami) { |
| 166 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 167 | queue->releasecxx . . $ queue_s#release. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 168 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&queue->mutexcxx . . $ queue_s#mutex.); |
| 169 | pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&queue->condcxx . . $ queue_s#cond.); |
| 170 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: queue(%p): release queue and broadcast waiting threads", whoami, (void*) queue); |
| 171 | } |