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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user