[Bf-blender-cvs] [d70ffd3] master: BLI_threads: add an helper to wait on a condition using a global mutex.

Bastien Montagne noreply at git.blender.org
Mon Aug 10 15:10:03 CEST 2015


Commit: d70ffd375fdf444f50a693f74ca79fe248337cda
Author: Bastien Montagne
Date:   Mon Aug 10 15:03:31 2015 +0200
Branches: master
https://developer.blender.org/rBd70ffd375fdf444f50a693f74ca79fe248337cda

BLI_threads: add an helper to wait on a condition using a global mutex.

Also, factorized internal code to get global mutex from its ID.

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

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

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

diff --git a/source/blender/blenlib/BLI_threads.h b/source/blender/blenlib/BLI_threads.h
index b2ead15..6ae833d 100644
--- a/source/blender/blenlib/BLI_threads.h
+++ b/source/blender/blenlib/BLI_threads.h
@@ -158,6 +158,7 @@ typedef pthread_cond_t ThreadCondition;
 
 void BLI_condition_init(ThreadCondition *cond);
 void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex);
+void BLI_condition_wait_global_mutex(ThreadCondition *cond, const int type);
 void BLI_condition_notify_one(ThreadCondition *cond);
 void BLI_condition_notify_all(ThreadCondition *cond);
 void BLI_condition_end(ThreadCondition *cond);
diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c
index 42f7a74..b609818 100644
--- a/source/blender/blenlib/intern/threads.c
+++ b/source/blender/blenlib/intern/threads.c
@@ -389,56 +389,45 @@ int BLI_system_num_threads_override_get(void)
 
 /* Global Mutex Locks */
 
+static ThreadMutex *global_mutex_from_type(const int type)
+{
+	switch (type) {
+		case LOCK_IMAGE:
+			return &_image_lock;
+		case LOCK_DRAW_IMAGE:
+			return &_image_draw_lock;
+		case LOCK_VIEWER:
+			return &_viewer_lock;
+		case LOCK_CUSTOM1:
+			return &_custom1_lock;
+		case LOCK_RCACHE:
+			return &_rcache_lock;
+		case LOCK_OPENGL:
+			return &_opengl_lock;
+		case LOCK_NODES:
+			return &_nodes_lock;
+		case LOCK_MOVIECLIP:
+			return &_movieclip_lock;
+		case LOCK_COLORMANAGE:
+			return &_colormanage_lock;
+		case LOCK_FFTW:
+			return &_fftw_lock;
+		case LOCK_VIEW3D:
+			return &_view3d_lock;
+		default:
+			BLI_assert(0);
+			return NULL;
+	}
+}
+
 void BLI_lock_thread(int type)
 {
-	if (type == LOCK_IMAGE)
-		pthread_mutex_lock(&_image_lock);
-	else if (type == LOCK_DRAW_IMAGE)
-		pthread_mutex_lock(&_image_draw_lock);
-	else if (type == LOCK_VIEWER)
-		pthread_mutex_lock(&_viewer_lock);
-	else if (type == LOCK_CUSTOM1)
-		pthread_mutex_lock(&_custom1_lock);
-	else if (type == LOCK_RCACHE)
-		pthread_mutex_lock(&_rcache_lock);
-	else if (type == LOCK_OPENGL)
-		pthread_mutex_lock(&_opengl_lock);
-	else if (type == LOCK_NODES)
-		pthread_mutex_lock(&_nodes_lock);
-	else if (type == LOCK_MOVIECLIP)
-		pthread_mutex_lock(&_movieclip_lock);
-	else if (type == LOCK_COLORMANAGE)
-		pthread_mutex_lock(&_colormanage_lock);
-	else if (type == LOCK_FFTW)
-		pthread_mutex_lock(&_fftw_lock);
-	else if (type == LOCK_VIEW3D)
-		pthread_mutex_lock(&_view3d_lock);
+	pthread_mutex_lock(global_mutex_from_type(type));
 }
 
 void BLI_unlock_thread(int type)
 {
-	if (type == LOCK_IMAGE)
-		pthread_mutex_unlock(&_image_lock);
-	else if (type == LOCK_DRAW_IMAGE)
-		pthread_mutex_unlock(&_image_draw_lock);
-	else if (type == LOCK_VIEWER)
-		pthread_mutex_unlock(&_viewer_lock);
-	else if (type == LOCK_CUSTOM1)
-		pthread_mutex_unlock(&_custom1_lock);
-	else if (type == LOCK_RCACHE)
-		pthread_mutex_unlock(&_rcache_lock);
-	else if (type == LOCK_OPENGL)
-		pthread_mutex_unlock(&_opengl_lock);
-	else if (type == LOCK_NODES)
-		pthread_mutex_unlock(&_nodes_lock);
-	else if (type == LOCK_MOVIECLIP)
-		pthread_mutex_unlock(&_movieclip_lock);
-	else if (type == LOCK_COLORMANAGE)
-		pthread_mutex_unlock(&_colormanage_lock);
-	else if (type == LOCK_FFTW)
-		pthread_mutex_unlock(&_fftw_lock);
-	else if (type == LOCK_VIEW3D)
-		pthread_mutex_unlock(&_view3d_lock);
+	pthread_mutex_unlock(global_mutex_from_type(type));
 }
 
 /* Mutex Locks */
@@ -619,6 +608,11 @@ void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex)
 	pthread_cond_wait(cond, mutex);
 }
 
+void BLI_condition_wait_global_mutex(ThreadCondition *cond, const int type)
+{
+	pthread_cond_wait(cond, global_mutex_from_type(type));
+}
+
 void BLI_condition_notify_one(ThreadCondition *cond)
 {
 	pthread_cond_signal(cond);




More information about the Bf-blender-cvs mailing list