[cairo-commit] 2 commits - src/cairo-mutex.c src/cairo-mutex-private.h
GitLab Mirror
gitlab-mirror at kemper.freedesktop.org
Sat May 27 10:20:59 UTC 2023
src/cairo-mutex-private.h | 2 +-
src/cairo-mutex.c | 41 ++++++++++++++++++++++++++++++++++-------
2 files changed, 35 insertions(+), 8 deletions(-)
New commits:
commit 0d7e0dcb175fd24e32600cb9cf0d97845a0c727f
Merge: 9831eba0e 46e538ddb
Author: Adrian Johnson <ajohnson at redneon.com>
Date: Sat May 27 10:20:57 2023 +0000
Merge branch 'issue-782' into 'master'
Use a spinlock to prevent race condition when initializing mutexes
Closes #782
See merge request cairo/cairo!478
commit 46e538ddbbf5ad02cb6a9d5ff4a483ba63660dc5
Author: Adrian Johnson <ajohnson at redneon.com>
Date: Sat May 27 17:59:21 2023 +0930
Use a spinlock to prevent race condition when initializing mutexes
Fixes #782
diff --git a/src/cairo-mutex-private.h b/src/cairo-mutex-private.h
index 61a7160a0..65732a180 100644
--- a/src/cairo-mutex-private.h
+++ b/src/cairo-mutex-private.h
@@ -53,7 +53,7 @@ cairo_private void _cairo_mutex_finalize (void);
#endif
/* only if using static initializer and/or finalizer define the boolean */
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER || _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
- cairo_private extern cairo_bool_t _cairo_mutex_initialized;
+ cairo_private extern int _cairo_mutex_initialized;
#endif
/* Finally, extern the static mutexes and undef */
diff --git a/src/cairo-mutex.c b/src/cairo-mutex.c
index 0a31dced3..5b6debeca 100644
--- a/src/cairo-mutex.c
+++ b/src/cairo-mutex.c
@@ -41,13 +41,17 @@
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER || _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
+#define _CAIRO_MUTEX_UNINITIALIZED 0
+#define _CAIRO_MUTEX_INITIALIZING 1
+#define _CAIRO_MUTEX_INITIALIZED 2
+
# if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
-# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE FALSE
+# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE _CAIRO_MUTEX_UNINITIALIZED
# else
-# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE TRUE
+# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE _CAIRO_MUTEX_INITIALIZED
# endif
-cairo_bool_t _cairo_mutex_initialized = _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE;
+int _cairo_mutex_initialized = _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE;
# undef _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE
@@ -56,24 +60,47 @@ cairo_bool_t _cairo_mutex_initialized = _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VA
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
void _cairo_mutex_initialize (void)
{
- if (_cairo_mutex_initialized)
+#if HAS_ATOMIC_OPS
+ int old_value;
+ do {
+ old_value = _cairo_atomic_int_cmpxchg_return_old (&_cairo_mutex_initialized,
+ _CAIRO_MUTEX_UNINITIALIZED,
+ _CAIRO_MUTEX_INITIALIZING);
+ if (old_value == _CAIRO_MUTEX_INITIALIZED)
+ return;
+
+ } while (old_value == _CAIRO_MUTEX_INITIALIZING);
+#else
+ if (_cairo_mutex_initialized == _CAIRO_MUTEX_INITIALIZED)
return;
- _cairo_mutex_initialized = TRUE;
+ _cairo_mutex_initialized = _CAIRO_MUTEX_INITIALIZED;
+#endif
#define CAIRO_MUTEX_DECLARE(mutex) CAIRO_MUTEX_INIT (mutex);
#include "cairo-mutex-list-private.h"
#undef CAIRO_MUTEX_DECLARE
+
+#if HAS_ATOMIC_OPS
+ _cairo_atomic_int_set_relaxed (&_cairo_mutex_initialized, _CAIRO_MUTEX_INITIALIZED);
+#endif
}
#endif
#if _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
void _cairo_mutex_finalize (void)
{
- if (!_cairo_mutex_initialized)
+#if HAS_ATOMIC_OPS
+ if (!_cairo_atomic_int_cmpxchg (&_cairo_mutex_initialized,
+ _CAIRO_MUTEX_INITIALIZED,
+ _CAIRO_MUTEX_UNINITIALIZED))
+ return;
+#else
+ if (_cairo_mutex_initialized != _CAIRO_MUTEX_INITIALIZED)
return;
- _cairo_mutex_initialized = FALSE;
+ _cairo_mutex_initialized = _CAIRO_MUTEX_UNINITIALIZED;
+#endif
#define CAIRO_MUTEX_DECLARE(mutex) CAIRO_MUTEX_FINI (mutex);
#include "cairo-mutex-list-private.h"
More information about the cairo-commit
mailing list