]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
semaphore: custom implementation to somehow work on OpenBSD and OSX
authorLibor Peltan <libor.peltan@nic.cz>
Tue, 2 Apr 2019 15:46:37 +0000 (17:46 +0200)
committerDaniel Salzman <daniel.salzman@nic.cz>
Fri, 26 Apr 2019 13:18:00 +0000 (15:18 +0200)
Knot.files
src/contrib/Makefile.inc
src/contrib/semaphore.c [new file with mode: 0644]
src/contrib/semaphore.h [new file with mode: 0644]
src/knot/updates/apply.c
src/knot/updates/apply.h
src/knot/updates/zone-update.c
src/knot/zone/zone.c
src/knot/zone/zone.h
tests/knot/test_zone-update.c

index ef099e81ee6ed8cdc9dee9c0b1e4dbfb98438b21..bb93e4d13d9abd1129ec73c4d1a68bccb3a298e4 100644 (file)
@@ -37,6 +37,8 @@ src/contrib/openbsd/strlcpy.c
 src/contrib/openbsd/strlcpy.h
 src/contrib/qp-trie/trie.c
 src/contrib/qp-trie/trie.h
+src/contrib/semaphore.c
+src/contrib/semaphore.h
 src/contrib/sockaddr.c
 src/contrib/sockaddr.h
 src/contrib/string.c
index cef7e0b8ccd9fd979bbf70b80b2c7bebfc41335f..f018760897667a8d20b8fc7260ffe9c32076b39f 100644 (file)
@@ -36,6 +36,8 @@ libcontrib_la_SOURCES = \
        contrib/net.h                           \
        contrib/qp-trie/trie.c                  \
        contrib/qp-trie/trie.h                  \
+       contrib/semaphore.c                     \
+       contrib/semaphore.h                     \
        contrib/sockaddr.c                      \
        contrib/sockaddr.h                      \
        contrib/string.c                        \
diff --git a/src/contrib/semaphore.c b/src/contrib/semaphore.c
new file mode 100644 (file)
index 0000000..724443d
--- /dev/null
@@ -0,0 +1,70 @@
+/*  Copyright (C) 2019 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 <stdlib.h>
+
+void knot_sem_init(knot_sem_t *sem, unsigned int value)
+{
+       int ret = sem_init(&sem->semaphore, 1, value);
+       if (ret == 0) {
+               sem->status = -1;
+       } else {
+               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_wait(knot_sem_t *sem)
+{
+       if (sem->status < 0) {
+               sem_wait(&sem->semaphore);
+       } 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_post(knot_sem_t *sem)
+{
+       if (sem->status < 0) {
+               sem_post(&sem->semaphore);
+       } 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)
+{
+       knot_sem_wait(sem);
+       if (sem->status < 0) {
+               sem_destroy(&sem->semaphore);
+       } else {
+               pthread_cond_destroy(&sem->status_lock->cond);
+               pthread_mutex_destroy(&sem->status_lock->mutex);
+               free(sem->status_lock);
+       }
+}
diff --git a/src/contrib/semaphore.h b/src/contrib/semaphore.h
new file mode 100644 (file)
index 0000000..be49b7f
--- /dev/null
@@ -0,0 +1,41 @@
+/*  Copyright (C) 2019 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 <pthread.h>
+#include <semaphore.h>
+
+#pragma once
+
+typedef struct {
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+} knot_sem_mutex_t;
+
+typedef struct {
+       int status;
+       union {
+               sem_t semaphore;
+               knot_sem_mutex_t *status_lock;
+       };
+} knot_sem_t;
+
+void knot_sem_init(knot_sem_t *sem, unsigned int value);
+
+void knot_sem_wait(knot_sem_t *sem);
+
+void knot_sem_post(knot_sem_t *sem);
+
+void knot_sem_destroy(knot_sem_t *sem);
index 11910d08f785dbf4b26896d49c198784aed17957..dc119909dd3163f0e1eba9facf1676cbbb4314da 100644 (file)
@@ -420,7 +420,7 @@ void update_cleanup(apply_ctx_t *ctx)
        zone_trees_unify_binodes(ctx->contents->nodes, ctx->contents->nsec3_nodes);
 
        if (ctx->cow_mutex != NULL) {
-               sem_post(ctx->cow_mutex);
+               knot_sem_post(ctx->cow_mutex);
        }
 }
 
@@ -449,7 +449,7 @@ void update_rollback(apply_ctx_t *ctx)
        }
 
        if (ctx->cow_mutex != NULL) {
-               sem_post(ctx->cow_mutex);
+               knot_sem_post(ctx->cow_mutex);
        }
 
        free(ctx->contents->nodes);
index 113db49800d5b318381b2bceebd38b96322017c2..d307e6681d1e458f045ef3b5f957e216775f5f1c 100644 (file)
@@ -16,8 +16,7 @@
 
 #pragma once
 
-#include <semaphore.h>
-
+#include "contrib/semaphore.h"
 #include "knot/zone/contents.h"
 #include "knot/updates/changesets.h"
 #include "contrib/ucw/lists.h"
@@ -31,7 +30,7 @@ struct apply_ctx {
        zone_tree_t *node_ptrs;   /*!< Just pointers to the affected nodes in contents. */
        zone_tree_t *nsec3_ptrs;  /*!< The same for NSEC3 nodes. */
        uint32_t flags;
-       sem_t *cow_mutex; // pointer to zone_t struct
+       knot_sem_t *cow_mutex;
 };
 
 typedef struct apply_ctx apply_ctx_t;
index b9ae86f893d1ea186e4550d208f675e8fc6785be..aa98907b048e7932d5eb4cc315b7327616c671ad 100644 (file)
@@ -127,7 +127,7 @@ int init_base(zone_update_t *update, zone_t *zone, zone_contents_t *old_contents
                return KNOT_ENOMEM;
        }
 
-       sem_wait(&zone->cow_lock);
+       knot_sem_wait(&zone->cow_lock);
        update->a_ctx->cow_mutex = &zone->cow_lock;
 
        int ret = KNOT_EINVAL;
@@ -137,7 +137,7 @@ int init_base(zone_update_t *update, zone_t *zone, zone_contents_t *old_contents
                ret = init_full(update, zone);
        }
        if (ret != KNOT_EOK) {
-               sem_post(&zone->cow_lock);
+               knot_sem_post(&zone->cow_lock);
                free(update->a_ctx);
        }
 
@@ -221,7 +221,7 @@ int zone_update_from_contents(zone_update_t *update, zone_t *zone_without_conten
                return KNOT_ENOMEM;
        }
 
-       sem_wait(&update->zone->cow_lock);
+       knot_sem_wait(&update->zone->cow_lock);
        update->a_ctx->cow_mutex = &update->zone->cow_lock;
 
        if (flags & UPDATE_INCREMENTAL) {
@@ -343,7 +343,7 @@ void zone_update_clear(zone_update_t *update)
                zone_contents_deep_free(update->new_cont);
        }
        if (update->a_ctx != NULL && update->a_ctx->cow_mutex != NULL) {
-               sem_post(update->a_ctx->cow_mutex);
+               knot_sem_post(update->a_ctx->cow_mutex);
        }
        free(update->a_ctx);
        mp_delete(update->mm.ctx);
index de97d4db2bd6025549f44c146ffbcaa59171c540..1e8e40449813ed51f4aa70ef556134fad0b8705b 100644 (file)
@@ -175,7 +175,7 @@ zone_t* zone_new(const knot_dname_t *name)
        zone->ddns_queue_size = 0;
        init_list(&zone->ddns_queue);
 
-       sem_init(&zone->cow_lock, 1, 1);
+       knot_sem_init(&zone->cow_lock, 1);
 
        // Preferred master lock
        pthread_mutex_init(&zone->preferred_lock, NULL);
@@ -215,8 +215,7 @@ void zone_free(zone_t **zone_ptr)
        free_ddns_queue(zone);
        pthread_mutex_destroy(&zone->ddns_lock);
 
-       sem_wait(&zone->cow_lock);
-       sem_destroy(&zone->cow_lock);
+       knot_sem_destroy(&zone->cow_lock);
 
        /* Control update. */
        zone_control_clear(zone);
index e4841c2c5ca3092fd8ca3e0dd9bd5d6e4510434c..440fe2f1347e8ca16ba5920360b4c10358674eab 100644 (file)
@@ -16,8 +16,7 @@
 
 #pragma once
 
-#include <semaphore.h>
-
+#include "contrib/semaphore.h"
 #include "knot/conf/conf.h"
 #include "knot/conf/confio.h"
 #include "knot/journal/journal_basic.h"
@@ -74,7 +73,7 @@ typedef struct zone
        struct zone_update *control_update;
 
        /*! \brief Ensue one COW tramsaction on zone's trees at a time. */
-       sem_t cow_lock;
+       knot_sem_t cow_lock;
 
        /*! \brief Ptr to journal DB (in struct server) */
        knot_lmdb_db_t *journaldb;
index 61fbf768df08901728e0b72862fc4e240abd0e56..288a11ca65939d7b5b688f4fa23a646458b60b86 100644 (file)
@@ -108,9 +108,9 @@ static int test_node_unified(zone_node_t *n1, void *v)
 
 static void test_zone_unified(zone_t *z)
 {
-       sem_wait(&z->cow_lock);
+       knot_sem_wait(&z->cow_lock);
        zone_tree_apply(z->contents->nodes, test_node_unified, NULL);
-       sem_post(&z->cow_lock);
+       knot_sem_post(&z->cow_lock);
 }
 
 void test_full(zone_t *zone, zs_scanner_t *sc)