| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 23:14:24 +11:00
										 |  |  | /** \file blender/blenlib/intern/task.c
 | 
					
						
							|  |  |  |  *  \ingroup bli | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * A generic task system which can be used for any task based subsystem. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  | #include "DNA_listBase.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | #include "BLI_listbase.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | #include "BLI_math.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  | #include "BLI_mempool.h"
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | #include "BLI_task.h"
 | 
					
						
							|  |  |  | #include "BLI_threads.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:23:58 +05:00
										 |  |  | #include "atomic_ops.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | /* Define this to enable some detailed statistic print. */ | 
					
						
							|  |  |  | #undef DEBUG_STATS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | /* Types */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | /* Number of per-thread pre-allocated tasks.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For more details see description of TaskMemPool. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define MEMPOOL_SIZE 256
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | /* Number of tasks which are pushed directly to local thread queue.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This allows thread to fetch next task without locking the whole queue. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-05-31 14:52:45 +02:00
										 |  |  | #define LOCAL_QUEUE_SIZE 1
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | /* Number of tasks which are allowed to be scheduled in a delayed manner.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This allows to use less locks per graph node children schedule. More details | 
					
						
							|  |  |  |  * could be found at TaskThreadLocalStorage::do_delayed_push. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define DELAYED_QUEUE_SIZE 4096
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | #ifndef NDEBUG
 | 
					
						
							| 
									
										
										
										
											2017-03-08 09:41:38 +01:00
										 |  |  | #  define ASSERT_THREAD_ID(scheduler, thread_id)                              \
 | 
					
						
							|  |  |  | 	do {                                                                      \ | 
					
						
							|  |  |  | 		if (!BLI_thread_is_main()) {                                          \ | 
					
						
							|  |  |  | 			TaskThread *thread = pthread_getspecific(scheduler->tls_id_key);  \ | 
					
						
							|  |  |  | 			if (thread == NULL) {                                             \ | 
					
						
							|  |  |  | 				BLI_assert(thread_id == 0);                                   \ | 
					
						
							|  |  |  | 			}                                                                 \ | 
					
						
							|  |  |  | 			else {                                                            \ | 
					
						
							|  |  |  | 				BLI_assert(thread_id == thread->id);                          \ | 
					
						
							|  |  |  | 			}                                                                 \ | 
					
						
							|  |  |  | 		}                                                                     \ | 
					
						
							|  |  |  | 		else {                                                                \ | 
					
						
							|  |  |  | 			BLI_assert(thread_id == 0);                                       \ | 
					
						
							|  |  |  | 		}                                                                     \ | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	} while (false) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #  define ASSERT_THREAD_ID(scheduler, thread_id)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | typedef struct Task { | 
					
						
							|  |  |  | 	struct Task *next, *prev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	TaskRunFunction run; | 
					
						
							|  |  |  | 	void *taskdata; | 
					
						
							|  |  |  | 	bool free_taskdata; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | 	TaskFreeFunction freedata; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	TaskPool *pool; | 
					
						
							|  |  |  | } Task; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | /* This is a per-thread storage of pre-allocated tasks.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The idea behind this is simple: reduce amount of malloc() calls when pushing | 
					
						
							|  |  |  |  * new task to the pool. This is done by keeping memory from the tasks which | 
					
						
							|  |  |  |  * were finished already, so instead of freeing that memory we put it to the | 
					
						
							|  |  |  |  * pool for the later re-use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The tricky part here is to avoid any inter-thread synchronization, hence no | 
					
						
							|  |  |  |  * lock must exist around this pool. The pool will become an owner of the pointer | 
					
						
							|  |  |  |  * from freed task, and only corresponding thread will be able to use this pool | 
					
						
							|  |  |  |  * (no memory stealing and such). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This leads to the following use of the pool: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - task_push() should provide proper thread ID from which the task is being | 
					
						
							|  |  |  |  *   pushed from. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - Task allocation function which check corresponding memory pool and if there | 
					
						
							|  |  |  |  *   is any memory in there it'll mark memory as re-used, remove it from the pool | 
					
						
							|  |  |  |  *   and use that memory for the new task. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   At this moment task queue owns the memory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - When task is done and task_free() is called the memory will be put to the | 
					
						
							|  |  |  |  *  pool which corresponds to a thread which handled the task. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct TaskMemPool { | 
					
						
							|  |  |  | 	/* Number of pre-allocated tasks in the pool. */ | 
					
						
							|  |  |  | 	int num_tasks; | 
					
						
							|  |  |  | 	/* Pre-allocated task memory pointers. */ | 
					
						
							|  |  |  | 	Task *tasks[MEMPOOL_SIZE]; | 
					
						
							|  |  |  | } TaskMemPool; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | typedef struct TaskMemPoolStats { | 
					
						
							|  |  |  | 	/* Number of allocations. */ | 
					
						
							|  |  |  | 	int num_alloc; | 
					
						
							|  |  |  | 	/* Number of avoided allocations (pointer was re-used from the pool). */ | 
					
						
							|  |  |  | 	int num_reuse; | 
					
						
							|  |  |  | 	/* Number of discarded memory due to pool saturation, */ | 
					
						
							|  |  |  | 	int num_discard; | 
					
						
							|  |  |  | } TaskMemPoolStats; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | typedef struct TaskThreadLocalStorage { | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	/* Memory pool for faster task allocation.
 | 
					
						
							|  |  |  | 	 * The idea is to re-use memory of finished/discarded tasks by this thread. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 	TaskMemPool task_mempool; | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Local queue keeps thread alive by keeping small amount of tasks ready
 | 
					
						
							|  |  |  | 	 * to be picked up without causing global thread locks for synchronization. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	int num_local_queue; | 
					
						
							| 
									
										
										
										
											2017-05-31 14:52:45 +02:00
										 |  |  | 	Task *local_queue[LOCAL_QUEUE_SIZE]; | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Thread can be marked for delayed tasks push. This is helpful when it's
 | 
					
						
							|  |  |  | 	 * know that lots of subsequent task pushed will happen from the same thread | 
					
						
							|  |  |  | 	 * without "interrupting" for task execution. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * We try to accumulate as much tasks as possible in a local queue without | 
					
						
							|  |  |  | 	 * any locks first, and then we push all of them into a scheduler's queue | 
					
						
							|  |  |  | 	 * from within a single mutex lock. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	bool do_delayed_push; | 
					
						
							|  |  |  | 	int num_delayed_queue; | 
					
						
							|  |  |  | 	Task *delayed_queue[DELAYED_QUEUE_SIZE]; | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | } TaskThreadLocalStorage; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | struct TaskPool { | 
					
						
							|  |  |  | 	TaskScheduler *scheduler; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	volatile size_t num; | 
					
						
							|  |  |  | 	ThreadMutex num_mutex; | 
					
						
							|  |  |  | 	ThreadCondition num_cond; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void *userdata; | 
					
						
							|  |  |  | 	ThreadMutex user_mutex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	volatile bool do_cancel; | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	volatile bool do_work; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	volatile bool is_suspended; | 
					
						
							|  |  |  | 	ListBase suspended_queue; | 
					
						
							|  |  |  | 	size_t num_suspended; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	/* If set, this pool may never be work_and_wait'ed, which means TaskScheduler
 | 
					
						
							|  |  |  | 	 * has to use its special background fallback thread in case we are in | 
					
						
							|  |  |  | 	 * single-threaded situation. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 	bool run_in_background; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	/* This is a task scheduler's ID of a thread at which pool was constructed.
 | 
					
						
							|  |  |  | 	 * It will be used to access task TLS. | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	int thread_id; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 	/* For the pools which are created from non-main thread which is not a
 | 
					
						
							|  |  |  | 	 * scheduler worker thread we can't re-use any of scheduler's threads TLS | 
					
						
							|  |  |  | 	 * and have to use our own one. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	bool use_local_tls; | 
					
						
							|  |  |  | 	TaskThreadLocalStorage local_tls; | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  | 	pthread_t creator_thread_id; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 	TaskMemPoolStats *mempool_stats; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct TaskScheduler { | 
					
						
							|  |  |  | 	pthread_t *threads; | 
					
						
							|  |  |  | 	struct TaskThread *task_threads; | 
					
						
							|  |  |  | 	int num_threads; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 	bool background_thread_only; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ListBase queue; | 
					
						
							|  |  |  | 	ThreadMutex queue_mutex; | 
					
						
							|  |  |  | 	ThreadCondition queue_cond; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	volatile bool do_exit; | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* NOTE: In pthread's TLS we store the whole TaskThread structure. */ | 
					
						
							|  |  |  | 	pthread_key_t tls_id_key; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct TaskThread { | 
					
						
							|  |  |  | 	TaskScheduler *scheduler; | 
					
						
							|  |  |  | 	int id; | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 	TaskThreadLocalStorage tls; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | } TaskThread; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | /* Helper */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | BLI_INLINE void task_data_free(Task *task, const int thread_id) | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (task->free_taskdata) { | 
					
						
							|  |  |  | 		if (task->freedata) { | 
					
						
							|  |  |  | 			task->freedata(task->pool, task->taskdata, thread_id); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			MEM_freeN(task->taskdata); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | BLI_INLINE void initialize_task_tls(TaskThreadLocalStorage *tls) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memset(tls, 0, sizeof(TaskThreadLocalStorage)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | BLI_INLINE TaskThreadLocalStorage *get_task_tls(TaskPool *pool, | 
					
						
							|  |  |  |                                                 const int thread_id) | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 	TaskScheduler *scheduler = pool->scheduler; | 
					
						
							|  |  |  | 	BLI_assert(thread_id >= 0); | 
					
						
							|  |  |  | 	BLI_assert(thread_id <= scheduler->num_threads); | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | 	if (pool->use_local_tls && thread_id == 0) { | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 		BLI_assert(pool->thread_id == 0); | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | 		BLI_assert(!BLI_thread_is_main()); | 
					
						
							|  |  |  | 		BLI_assert(pthread_equal(pthread_self(), pool->creator_thread_id)); | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 		return &pool->local_tls; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	if (thread_id == 0) { | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | 		BLI_assert(BLI_thread_is_main()); | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 		return &scheduler->task_threads[pool->thread_id].tls; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	return &scheduler->task_threads[thread_id].tls; | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BLI_INLINE void free_task_tls(TaskThreadLocalStorage *tls) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	TaskMemPool *task_mempool = &tls->task_mempool; | 
					
						
							|  |  |  | 	for (int i = 0; i < task_mempool->num_tasks; ++i) { | 
					
						
							|  |  |  | 		MEM_freeN(task_mempool->tasks[i]); | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Task *task_alloc(TaskPool *pool, const int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:33:27 +01:00
										 |  |  | 	BLI_assert(thread_id <= pool->scheduler->num_threads); | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	if (thread_id != -1) { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:33:27 +01:00
										 |  |  | 		BLI_assert(thread_id >= 0); | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 		BLI_assert(thread_id <= pool->scheduler->num_threads); | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 		TaskThreadLocalStorage *tls = get_task_tls(pool, thread_id); | 
					
						
							|  |  |  | 		TaskMemPool *task_mempool = &tls->task_mempool; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 		/* Try to re-use task memory from a thread local storage. */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 		if (task_mempool->num_tasks > 0) { | 
					
						
							|  |  |  | 			--task_mempool->num_tasks; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 			/* Success! We've just avoided task allocation. */ | 
					
						
							|  |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 			pool->mempool_stats[thread_id].num_reuse++; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 			return task_mempool->tasks[task_mempool->num_tasks]; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		/* We are doomed to allocate new task data. */ | 
					
						
							|  |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 		pool->mempool_stats[thread_id].num_alloc++; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return MEM_mallocN(sizeof(Task), "New task"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void task_free(TaskPool *pool, Task *task, const int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	task_data_free(task, thread_id); | 
					
						
							| 
									
										
										
										
											2017-03-06 11:33:27 +01:00
										 |  |  | 	BLI_assert(thread_id >= 0); | 
					
						
							|  |  |  | 	BLI_assert(thread_id <= pool->scheduler->num_threads); | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | 	if (thread_id == 0) { | 
					
						
							|  |  |  | 		BLI_assert(pool->use_local_tls || BLI_thread_is_main()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 	TaskThreadLocalStorage *tls = get_task_tls(pool, thread_id); | 
					
						
							|  |  |  | 	TaskMemPool *task_mempool = &tls->task_mempool; | 
					
						
							|  |  |  | 	if (task_mempool->num_tasks < MEMPOOL_SIZE - 1) { | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 		/* Successfully allowed the task to be re-used later. */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 		task_mempool->tasks[task_mempool->num_tasks] = task; | 
					
						
							|  |  |  | 		++task_mempool->num_tasks; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* Local storage saturated, no other way than just discard
 | 
					
						
							|  |  |  | 		 * the memory. | 
					
						
							|  |  |  | 		 * | 
					
						
							|  |  |  | 		 * TODO(sergey): We can perhaps store such pointer in a global | 
					
						
							|  |  |  | 		 * scheduler pool, maybe it'll be faster than discarding and | 
					
						
							|  |  |  | 		 * allocating again. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		MEM_freeN(task); | 
					
						
							|  |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 		pool->mempool_stats[thread_id].num_discard++; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | /* Task Scheduler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void task_pool_num_decrease(TaskPool *pool, size_t done) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 	BLI_mutex_lock(&pool->num_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	BLI_assert(pool->num >= done); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 	pool->num -= done; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 	if (pool->num == 0) | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 		BLI_condition_notify_all(&pool->num_cond); | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&pool->num_mutex); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | static void task_pool_num_increase(TaskPool *pool, size_t new) | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 	BLI_mutex_lock(&pool->num_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	pool->num += new; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	BLI_condition_notify_all(&pool->num_cond); | 
					
						
							| 
									
										
										
										
											2016-05-10 15:43:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&pool->num_mutex); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool task_scheduler_thread_wait_pop(TaskScheduler *scheduler, Task **task) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-21 11:31:30 +01:00
										 |  |  | 	bool found_task = false; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (!scheduler->queue.first && !scheduler->do_exit) | 
					
						
							|  |  |  | 		BLI_condition_wait(&scheduler->queue_cond, &scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 11:31:30 +01:00
										 |  |  | 	do { | 
					
						
							|  |  |  | 		Task *current_task; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:35:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* Assuming we can only have a void queue in 'exit' case here seems logical (we should only be here after
 | 
					
						
							|  |  |  | 		 * our worker thread has been woken up from a condition_wait(), which only happens after a new task was | 
					
						
							|  |  |  | 		 * added to the queue), but it is wrong. | 
					
						
							| 
									
										
										
										
											2015-11-06 05:09:14 +11:00
										 |  |  | 		 * Waiting on condition may wake up the thread even if condition is not signaled (spurious wake-ups), and some | 
					
						
							| 
									
										
										
										
											2015-11-02 16:35:53 +01:00
										 |  |  | 		 * race condition may also empty the queue **after** condition has been signaled, but **before** awoken thread | 
					
						
							|  |  |  | 		 * reaches this point... | 
					
						
							| 
									
										
										
										
											2015-11-06 05:09:14 +11:00
										 |  |  | 		 * See http://stackoverflow.com/questions/8594591
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:35:53 +01:00
										 |  |  | 		 * | 
					
						
							|  |  |  | 		 * So we only abort here if do_exit is set. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (scheduler->do_exit) { | 
					
						
							| 
									
										
										
										
											2014-11-21 11:31:30 +01:00
										 |  |  | 			BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-11-02 16:35:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 11:31:30 +01:00
										 |  |  | 		for (current_task = scheduler->queue.first; | 
					
						
							|  |  |  | 		     current_task != NULL; | 
					
						
							|  |  |  | 		     current_task = current_task->next) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			TaskPool *pool = current_task->pool; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (scheduler->background_thread_only && !pool->run_in_background) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 10:48:13 +01:00
										 |  |  | 			*task = current_task; | 
					
						
							|  |  |  | 			found_task = true; | 
					
						
							|  |  |  | 			BLI_remlink(&scheduler->queue, *task); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2014-11-21 11:31:30 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (!found_task) | 
					
						
							|  |  |  | 			BLI_condition_wait(&scheduler->queue_cond, &scheduler->queue_mutex); | 
					
						
							|  |  |  | 	} while (!found_task); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | BLI_INLINE void handle_local_queue(TaskThreadLocalStorage *tls, | 
					
						
							|  |  |  |                                    const int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	while (tls->num_local_queue > 0) { | 
					
						
							|  |  |  | 		/* We pop task from queue before handling it so handler of the task can
 | 
					
						
							|  |  |  | 		 * push next job to the local queue. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		tls->num_local_queue--; | 
					
						
							|  |  |  | 		Task *local_task = tls->local_queue[tls->num_local_queue]; | 
					
						
							|  |  |  | 		/* TODO(sergey): Double-check work_and_wait() doesn't handle other's
 | 
					
						
							|  |  |  | 		 * pool tasks. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		TaskPool *local_pool = local_task->pool; | 
					
						
							|  |  |  | 		local_task->run(local_pool, local_task->taskdata, thread_id); | 
					
						
							|  |  |  | 		task_free(local_pool, local_task, thread_id); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | static void *task_scheduler_thread_run(void *thread_p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	TaskThread *thread = (TaskThread *) thread_p; | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	TaskThreadLocalStorage *tls = &thread->tls; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	TaskScheduler *scheduler = thread->scheduler; | 
					
						
							|  |  |  | 	int thread_id = thread->id; | 
					
						
							|  |  |  | 	Task *task; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	pthread_setspecific(scheduler->tls_id_key, thread); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	/* keep popping off tasks */ | 
					
						
							|  |  |  | 	while (task_scheduler_thread_wait_pop(scheduler, &task)) { | 
					
						
							|  |  |  | 		TaskPool *pool = task->pool; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* run task */ | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 		BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 		task->run(pool, task->taskdata, thread_id); | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 		BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* delete task */ | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 		task_free(pool, task, thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 		/* Handle all tasks from local queue. */ | 
					
						
							|  |  |  | 		handle_local_queue(tls, thread_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 		/* notify pool task was done */ | 
					
						
							|  |  |  | 		task_pool_num_decrease(pool, 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TaskScheduler *BLI_task_scheduler_create(int num_threads) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	TaskScheduler *scheduler = MEM_callocN(sizeof(TaskScheduler), "TaskScheduler"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* multiple places can use this task scheduler, sharing the same
 | 
					
						
							|  |  |  | 	 * threads, so we keep track of the number of users. */ | 
					
						
							|  |  |  | 	scheduler->do_exit = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 06:07:10 +11:00
										 |  |  | 	BLI_listbase_clear(&scheduler->queue); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	BLI_mutex_init(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 	BLI_condition_init(&scheduler->queue_cond); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (num_threads == 0) { | 
					
						
							|  |  |  | 		/* automatic number of threads will be main thread + num cores */ | 
					
						
							|  |  |  | 		num_threads = BLI_system_thread_count(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* main thread will also work, so we count it too */ | 
					
						
							|  |  |  | 	num_threads -= 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 	/* Add background-only thread if needed. */ | 
					
						
							|  |  |  | 	if (num_threads == 0) { | 
					
						
							| 
									
										
										
										
											2017-03-03 15:53:02 +01:00
										 |  |  | 		scheduler->background_thread_only = true; | 
					
						
							|  |  |  | 		num_threads = 1; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 	scheduler->task_threads = MEM_mallocN(sizeof(TaskThread) * (num_threads + 1), | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 	                                      "TaskScheduler task threads"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 	/* Initialize TLS for main thread. */ | 
					
						
							|  |  |  | 	initialize_task_tls(&scheduler->task_threads[0].tls); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	pthread_key_create(&scheduler->tls_id_key, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	/* launch threads that will be waiting for work */ | 
					
						
							|  |  |  | 	if (num_threads > 0) { | 
					
						
							|  |  |  | 		int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		scheduler->num_threads = num_threads; | 
					
						
							|  |  |  | 		scheduler->threads = MEM_callocN(sizeof(pthread_t) * num_threads, "TaskScheduler threads"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (i = 0; i < num_threads; i++) { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 			TaskThread *thread = &scheduler->task_threads[i + 1]; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 			thread->scheduler = scheduler; | 
					
						
							|  |  |  | 			thread->id = i + 1; | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 			initialize_task_tls(&thread->tls); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (pthread_create(&scheduler->threads[i], NULL, task_scheduler_thread_run, thread) != 0) { | 
					
						
							|  |  |  | 				fprintf(stderr, "TaskScheduler failed to launch thread %d/%d\n", i, num_threads); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	return scheduler; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BLI_task_scheduler_free(TaskScheduler *scheduler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Task *task; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* stop all waiting threads */ | 
					
						
							|  |  |  | 	BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 	scheduler->do_exit = true; | 
					
						
							|  |  |  | 	BLI_condition_notify_all(&scheduler->queue_cond); | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 	pthread_key_delete(scheduler->tls_id_key); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	/* delete threads */ | 
					
						
							|  |  |  | 	if (scheduler->threads) { | 
					
						
							|  |  |  | 		int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (i = 0; i < scheduler->num_threads; i++) { | 
					
						
							|  |  |  | 			if (pthread_join(scheduler->threads[i], NULL) != 0) | 
					
						
							|  |  |  | 				fprintf(stderr, "TaskScheduler failed to join thread %d/%d\n", i, scheduler->num_threads); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		MEM_freeN(scheduler->threads); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Delete task thread data */ | 
					
						
							|  |  |  | 	if (scheduler->task_threads) { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 		for (int i = 0; i < scheduler->num_threads + 1; ++i) { | 
					
						
							|  |  |  | 			TaskThreadLocalStorage *tls = &scheduler->task_threads[i].tls; | 
					
						
							|  |  |  | 			free_task_tls(tls); | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 11:12:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		MEM_freeN(scheduler->task_threads); | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	/* delete leftover tasks */ | 
					
						
							|  |  |  | 	for (task = scheduler->queue.first; task; task = task->next) { | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | 		task_data_free(task, 0); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	BLI_freelistN(&scheduler->queue); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* delete mutex/condition */ | 
					
						
							|  |  |  | 	BLI_mutex_end(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 	BLI_condition_end(&scheduler->queue_cond); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MEM_freeN(scheduler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int BLI_task_scheduler_num_threads(TaskScheduler *scheduler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return scheduler->num_threads + 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void task_scheduler_push(TaskScheduler *scheduler, Task *task, TaskPriority priority) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	task_pool_num_increase(task->pool, 1); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* add task to queue */ | 
					
						
							|  |  |  | 	BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (priority == TASK_PRIORITY_HIGH) | 
					
						
							|  |  |  | 		BLI_addhead(&scheduler->queue, task); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		BLI_addtail(&scheduler->queue, task); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_condition_notify_one(&scheduler->queue_cond); | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | static void task_scheduler_push_all(TaskScheduler *scheduler, | 
					
						
							|  |  |  |                                     TaskPool *pool, | 
					
						
							|  |  |  |                                     Task **tasks, | 
					
						
							|  |  |  |                                     int num_tasks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (num_tasks == 0) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_pool_num_increase(pool, num_tasks); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < num_tasks; i++) { | 
					
						
							|  |  |  | 		BLI_addhead(&scheduler->queue, tasks[i]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_condition_notify_all(&scheduler->queue_cond); | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | static void task_scheduler_clear(TaskScheduler *scheduler, TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Task *task, *nexttask; | 
					
						
							|  |  |  | 	size_t done = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* free all tasks from this pool from the queue */ | 
					
						
							|  |  |  | 	for (task = scheduler->queue.first; task; task = nexttask) { | 
					
						
							|  |  |  | 		nexttask = task->next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (task->pool == pool) { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 			task_data_free(task, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 			BLI_freelinkN(&scheduler->queue, task); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			done++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* notify done */ | 
					
						
							|  |  |  | 	task_pool_num_decrease(pool, done); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Task Pool */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | static TaskPool *task_pool_create_ex(TaskScheduler *scheduler, | 
					
						
							|  |  |  |                                      void *userdata, | 
					
						
							|  |  |  |                                      const bool is_background, | 
					
						
							|  |  |  |                                      const bool is_suspended) | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-09 14:54:24 +02:00
										 |  |  | 	TaskPool *pool = MEM_mallocN(sizeof(TaskPool), "TaskPool"); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  | 	/* Assert we do not try to create a background pool from some parent task - those only work OK from main thread. */ | 
					
						
							|  |  |  | 	if (is_background) { | 
					
						
							|  |  |  | 		const pthread_t thread_id = pthread_self(); | 
					
						
							| 
									
										
										
										
											2015-11-02 19:25:00 +01:00
										 |  |  | 		int i = scheduler->num_threads; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		while (i--) { | 
					
						
							| 
									
										
										
										
											2015-11-02 19:25:00 +01:00
										 |  |  | 			BLI_assert(!pthread_equal(scheduler->threads[i], thread_id)); | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	pool->scheduler = scheduler; | 
					
						
							|  |  |  | 	pool->num = 0; | 
					
						
							|  |  |  | 	pool->do_cancel = false; | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	pool->do_work = false; | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	pool->is_suspended = is_suspended; | 
					
						
							|  |  |  | 	pool->num_suspended = 0; | 
					
						
							|  |  |  | 	pool->suspended_queue.first = pool->suspended_queue.last = NULL; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | 	pool->run_in_background = is_background; | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 	pool->use_local_tls = false; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_init(&pool->num_mutex); | 
					
						
							|  |  |  | 	BLI_condition_init(&pool->num_cond); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pool->userdata = userdata; | 
					
						
							|  |  |  | 	BLI_mutex_init(&pool->user_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	if (BLI_thread_is_main()) { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 		pool->thread_id = 0; | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 		TaskThread *thread = pthread_getspecific(scheduler->tls_id_key); | 
					
						
							| 
									
										
										
										
											2017-03-08 09:41:38 +01:00
										 |  |  | 		if (thread == NULL) { | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 			/* NOTE: Task pool is created from non-main thread which is not
 | 
					
						
							|  |  |  | 			 * managed by the task scheduler. We identify ourselves as thread ID | 
					
						
							|  |  |  | 			 * 0 but we do not use scheduler's TLS storage and use our own | 
					
						
							|  |  |  | 			 * instead to avoid any possible threading conflicts. | 
					
						
							|  |  |  | 			 */ | 
					
						
							| 
									
										
										
										
											2017-03-08 09:41:38 +01:00
										 |  |  | 			pool->thread_id = 0; | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 			pool->use_local_tls = true; | 
					
						
							| 
									
										
										
										
											2017-04-13 13:32:39 +02:00
										 |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  | 			pool->creator_thread_id = pthread_self(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 			initialize_task_tls(&pool->local_tls); | 
					
						
							| 
									
										
										
										
											2017-03-08 09:41:38 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			pool->thread_id = thread->id; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 	pool->mempool_stats = | 
					
						
							|  |  |  | 	        MEM_callocN(sizeof(*pool->mempool_stats) * (scheduler->num_threads + 1), | 
					
						
							|  |  |  | 	                    "per-taskpool mempool stats"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	/* Ensure malloc will go fine from threads,
 | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * This is needed because we could be in main thread here | 
					
						
							| 
									
										
										
										
											2018-01-11 17:55:58 +01:00
										 |  |  | 	 * and malloc could be non-thread safe at this point because | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	 * no other jobs are running. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-02-16 01:13:46 +11:00
										 |  |  | 	BLI_threaded_malloc_begin(); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return pool; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create a normal task pool. | 
					
						
							|  |  |  |  * This means that in single-threaded context, it will not be executed at all until you call | 
					
						
							|  |  |  |  * \a BLI_task_pool_work_and_wait() on it. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TaskPool *BLI_task_pool_create(TaskScheduler *scheduler, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	return task_pool_create_ex(scheduler, userdata, false, false); | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Create a background task pool. | 
					
						
							|  |  |  |  * In multi-threaded context, there is no differences with \a BLI_task_pool_create(), but in single-threaded case | 
					
						
							|  |  |  |  * it is ensured to have at least one worker thread to run on (i.e. you do not have to call | 
					
						
							|  |  |  |  * \a BLI_task_pool_work_and_wait() on it to be sure it will be processed). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note Background pools are non-recursive (that is, you should not create other background pools in tasks assigned | 
					
						
							| 
									
										
										
										
											2015-11-06 05:09:14 +11:00
										 |  |  |  *       to a background pool, they could end never being executed, since the 'fallback' background thread is already | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  |  *       busy with parent task in single-threaded context). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TaskPool *BLI_task_pool_create_background(TaskScheduler *scheduler, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	return task_pool_create_ex(scheduler, userdata, true, false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Similar to BLI_task_pool_create() but does not schedule any tasks for execution | 
					
						
							|  |  |  |  * for until BLI_task_pool_work_and_wait() is called. This helps reducing therading | 
					
						
							|  |  |  |  * overhead when pushing huge amount of small initial tasks from the main thread. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TaskPool *BLI_task_pool_create_suspended(TaskScheduler *scheduler, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return task_pool_create_ex(scheduler, userdata, false, true); | 
					
						
							| 
									
										
										
										
											2015-11-02 16:57:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | void BLI_task_pool_free(TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-03 17:12:03 +01:00
										 |  |  | 	BLI_task_pool_cancel(pool); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_end(&pool->num_mutex); | 
					
						
							|  |  |  | 	BLI_condition_end(&pool->num_cond); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_end(&pool->user_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | #ifdef DEBUG_STATS
 | 
					
						
							|  |  |  | 	printf("Thread ID    Allocated   Reused   Discarded\n"); | 
					
						
							|  |  |  | 	for (int i = 0; i < pool->scheduler->num_threads + 1; ++i) { | 
					
						
							|  |  |  | 		printf("%02d           %05d       %05d    %05d\n", | 
					
						
							|  |  |  | 		       i, | 
					
						
							|  |  |  | 		       pool->mempool_stats[i].num_alloc, | 
					
						
							|  |  |  | 		       pool->mempool_stats[i].num_reuse, | 
					
						
							|  |  |  | 		       pool->mempool_stats[i].num_discard); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MEM_freeN(pool->mempool_stats); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 18:18:33 +02:00
										 |  |  | 	if (pool->use_local_tls) { | 
					
						
							|  |  |  | 		free_task_tls(&pool->local_tls); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	MEM_freeN(pool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 01:13:46 +11:00
										 |  |  | 	BLI_threaded_malloc_end(); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | BLI_INLINE bool task_can_use_local_queues(TaskPool *pool, int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (thread_id != -1 && (thread_id != pool->thread_id || pool->do_work)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | static void task_pool_push( | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  |         TaskPool *pool, TaskRunFunction run, void *taskdata, | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  |         bool free_taskdata, TaskFreeFunction freedata, TaskPriority priority, | 
					
						
							|  |  |  |         int thread_id) | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	/* Allocate task and fill it's properties. */ | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | 	Task *task = task_alloc(pool, thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	task->run = run; | 
					
						
							|  |  |  | 	task->taskdata = taskdata; | 
					
						
							|  |  |  | 	task->free_taskdata = free_taskdata; | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | 	task->freedata = freedata; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	task->pool = pool; | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	/* For suspended pools we put everything yo a global queue first
 | 
					
						
							|  |  |  | 	 * and exit as soon as possible. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * This tasks will be moved to actual execution when pool is | 
					
						
							|  |  |  | 	 * activated by work_and_wait(). | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 	if (pool->is_suspended) { | 
					
						
							|  |  |  | 		BLI_addhead(&pool->suspended_queue, task); | 
					
						
							|  |  |  | 		atomic_fetch_and_add_z(&pool->num_suspended, 1); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	/* Populate to any local queue first, this is cheapest push ever. */ | 
					
						
							|  |  |  | 	if (task_can_use_local_queues(pool, thread_id)) { | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 		ASSERT_THREAD_ID(pool->scheduler, thread_id); | 
					
						
							|  |  |  | 		TaskThreadLocalStorage *tls = get_task_tls(pool, thread_id); | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 		/* Try to push to a local execution queue.
 | 
					
						
							|  |  |  | 		 * These tasks will be picked up next. | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2017-05-31 14:52:45 +02:00
										 |  |  | 		if (tls->num_local_queue < LOCAL_QUEUE_SIZE) { | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 			tls->local_queue[tls->num_local_queue] = task; | 
					
						
							|  |  |  | 			tls->num_local_queue++; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 		/* If we are in the delayed tasks push mode, we push tasks to a
 | 
					
						
							|  |  |  | 		 * temporary local queue first without any locks, and then move them | 
					
						
							|  |  |  | 		 * to global execution queue with a single lock. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (tls->do_delayed_push && tls->num_delayed_queue < DELAYED_QUEUE_SIZE) { | 
					
						
							|  |  |  | 			tls->delayed_queue[tls->num_delayed_queue] = task; | 
					
						
							|  |  |  | 			tls->num_delayed_queue++; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 	/* Do push to a global execution ppol, slowest possible method,
 | 
					
						
							|  |  |  | 	 * causes quite reasonable amount of threading overhead. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	task_scheduler_push(pool->scheduler, task, priority); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | void BLI_task_pool_push_ex( | 
					
						
							|  |  |  |         TaskPool *pool, TaskRunFunction run, void *taskdata, | 
					
						
							|  |  |  |         bool free_taskdata, TaskFreeFunction freedata, TaskPriority priority) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	task_pool_push(pool, run, taskdata, free_taskdata, freedata, priority, -1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 16:52:19 +01:00
										 |  |  | void BLI_task_pool_push( | 
					
						
							|  |  |  |         TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskPriority priority) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BLI_task_pool_push_ex(pool, run, taskdata, free_taskdata, NULL, priority); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 09:55:58 +02:00
										 |  |  | void BLI_task_pool_push_from_thread(TaskPool *pool, TaskRunFunction run, | 
					
						
							|  |  |  |         void *taskdata, bool free_taskdata, TaskPriority priority, int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	task_pool_push(pool, run, taskdata, free_taskdata, NULL, priority, thread_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | void BLI_task_pool_work_and_wait(TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	TaskThreadLocalStorage *tls = get_task_tls(pool, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	TaskScheduler *scheduler = pool->scheduler; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 02:40:04 +11:00
										 |  |  | 	if (atomic_fetch_and_and_uint8((uint8_t *)&pool->is_suspended, 0)) { | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 		if (pool->num_suspended) { | 
					
						
							|  |  |  | 			task_pool_num_increase(pool, pool->num_suspended); | 
					
						
							|  |  |  | 			BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			BLI_movelisttolist(&scheduler->queue, &pool->suspended_queue); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			BLI_condition_notify_all(&scheduler->queue_cond); | 
					
						
							|  |  |  | 			BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 	pool->do_work = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT_THREAD_ID(pool->scheduler, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 	BLI_mutex_lock(&pool->num_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (pool->num != 0) { | 
					
						
							|  |  |  | 		Task *task, *work_task = NULL; | 
					
						
							|  |  |  | 		bool found_task = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BLI_mutex_unlock(&pool->num_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BLI_mutex_lock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* find task from this pool. if we get a task from another pool,
 | 
					
						
							|  |  |  | 		 * we can get into deadlock */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 10:48:13 +01:00
										 |  |  | 		for (task = scheduler->queue.first; task; task = task->next) { | 
					
						
							|  |  |  | 			if (task->pool == pool) { | 
					
						
							|  |  |  | 				work_task = task; | 
					
						
							|  |  |  | 				found_task = true; | 
					
						
							|  |  |  | 				BLI_remlink(&scheduler->queue, task); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BLI_mutex_unlock(&scheduler->queue_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* if found task, do it, otherwise wait until other tasks are done */ | 
					
						
							|  |  |  | 		if (found_task) { | 
					
						
							|  |  |  | 			/* run task */ | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 			BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 			work_task->run(pool, work_task->taskdata, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | 			BLI_assert(!tls->do_delayed_push); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			/* delete task */ | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 			task_free(pool, task, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 15:40:05 +01:00
										 |  |  | 			/* Handle all tasks from local queue. */ | 
					
						
							|  |  |  | 			handle_local_queue(tls, pool->thread_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | 			/* notify pool task was done */ | 
					
						
							|  |  |  | 			task_pool_num_decrease(pool, 1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BLI_mutex_lock(&pool->num_mutex); | 
					
						
							|  |  |  | 		if (pool->num == 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!found_task) | 
					
						
							|  |  |  | 			BLI_condition_wait(&pool->num_cond, &pool->num_mutex); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&pool->num_mutex); | 
					
						
							| 
									
										
										
										
											2017-03-07 17:29:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	handle_local_queue(tls, pool->thread_id); | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BLI_task_pool_cancel(TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pool->do_cancel = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_scheduler_clear(pool->scheduler, pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* wait until all entries are cleared */ | 
					
						
							|  |  |  | 	BLI_mutex_lock(&pool->num_mutex); | 
					
						
							|  |  |  | 	while (pool->num) | 
					
						
							|  |  |  | 		BLI_condition_wait(&pool->num_cond, &pool->num_mutex); | 
					
						
							|  |  |  | 	BLI_mutex_unlock(&pool->num_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pool->do_cancel = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-26 01:06:19 +00:00
										 |  |  | bool BLI_task_pool_canceled(TaskPool *pool) | 
					
						
							| 
									
										
										
										
											2013-10-12 14:08:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return pool->do_cancel; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *BLI_task_pool_userdata(TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return pool->userdata; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ThreadMutex *BLI_task_pool_user_mutex(TaskPool *pool) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return &pool->user_mutex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 15:24:09 +02:00
										 |  |  | void BLI_task_pool_delayed_push_begin(TaskPool *pool, int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (task_can_use_local_queues(pool, thread_id)) { | 
					
						
							|  |  |  | 		ASSERT_THREAD_ID(pool->scheduler, thread_id); | 
					
						
							|  |  |  | 		TaskThreadLocalStorage *tls = get_task_tls(pool, thread_id); | 
					
						
							|  |  |  | 		tls->do_delayed_push = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BLI_task_pool_delayed_push_end(TaskPool *pool, int thread_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (task_can_use_local_queues(pool, thread_id)) { | 
					
						
							|  |  |  | 		ASSERT_THREAD_ID(pool->scheduler, thread_id); | 
					
						
							|  |  |  | 		TaskThreadLocalStorage *tls = get_task_tls(pool, thread_id); | 
					
						
							|  |  |  | 		BLI_assert(tls->do_delayed_push); | 
					
						
							|  |  |  | 		task_scheduler_push_all(pool->scheduler, | 
					
						
							|  |  |  | 		                        pool, | 
					
						
							|  |  |  | 		                        tls->delayed_queue, | 
					
						
							|  |  |  | 		                        tls->num_delayed_queue); | 
					
						
							|  |  |  | 		tls->do_delayed_push = false; | 
					
						
							|  |  |  | 		tls->num_delayed_queue = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | /* Parallel range routines */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Main functions: | 
					
						
							|  |  |  |  * - #BLI_task_parallel_range | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  |  * - #BLI_task_parallel_listbase (#ListBase - double linked list) | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * TODO: | 
					
						
							|  |  |  |  * - #BLI_task_parallel_foreach_link (#Link - single linked list) | 
					
						
							|  |  |  |  * - #BLI_task_parallel_foreach_ghash/gset (#GHash/#GSet - hash & set) | 
					
						
							|  |  |  |  * - #BLI_task_parallel_foreach_mempool (#BLI_mempool - iterate over mempools) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 11:01:59 +01:00
										 |  |  | /* Allows to avoid using malloc for userdata_chunk in tasks, when small enough. */ | 
					
						
							|  |  |  | #define MALLOCA(_size) ((_size) <= 8192) ? alloca((_size)) : MEM_mallocN((_size), __func__)
 | 
					
						
							|  |  |  | #define MALLOCA_FREE(_mem, _size) if (((_mem) != NULL) && ((_size) > 8192)) MEM_freeN((_mem))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | typedef struct ParallelRangeState { | 
					
						
							|  |  |  | 	int start, stop; | 
					
						
							|  |  |  | 	void *userdata; | 
					
						
							| 
									
										
										
										
											2016-01-16 15:59:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 	TaskParallelRangeFunc func; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int iter; | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	int chunk_size; | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | } ParallelRangeState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BLI_INLINE bool parallel_range_next_iter_get( | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  |         ParallelRangeState * __restrict state, | 
					
						
							|  |  |  |         int * __restrict iter, int * __restrict count) | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-23 16:14:53 +01:00
										 |  |  | 	int previter = atomic_fetch_and_add_int32(&state->iter, state->chunk_size); | 
					
						
							| 
									
										
										
										
											2016-05-16 15:57:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 15:50:12 +03:00
										 |  |  | 	*iter = previter; | 
					
						
							| 
									
										
										
										
											2016-05-16 15:57:19 +02:00
										 |  |  | 	*count = max_ii(0, min_ii(state->chunk_size, state->stop - previter)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (previter < state->stop); | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void parallel_range_func( | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  |         TaskPool * __restrict pool, | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  |         void *userdata_chunk, | 
					
						
							| 
									
										
										
										
											2018-01-05 16:33:13 +01:00
										 |  |  |         int thread_id) | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	ParallelRangeState * __restrict state = BLI_task_pool_userdata(pool); | 
					
						
							| 
									
										
										
										
											2018-01-05 16:33:13 +01:00
										 |  |  | 	ParallelRangeTLS tls = { | 
					
						
							|  |  |  | 		.thread_id = thread_id, | 
					
						
							|  |  |  | 		.userdata_chunk = userdata_chunk, | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	int iter, count; | 
					
						
							|  |  |  | 	while (parallel_range_next_iter_get(state, &iter, &count)) { | 
					
						
							| 
									
										
										
										
											2018-01-05 16:33:13 +01:00
										 |  |  | 		for (int i = 0; i < count; ++i) { | 
					
						
							|  |  |  | 			state->func(state->userdata, iter + i, &tls); | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 12:27:43 +01:00
										 |  |  | static void palallel_range_single_thread(const int start, int const stop, | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  |                                          void *userdata, | 
					
						
							|  |  |  |                                          TaskParallelRangeFunc func, | 
					
						
							|  |  |  |                                          const ParallelRangeSettings *settings) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	void *userdata_chunk = settings->userdata_chunk; | 
					
						
							|  |  |  | 	const size_t userdata_chunk_size = settings->userdata_chunk_size; | 
					
						
							|  |  |  | 	void *userdata_chunk_local = NULL; | 
					
						
							|  |  |  | 	const bool use_userdata_chunk = (userdata_chunk_size != 0) && (userdata_chunk != NULL); | 
					
						
							|  |  |  | 	if (use_userdata_chunk) { | 
					
						
							|  |  |  | 		userdata_chunk_local = MALLOCA(userdata_chunk_size); | 
					
						
							|  |  |  | 		memcpy(userdata_chunk_local, userdata_chunk, userdata_chunk_size); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ParallelRangeTLS tls = { | 
					
						
							|  |  |  | 		.thread_id = 0, | 
					
						
							|  |  |  | 		.userdata_chunk = userdata_chunk_local, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	for (int i = start; i < stop; ++i) { | 
					
						
							|  |  |  | 		func(userdata, i, &tls); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (settings->func_finalize != NULL) { | 
					
						
							|  |  |  | 		settings->func_finalize(userdata, userdata_chunk_local); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MALLOCA_FREE(userdata_chunk_local, userdata_chunk_size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 11:01:59 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * This function allows to parallelized for loops in a similar way to OpenMP's 'parallel for' statement. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  |  * See public API doc of ParallelRangeSettings for description of all settings. | 
					
						
							| 
									
										
										
										
											2015-11-25 11:01:59 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-01-09 10:47:01 +01:00
										 |  |  | void BLI_task_parallel_range(const int start, const int stop, | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  |                              void *userdata, | 
					
						
							|  |  |  |                              TaskParallelRangeFunc func, | 
					
						
							|  |  |  |                              const ParallelRangeSettings *settings) | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	TaskScheduler *task_scheduler; | 
					
						
							|  |  |  | 	TaskPool *task_pool; | 
					
						
							|  |  |  | 	ParallelRangeState state; | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	int i, num_threads, num_tasks; | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 	void *userdata_chunk = settings->userdata_chunk; | 
					
						
							|  |  |  | 	const size_t userdata_chunk_size = settings->userdata_chunk_size; | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 	void *userdata_chunk_local = NULL; | 
					
						
							|  |  |  | 	void *userdata_chunk_array = NULL; | 
					
						
							| 
									
										
										
										
											2018-01-05 16:33:13 +01:00
										 |  |  | 	const bool use_userdata_chunk = (userdata_chunk_size != 0) && (userdata_chunk != NULL); | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-04 17:23:54 +01:00
										 |  |  | 	if (start == stop) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-16 15:59:37 +01:00
										 |  |  | 	BLI_assert(start < stop); | 
					
						
							|  |  |  | 	if (userdata_chunk_size != 0) { | 
					
						
							|  |  |  | 		BLI_assert(userdata_chunk != NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 10:38:14 +02:00
										 |  |  | 	/* If it's not enough data to be crunched, don't bother with tasks at all,
 | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 	 * do everything from the main thread. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 	if (!settings->use_threading) { | 
					
						
							|  |  |  | 		palallel_range_single_thread(start, stop, | 
					
						
							|  |  |  | 		                             userdata, | 
					
						
							|  |  |  | 		                             func, | 
					
						
							|  |  |  | 		                             settings); | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_scheduler = BLI_task_scheduler_get(); | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	num_threads = BLI_task_scheduler_num_threads(task_scheduler); | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* The idea here is to prevent creating task for each of the loop iterations
 | 
					
						
							|  |  |  | 	 * and instead have tasks which are evenly distributed across CPU cores and | 
					
						
							| 
									
										
										
										
											2014-10-23 10:38:14 +02:00
										 |  |  | 	 * pull next iter to be crunched using the queue. | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-01-08 12:13:56 +01:00
										 |  |  | 	num_tasks = num_threads + 2; | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state.start = start; | 
					
						
							|  |  |  | 	state.stop = stop; | 
					
						
							|  |  |  | 	state.userdata = userdata; | 
					
						
							|  |  |  | 	state.func = func; | 
					
						
							|  |  |  | 	state.iter = start; | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 	switch (settings->scheduling_mode) { | 
					
						
							|  |  |  | 		case TASK_SCHEDULING_STATIC: | 
					
						
							| 
									
										
										
										
											2018-01-08 12:08:18 +01:00
										 |  |  | 			state.chunk_size = max_ii( | 
					
						
							|  |  |  | 			        settings->min_iter_per_thread, | 
					
						
							|  |  |  | 			        (stop - start) / (num_tasks)); | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case TASK_SCHEDULING_DYNAMIC: | 
					
						
							| 
									
										
										
										
											2018-01-08 12:08:18 +01:00
										 |  |  | 			/* TODO(sergey): Make it configurable from min_iter_per_thread. */ | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 			state.chunk_size = 32; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-09 18:07:34 +01:00
										 |  |  | 	num_tasks = min_ii(num_tasks, | 
					
						
							|  |  |  | 	                   max_ii(1, (stop - start) / state.chunk_size)); | 
					
						
							| 
									
										
										
										
											2017-12-22 16:37:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-09 18:10:47 +01:00
										 |  |  | 	if (num_tasks == 1) { | 
					
						
							|  |  |  | 		palallel_range_single_thread(start, stop, | 
					
						
							|  |  |  | 		                             userdata, | 
					
						
							|  |  |  | 		                             func, | 
					
						
							|  |  |  | 		                             settings); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_pool = BLI_task_pool_create_suspended(task_scheduler, &state); | 
					
						
							| 
									
										
										
										
											2018-01-08 12:08:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 16:37:25 +01:00
										 |  |  | 	/* NOTE: This way we are adding a memory barrier and ensure all worker
 | 
					
						
							|  |  |  | 	 * threads can read and modify the value, without any locks. */ | 
					
						
							| 
									
										
										
										
											2017-11-23 16:14:53 +01:00
										 |  |  | 	atomic_fetch_and_add_int32(&state.iter, 0); | 
					
						
							| 
									
										
										
										
											2015-12-28 00:28:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 	if (use_userdata_chunk) { | 
					
						
							| 
									
										
										
										
											2017-06-12 13:35:00 +10:00
										 |  |  | 		userdata_chunk_array = MALLOCA(userdata_chunk_size * num_tasks); | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 18:24:08 +01:00
										 |  |  | 	for (i = 0; i < num_tasks; i++) { | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 		if (use_userdata_chunk) { | 
					
						
							|  |  |  | 			userdata_chunk_local = (char *)userdata_chunk_array + (userdata_chunk_size * i); | 
					
						
							|  |  |  | 			memcpy(userdata_chunk_local, userdata_chunk, userdata_chunk_size); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Use this pool's pre-allocated tasks. */ | 
					
						
							| 
									
										
										
										
											2016-05-16 16:46:54 +02:00
										 |  |  | 		BLI_task_pool_push_from_thread(task_pool, | 
					
						
							|  |  |  | 		                               parallel_range_func, | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 		                               userdata_chunk_local, false, | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 		                               TASK_PRIORITY_HIGH, | 
					
						
							|  |  |  | 		                               task_pool->thread_id); | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_task_pool_work_and_wait(task_pool); | 
					
						
							|  |  |  | 	BLI_task_pool_free(task_pool); | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (use_userdata_chunk) { | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 		if (settings->func_finalize != NULL) { | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 			for (i = 0; i < num_tasks; i++) { | 
					
						
							|  |  |  | 				userdata_chunk_local = (char *)userdata_chunk_array + (userdata_chunk_size * i); | 
					
						
							| 
									
										
										
										
											2018-01-08 12:07:09 +01:00
										 |  |  | 				settings->func_finalize(userdata, userdata_chunk_local); | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-19 06:25:54 +10:00
										 |  |  | 		MALLOCA_FREE(userdata_chunk_array, userdata_chunk_size * num_tasks); | 
					
						
							| 
									
										
										
										
											2016-05-16 17:15:18 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-22 11:56:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 11:01:59 +01:00
										 |  |  | #undef MALLOCA
 | 
					
						
							|  |  |  | #undef MALLOCA_FREE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  | typedef struct ParallelListbaseState { | 
					
						
							|  |  |  | 	void *userdata; | 
					
						
							|  |  |  | 	TaskParallelListbaseFunc func; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int chunk_size; | 
					
						
							|  |  |  | 	int index; | 
					
						
							|  |  |  | 	Link *link; | 
					
						
							|  |  |  | 	SpinLock lock; | 
					
						
							|  |  |  | } ParallelListState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BLI_INLINE Link *parallel_listbase_next_iter_get( | 
					
						
							|  |  |  |         ParallelListState * __restrict state, | 
					
						
							|  |  |  |         int * __restrict index, | 
					
						
							|  |  |  |         int * __restrict count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int task_count = 0; | 
					
						
							|  |  |  | 	BLI_spin_lock(&state->lock); | 
					
						
							|  |  |  | 	Link *result = state->link; | 
					
						
							|  |  |  | 	if (LIKELY(result != NULL)) { | 
					
						
							|  |  |  | 		*index = state->index; | 
					
						
							|  |  |  | 		while (state->link != NULL && task_count < state->chunk_size) { | 
					
						
							|  |  |  | 			++task_count; | 
					
						
							|  |  |  | 			state->link = state->link->next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		state->index += task_count; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	BLI_spin_unlock(&state->lock); | 
					
						
							|  |  |  | 	*count = task_count; | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void parallel_listbase_func( | 
					
						
							|  |  |  |         TaskPool * __restrict pool, | 
					
						
							|  |  |  |         void *UNUSED(taskdata), | 
					
						
							|  |  |  |         int UNUSED(threadid)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ParallelListState * __restrict state = BLI_task_pool_userdata(pool); | 
					
						
							|  |  |  | 	Link *link; | 
					
						
							|  |  |  | 	int index, count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ((link = parallel_listbase_next_iter_get(state, &index, &count)) != NULL) { | 
					
						
							|  |  |  | 		for (int i = 0; i < count; ++i) { | 
					
						
							|  |  |  | 			state->func(state->userdata, link, index + i); | 
					
						
							|  |  |  | 			link = link->next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * This function allows to parallelize for loops over ListBase items. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param listbase The double linked list to loop over. | 
					
						
							|  |  |  |  * \param userdata Common userdata passed to all instances of \a func. | 
					
						
							|  |  |  |  * \param func Callback function. | 
					
						
							|  |  |  |  * \param use_threading If \a true, actually split-execute loop in threads, else just do a sequential forloop | 
					
						
							|  |  |  |  *                      (allows caller to use any kind of test to switch on parallelization or not). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note There is no static scheduling here, since it would need another full loop over items to count them... | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void BLI_task_parallel_listbase( | 
					
						
							|  |  |  |         struct ListBase *listbase, | 
					
						
							|  |  |  |         void *userdata, | 
					
						
							|  |  |  |         TaskParallelListbaseFunc func, | 
					
						
							|  |  |  |         const bool use_threading) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	TaskScheduler *task_scheduler; | 
					
						
							|  |  |  | 	TaskPool *task_pool; | 
					
						
							|  |  |  | 	ParallelListState state; | 
					
						
							|  |  |  | 	int i, num_threads, num_tasks; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BLI_listbase_is_empty(listbase)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!use_threading) { | 
					
						
							|  |  |  | 		i = 0; | 
					
						
							|  |  |  | 		for (Link *link = listbase->first; link != NULL; link = link->next, ++i) { | 
					
						
							|  |  |  | 			func(userdata, link, i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_scheduler = BLI_task_scheduler_get(); | 
					
						
							| 
									
										
										
										
											2017-12-22 12:25:11 +01:00
										 |  |  | 	task_pool = BLI_task_pool_create_suspended(task_scheduler, &state); | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  | 	num_threads = BLI_task_scheduler_num_threads(task_scheduler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* The idea here is to prevent creating task for each of the loop iterations
 | 
					
						
							|  |  |  | 	 * and instead have tasks which are evenly distributed across CPU cores and | 
					
						
							|  |  |  | 	 * pull next iter to be crunched using the queue. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-01-08 12:13:56 +01:00
										 |  |  | 	num_tasks = num_threads + 2; | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state.index = 0; | 
					
						
							|  |  |  | 	state.link = listbase->first; | 
					
						
							|  |  |  | 	state.userdata = userdata; | 
					
						
							|  |  |  | 	state.func = func; | 
					
						
							|  |  |  | 	state.chunk_size = 32; | 
					
						
							|  |  |  | 	BLI_spin_init(&state.lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < num_tasks; i++) { | 
					
						
							|  |  |  | 		/* Use this pool's pre-allocated tasks. */ | 
					
						
							|  |  |  | 		BLI_task_pool_push_from_thread(task_pool, | 
					
						
							|  |  |  | 		                               parallel_listbase_func, | 
					
						
							|  |  |  | 		                               NULL, false, | 
					
						
							| 
									
										
										
										
											2017-03-06 11:21:50 +01:00
										 |  |  | 		                               TASK_PRIORITY_HIGH, | 
					
						
							|  |  |  | 		                               task_pool->thread_id); | 
					
						
							| 
									
										
										
										
											2016-05-13 11:03:04 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_task_pool_work_and_wait(task_pool); | 
					
						
							|  |  |  | 	BLI_task_pool_free(task_pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_spin_end(&state.lock); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct ParallelMempoolState { | 
					
						
							|  |  |  | 	void *userdata; | 
					
						
							|  |  |  | 	TaskParallelMempoolFunc func; | 
					
						
							|  |  |  | } ParallelMempoolState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void parallel_mempool_func( | 
					
						
							|  |  |  |         TaskPool * __restrict pool, | 
					
						
							|  |  |  |         void *taskdata, | 
					
						
							|  |  |  |         int UNUSED(threadid)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ParallelMempoolState * __restrict state = BLI_task_pool_userdata(pool); | 
					
						
							|  |  |  | 	BLI_mempool_iter *iter = taskdata; | 
					
						
							|  |  |  | 	MempoolIterData *item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ((item = BLI_mempool_iterstep(iter)) != NULL) { | 
					
						
							|  |  |  | 		state->func(state->userdata, item); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * This function allows to parallelize for loops over Mempool items. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-03-14 01:58:46 +11:00
										 |  |  |  * \param mempool: The iterable BLI_mempool to loop over. | 
					
						
							|  |  |  |  * \param userdata: Common userdata passed to all instances of \a func. | 
					
						
							|  |  |  |  * \param func: Callback function. | 
					
						
							|  |  |  |  * \param use_threading: If \a true, actually split-execute loop in threads, else just do a sequential for loop | 
					
						
							|  |  |  |  * (allows caller to use any kind of test to switch on parallelization or not). | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \note There is no static scheduling here. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void BLI_task_parallel_mempool( | 
					
						
							|  |  |  |         BLI_mempool *mempool, | 
					
						
							|  |  |  |         void *userdata, | 
					
						
							|  |  |  |         TaskParallelMempoolFunc func, | 
					
						
							|  |  |  |         const bool use_threading) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	TaskScheduler *task_scheduler; | 
					
						
							|  |  |  | 	TaskPool *task_pool; | 
					
						
							|  |  |  | 	ParallelMempoolState state; | 
					
						
							|  |  |  | 	int i, num_threads, num_tasks; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 23:36:11 +11:00
										 |  |  | 	if (BLI_mempool_len(mempool) == 0) { | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!use_threading) { | 
					
						
							|  |  |  | 		BLI_mempool_iter iter; | 
					
						
							|  |  |  | 		BLI_mempool_iternew(mempool, &iter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (void *item = BLI_mempool_iterstep(&iter); item != NULL; item = BLI_mempool_iterstep(&iter)) { | 
					
						
							|  |  |  | 			func(userdata, item); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	task_scheduler = BLI_task_scheduler_get(); | 
					
						
							| 
									
										
										
										
											2017-12-22 12:25:11 +01:00
										 |  |  | 	task_pool = BLI_task_pool_create_suspended(task_scheduler, &state); | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  | 	num_threads = BLI_task_scheduler_num_threads(task_scheduler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* The idea here is to prevent creating task for each of the loop iterations
 | 
					
						
							|  |  |  | 	 * and instead have tasks which are evenly distributed across CPU cores and | 
					
						
							|  |  |  | 	 * pull next item to be crunched using the threaded-aware BLI_mempool_iter. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-01-08 12:13:56 +01:00
										 |  |  | 	num_tasks = num_threads + 2; | 
					
						
							| 
									
										
										
										
											2017-11-23 21:14:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state.userdata = userdata; | 
					
						
							|  |  |  | 	state.func = func; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mempool_iter *mempool_iterators = BLI_mempool_iter_threadsafe_create(mempool, (size_t)num_tasks); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < num_tasks; i++) { | 
					
						
							|  |  |  | 		/* Use this pool's pre-allocated tasks. */ | 
					
						
							|  |  |  | 		BLI_task_pool_push_from_thread(task_pool, | 
					
						
							|  |  |  | 		                               parallel_mempool_func, | 
					
						
							|  |  |  | 		                               &mempool_iterators[i], false, | 
					
						
							|  |  |  | 		                               TASK_PRIORITY_HIGH, | 
					
						
							|  |  |  | 		                               task_pool->thread_id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_task_pool_work_and_wait(task_pool); | 
					
						
							|  |  |  | 	BLI_task_pool_free(task_pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_mempool_iter_threadsafe_free(mempool_iterators); | 
					
						
							|  |  |  | } |