From 01032942580ba5f04ca9c809b049b76a68567aec Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 7 Jun 2014 17:10:57 -0400 Subject: error: Use atomics instead of mutexes. --- libdimension/compiler-internal.h | 4 ++- libdimension/error.c | 57 +++++++--------------------------------- libdimension/pool.c | 2 +- 3 files changed, 14 insertions(+), 49 deletions(-) diff --git a/libdimension/compiler-internal.h b/libdimension/compiler-internal.h index e59ece2..ffe8de3 100644 --- a/libdimension/compiler-internal.h +++ b/libdimension/compiler-internal.h @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2010-2011 Tavian Barnes * + * Copyright (C) 2010-2014 Tavian Barnes * * * * This file is part of The Dimension Library. * * * @@ -49,3 +49,5 @@ #define DMNSN_DESTRUCTOR #define DMNSN_LATE_DESTRUCTOR #endif + +#define atomic _Atomic diff --git a/libdimension/error.c b/libdimension/error.c index cdc9e31..1366c4c 100644 --- a/libdimension/error.c +++ b/libdimension/error.c @@ -24,11 +24,13 @@ */ #include "dimension-internal.h" +#include #include -#include +#include +#include #include #include -#include +#include /// Report internal errors in this file. #define DMNSN_LOCAL_ERROR(str) \ @@ -38,43 +40,14 @@ abort(); \ } while (0) -/// dmnsn_local_lock_mutex implementation. -static void -dmnsn_local_lock_mutex_impl(pthread_mutex_t *mutex) -{ - if (pthread_mutex_lock(mutex) != 0) { - DMNSN_LOCAL_ERROR("Couldn't lock mutex."); - } -} - -/// dmnsn_local_unlock_mutex implementation. -static void -dmnsn_local_unlock_mutex_impl(pthread_mutex_t *mutex) -{ - if (pthread_mutex_unlock(mutex) != 0) { - DMNSN_LOCAL_ERROR("Couldn't lock mutex."); - } -} - -/// Lock a mutex, bailing out without dmnsn_error() on error. -#define dmnsn_local_lock_mutex(mutex) \ - dmnsn_local_lock_mutex_impl((mutex)); { -/// Unlock a mutex, bailing out without dmnsn_error() on error. -#define dmnsn_local_unlock_mutex(mutex) \ - dmnsn_local_unlock_mutex_impl((mutex)); } - /// The default fatal error handler. static void dmnsn_default_fatal_error_fn(void); /// The current fatal error handler. -static dmnsn_fatal_error_fn *dmnsn_fatal = dmnsn_default_fatal_error_fn; -/// Mutex which protects \c dmnsn_fatal. -static pthread_mutex_t dmnsn_fatal_mutex = PTHREAD_MUTEX_INITIALIZER; +static atomic(dmnsn_fatal_error_fn *) dmnsn_fatal = ATOMIC_VAR_INIT(dmnsn_default_fatal_error_fn); /// The current resilience. -static bool dmnsn_always_die = false; -/// Mutex which protects \c dmnsn_always_die. -static pthread_mutex_t dmnsn_always_die_mutex = PTHREAD_MUTEX_INITIALIZER; +static atomic_bool dmnsn_always_die = ATOMIC_VAR_INIT(false); // Called by dmnsn_error macro (don't call directly) void @@ -84,10 +57,7 @@ dmnsn_report_error(bool die, const char *func, const char *file, // Save the value of errno int err = errno; - bool always_die; - dmnsn_local_lock_mutex(&dmnsn_always_die_mutex); - always_die = dmnsn_always_die; - dmnsn_local_unlock_mutex(&dmnsn_always_die_mutex); + bool always_die = atomic_load(&dmnsn_always_die); // Print the diagnostic string fprintf(stderr, "Dimension %s: %s, %s:%u: %s\n", @@ -135,27 +105,20 @@ dmnsn_report_error(bool die, const char *func, const char *file, void dmnsn_die_on_warnings(bool always_die) { - dmnsn_local_lock_mutex(&dmnsn_always_die_mutex); - dmnsn_always_die = always_die; - dmnsn_local_unlock_mutex(&dmnsn_always_die_mutex); + atomic_store(&dmnsn_always_die, always_die); } dmnsn_fatal_error_fn * dmnsn_get_fatal_error_fn(void) { - dmnsn_fatal_error_fn *fatal; - dmnsn_local_lock_mutex(&dmnsn_fatal_mutex); - fatal = dmnsn_fatal; - dmnsn_local_unlock_mutex(&dmnsn_fatal_mutex); + dmnsn_fatal_error_fn *fatal = atomic_load(&dmnsn_fatal); return fatal; } void dmnsn_set_fatal_error_fn(dmnsn_fatal_error_fn *fatal) { - dmnsn_local_lock_mutex(&dmnsn_fatal_mutex); - dmnsn_fatal = fatal; - dmnsn_local_unlock_mutex(&dmnsn_fatal_mutex); + atomic_store(&dmnsn_fatal, fatal); } static void diff --git a/libdimension/pool.c b/libdimension/pool.c index bd31677..82cd46f 100644 --- a/libdimension/pool.c +++ b/libdimension/pool.c @@ -50,7 +50,7 @@ struct dmnsn_pool { /// Thread-local block. pthread_key_t thread_block; /// Global chain of pools. - _Atomic(dmnsn_pool_block *) chain; + atomic(dmnsn_pool_block *) chain; }; dmnsn_pool * -- cgit v1.2.3