From c0f8e15295371ae69b38185d3ee5659cdd1af386 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Mon, 19 Aug 2013 10:51:40 +0000 Subject: Speedup for guarded allocator - Re-arrange locks, so no actual memory allocation (which is relatively slow) happens from inside the lock. operation system will take care of locks which might be needed there on it's own. - Use spin lock instead of mutex, since it's just list operations happens from inside lock, no need in mutex here. - Use atomic operations for memory in use and total used blocks counters. This makes guarded allocator almost the same speed as non-guarded one in files from Tube project. There're still MemHead/MemTail overhead which might be bad for CPU cache utilization --- source/blender/blenlib/intern/threads.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (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 2b6fb52c49c..c8b84d9310a 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -107,7 +107,7 @@ static void *thread_tls_data; * BLI_end_threads(&lb); * ************************************************ */ -static pthread_mutex_t _malloc_lock = PTHREAD_MUTEX_INITIALIZER; +static SpinLock _malloc_lock; static pthread_mutex_t _image_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t _image_draw_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t _viewer_lock = PTHREAD_MUTEX_INITIALIZER; @@ -134,17 +134,24 @@ typedef struct ThreadSlot { static void BLI_lock_malloc_thread(void) { - pthread_mutex_lock(&_malloc_lock); + BLI_spin_lock(&_malloc_lock); } static void BLI_unlock_malloc_thread(void) { - pthread_mutex_unlock(&_malloc_lock); + BLI_spin_unlock(&_malloc_lock); } void BLI_threadapi_init(void) { mainid = pthread_self(); + + BLI_spin_init(&_malloc_lock); +} + +void BLI_threadapi_exit(void) +{ + BLI_spin_end(&_malloc_lock); } /* tot = 0 only initializes malloc mutex in a safe way (see sequence.c) -- cgit v1.2.3