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
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 \
--- /dev/null
+/* 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);
+ }
+}
--- /dev/null
+/* 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);
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);
}
}
}
if (ctx->cow_mutex != NULL) {
- sem_post(ctx->cow_mutex);
+ knot_sem_post(ctx->cow_mutex);
}
free(ctx->contents->nodes);
#pragma once
-#include <semaphore.h>
-
+#include "contrib/semaphore.h"
#include "knot/zone/contents.h"
#include "knot/updates/changesets.h"
#include "contrib/ucw/lists.h"
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;
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;
ret = init_full(update, zone);
}
if (ret != KNOT_EOK) {
- sem_post(&zone->cow_lock);
+ knot_sem_post(&zone->cow_lock);
free(update->a_ctx);
}
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) {
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);
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);
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);
#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"
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;
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)