From b88597c2186689e8eaaf3cb3f5b2601b449781fe Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Mon, 18 May 2015 12:41:51 +0500 Subject: Make switching to threaded malloc safe to be called from threads For a long time this function was only intended to be used from the main thread, but since out implementation of parallel range (which is currently only used by mesh deform modifier) we might want to switch to threaded alloc from object update thread. Now we're using spinlock around the check, which makes the code safe to be used from all over the place. We might consider using a bit of atomics operations magic there, but it's not so much important for now, this code is not used in the performance critical code path. --- source/blender/blenlib/intern/threads.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source/blender/blenlib/intern/threads.c') diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index da06500565c..a404f46a6e2 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -802,10 +802,12 @@ void BLI_begin_threaded_malloc(void) /* Used for debug only */ /* BLI_assert(thread_levels >= 0); */ + BLI_spin_lock(&_malloc_lock); if (thread_levels == 0) { MEM_set_lock_callback(BLI_lock_malloc_thread, BLI_unlock_malloc_thread); } thread_levels++; + BLI_spin_unlock(&_malloc_lock); } void BLI_end_threaded_malloc(void) @@ -813,8 +815,10 @@ void BLI_end_threaded_malloc(void) /* Used for debug only */ /* BLI_assert(thread_levels >= 0); */ + BLI_spin_lock(&_malloc_lock); thread_levels--; if (thread_levels == 0) MEM_set_lock_callback(NULL, NULL); + BLI_spin_unlock(&_malloc_lock); } -- cgit v1.2.3