From 33f3143d3a9a036dc95cf6e88bcb7a66c46aa8fd Mon Sep 17 00:00:00 2001 From: Philip Schaten Date: Tue, 27 Feb 2024 17:27:35 +0100 Subject: [PATCH] mutual exclusion with bart_lock; this is a wrapper around the locks from the omp runtime - was required to be able to easily switch between mutex implementations. --- src/misc/lock.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/lock.h | 8 +++++++ 2 files changed, 64 insertions(+) create mode 100644 src/misc/lock.c create mode 100644 src/misc/lock.h diff --git a/src/misc/lock.c b/src/misc/lock.c new file mode 100644 index 000000000..b321f5fbd --- /dev/null +++ b/src/misc/lock.c @@ -0,0 +1,56 @@ +/* Copyright 2024. Institute of Biomedical Imaging. TU Graz. + * All rights reserved. Use of this source code is governed by + * a BSD-style license which can be found in the LICENSE file. + * + * Authors: + * 2024 Philip Schaten + */ + +#ifdef _OPENMP +#include +#endif + +#include "misc/misc.h" + +#include "lock.h" + +struct bart_lock { +#ifdef _OPENMP + omp_lock_t omp; +#else + int dummy; +#endif +}; + +void bart_lock(bart_lock_t* lock) +{ +#ifdef _OPENMP + omp_set_lock(&lock->omp); +#endif +} + +void bart_unlock(bart_lock_t* lock) +{ +#ifdef _OPENMP + omp_unset_lock(&lock->omp); +#endif +} + +bart_lock_t* bart_lock_create(void) +{ + bart_lock_t* lock = xmalloc(sizeof *lock); + +#ifdef _OPENMP + omp_init_lock(&lock->omp); +#endif + return lock; +} + +void bart_lock_destroy(bart_lock_t* lock) +{ +#ifdef _OPENMP + omp_destroy_lock(&lock->omp); +#endif + xfree(lock); +} + diff --git a/src/misc/lock.h b/src/misc/lock.h new file mode 100644 index 000000000..d0b3f08e1 --- /dev/null +++ b/src/misc/lock.h @@ -0,0 +1,8 @@ + +typedef struct bart_lock bart_lock_t; + +void bart_lock(bart_lock_t* lock); +void bart_unlock(bart_lock_t* lock); +void bart_lock_destroy(bart_lock_t* x); +bart_lock_t* bart_lock_create(void); +