[Bf-blender-cvs] [e177c51] master: Use atomic operations in task pool

Sergey Sharybin noreply at git.blender.org
Tue Dec 2 11:56:08 CET 2014


Commit: e177c5143055306d4b128663d537568bd1256645
Author: Sergey Sharybin
Date:   Tue Dec 2 15:23:58 2014 +0500
Branches: master
https://developer.blender.org/rBe177c5143055306d4b128663d537568bd1256645

Use atomic operations in task pool

This ensures proper values of currently running tasks in the pool
(previously difference between mutex locks when acquiring new job
and releasing it might in theory give wrong values).

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

M	source/blender/blenlib/CMakeLists.txt
M	source/blender/blenlib/SConscript
M	source/blender/blenlib/intern/task.c

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

diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index cb84c0d..55a5d91 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -30,6 +30,7 @@ set(INC
 	# ../blenkernel  # dont add this back!
 	../makesdna
 	../../../intern/guardedalloc
+	../../../intern/atomic
 	../../../extern/wcwidth
 )
 
diff --git a/source/blender/blenlib/SConscript b/source/blender/blenlib/SConscript
index 2dabb5a..55747a4 100644
--- a/source/blender/blenlib/SConscript
+++ b/source/blender/blenlib/SConscript
@@ -35,6 +35,7 @@ incs = [
     '.',
     '#/extern/wcwidth',
     '#/intern/guardedalloc',
+    '#/intern/atomic',
     '../makesdna',
     env['BF_FREETYPE_INC'],
     env['BF_ZLIB_INC'],
diff --git a/source/blender/blenlib/intern/task.c b/source/blender/blenlib/intern/task.c
index e4cded1..d187a8d 100644
--- a/source/blender/blenlib/intern/task.c
+++ b/source/blender/blenlib/intern/task.c
@@ -33,6 +33,8 @@
 #include "BLI_task.h"
 #include "BLI_threads.h"
 
+#include "atomic_ops.h"
+
 /* Types */
 
 typedef struct Task {
@@ -49,8 +51,8 @@ struct TaskPool {
 
 	volatile size_t num;
 	volatile size_t done;
-	volatile int num_threads;
-	volatile int currently_running_tasks;
+	size_t num_threads;
+	size_t currently_running_tasks;
 	ThreadMutex num_mutex;
 	ThreadCondition num_cond;
 
@@ -86,7 +88,7 @@ static void task_pool_num_decrease(TaskPool *pool, size_t done)
 	BLI_assert(pool->num >= done);
 
 	pool->num -= done;
-	pool->currently_running_tasks -= done;
+	atomic_sub_z(&pool->currently_running_tasks, done);
 	pool->done += done;
 
 	if (pool->num == 0)
@@ -130,7 +132,7 @@ static bool task_scheduler_thread_wait_pop(TaskScheduler *scheduler, Task **task
 			{
 				*task = current_task;
 				found_task = true;
-				pool->currently_running_tasks++;
+				atomic_add_z(&pool->currently_running_tasks, 1);
 				BLI_remlink(&scheduler->queue, *task);
 				break;
 			}
@@ -392,7 +394,7 @@ void BLI_task_pool_work_and_wait(TaskPool *pool)
 		/* if found task, do it, otherwise wait until other tasks are done */
 		if (found_task) {
 			/* run task */
-			pool->currently_running_tasks++;
+			atomic_add_z(&pool->currently_running_tasks, 1);
 			work_task->run(pool, work_task->taskdata, 0);
 
 			/* delete task */




More information about the Bf-blender-cvs mailing list