[Bf-blender-cvs] [b88597c] master: Make switching to threaded malloc safe to be called from threads

Sergey Sharybin noreply at git.blender.org
Mon May 18 13:41:00 CEST 2015


Commit: b88597c2186689e8eaaf3cb3f5b2601b449781fe
Author: Sergey Sharybin
Date:   Mon May 18 12:41:51 2015 +0500
Branches: master
https://developer.blender.org/rBb88597c2186689e8eaaf3cb3f5b2601b449781fe

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.

===================================================================

M	source/blender/blenlib/intern/threads.c

===================================================================

diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c
index da06500..a404f46 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);
 }




More information about the Bf-blender-cvs mailing list