summaryrefslogtreecommitdiffstats
path: root/src/contrib/semaphore.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 19:05:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 19:05:44 +0000
commitb045529c40c83601909dca7b76a53498e9a70f33 (patch)
tree88371572105933fd950676c07b3a12163a0c9de0 /src/contrib/semaphore.c
parentInitial commit. (diff)
downloadknot-b045529c40c83601909dca7b76a53498e9a70f33.tar.xz
knot-b045529c40c83601909dca7b76a53498e9a70f33.zip
Adding upstream version 3.3.4.upstream/3.3.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/contrib/semaphore.c')
-rw-r--r--src/contrib/semaphore.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/contrib/semaphore.c b/src/contrib/semaphore.c
new file mode 100644
index 0000000..1953fc3
--- /dev/null
+++ b/src/contrib/semaphore.c
@@ -0,0 +1,134 @@
+/* Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "semaphore.h"
+
+#include <assert.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#if defined(__APPLE__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
+#define SEM_STATUS_POSIX INT_MIN
+
+void knot_sem_init(knot_sem_t *sem, int value)
+{
+ assert((sem != NULL) && (value != SEM_STATUS_POSIX));
+ if (value < 0) {
+ goto nonposix;
+ }
+ int ret = sem_init(&sem->semaphore, 1, value);
+ if (ret == 0) {
+ sem->status = SEM_STATUS_POSIX;
+ return;
+ }
+nonposix:
+ knot_sem_init_nonposix(sem, value);
+}
+
+void knot_sem_init_nonposix(knot_sem_t *sem, int value)
+{
+ assert((sem != NULL) && (value != SEM_STATUS_POSIX));
+ sem->status = value;
+ sem->status_lock = malloc(sizeof(*sem->status_lock));
+ pthread_mutex_init(&sem->status_lock->mutex, NULL);
+ pthread_cond_init(&sem->status_lock->cond, NULL);
+}
+
+void knot_sem_reset(knot_sem_t *sem, int value)
+{
+ assert((sem != NULL) && (value != SEM_STATUS_POSIX) && (sem->status != SEM_STATUS_POSIX));
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ sem->status = value;
+ pthread_cond_signal(&sem->status_lock->cond);
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+}
+
+void knot_sem_wait(knot_sem_t *sem)
+{
+ assert(sem != NULL);
+ if (sem->status == SEM_STATUS_POSIX) {
+ int semret;
+ do {
+ semret = sem_wait(&sem->semaphore);
+ } while (semret != 0); // repeat wait as it might be interrupted by a signal
+ } else {
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ while (sem->status <= 0) {
+ pthread_cond_wait(&sem->status_lock->cond, &sem->status_lock->mutex);
+ }
+ sem->status--;
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+ }
+}
+
+void knot_sem_wait_post(knot_sem_t *sem)
+{
+ assert((sem != NULL) && (sem->status != SEM_STATUS_POSIX));
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ while (sem->status <= 0) {
+ pthread_cond_wait(&sem->status_lock->cond, &sem->status_lock->mutex);
+ }
+ pthread_cond_signal(&sem->status_lock->cond);
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+}
+
+void knot_sem_get_ahead(knot_sem_t *sem)
+{
+ assert((sem != NULL) && (sem->status != SEM_STATUS_POSIX));
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ sem->status--;
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+}
+
+void knot_sem_get_assert(knot_sem_t *sem)
+{
+ assert((sem != NULL) && (sem->status != SEM_STATUS_POSIX));
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ assert(sem->status > 0);
+ sem->status--;
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+}
+
+void knot_sem_post(knot_sem_t *sem)
+{
+ assert(sem != NULL);
+ if (sem->status == SEM_STATUS_POSIX) {
+ int semret = sem_post(&sem->semaphore);
+ (void)semret;
+ assert(semret == 0);
+ } else {
+ pthread_mutex_lock(&sem->status_lock->mutex);
+ sem->status++;
+ pthread_cond_signal(&sem->status_lock->cond);
+ pthread_mutex_unlock(&sem->status_lock->mutex);
+ }
+}
+
+void knot_sem_destroy(knot_sem_t *sem)
+{
+ assert(sem != NULL);
+ knot_sem_wait(sem); // NOTE this is questionable if the initial value was > 1
+ if (sem->status == SEM_STATUS_POSIX) {
+ sem_destroy(&sem->semaphore);
+ } else {
+ pthread_cond_destroy(&sem->status_lock->cond);
+ pthread_mutex_destroy(&sem->status_lock->mutex);
+ free(sem->status_lock);
+ }
+}