diff options
Diffstat (limited to 'intern/cycles/util/util_task.h')
-rw-r--r-- | intern/cycles/util/util_task.h | 102 |
1 files changed, 17 insertions, 85 deletions
diff --git a/intern/cycles/util/util_task.h b/intern/cycles/util/util_task.h index fd30a33d8ef..a56ca62f62c 100644 --- a/intern/cycles/util/util_task.h +++ b/intern/cycles/util/util_task.h @@ -19,48 +19,16 @@ #include "util/util_list.h" #include "util/util_string.h" +#include "util/util_tbb.h" #include "util/util_thread.h" #include "util/util_vector.h" CCL_NAMESPACE_BEGIN -class Task; class TaskPool; class TaskScheduler; -/* Notes on Thread ID - * - * Thread ID argument reports the 0-based ID of a working thread from which - * the run() callback is being invoked. Thread ID of 0 denotes the thread from - * which wait_work() was called. - * - * DO NOT use this ID to control execution flaw, use it only for things like - * emulating TLS which does not affect on scheduling. Don't use this ID to make - * any decisions. - * - * It is to be noted here that dedicated task pool will always report thread ID - * of 0. - */ - -typedef function<void(int thread_id)> TaskRunFunction; - -/* Task - * - * Base class for tasks to be executed in threads. */ - -class Task { - public: - Task(){}; - explicit Task(const TaskRunFunction &run_) : run(run_) - { - } - - virtual ~Task() - { - } - - TaskRunFunction run; -}; +typedef function<void(void)> TaskRunFunction; /* Task Pool * @@ -68,8 +36,7 @@ class Task { * pool, we can wait for all tasks to be done, or cancel them before they are * done. * - * The run callback that actually executes the task may be created like this: - * function_bind(&MyClass::task_execute, this, _1, _2) */ + * TaskRunFunction may be created with std::bind or lambda expressions. */ class TaskPool { public: @@ -89,27 +56,15 @@ class TaskPool { TaskPool(); ~TaskPool(); - void push(Task *task, bool front = false); - void push(const TaskRunFunction &run, bool front = false); + void push(TaskRunFunction &&task); void wait_work(Summary *stats = NULL); /* work and wait until all tasks are done */ - void cancel(); /* cancel all tasks, keep worker threads running */ - void stop(); /* stop all worker threads */ - bool finished(); /* check if all work has been completed */ + void cancel(); /* cancel all tasks and wait until they are no longer executing */ bool canceled(); /* for worker threads, test if canceled */ protected: - friend class TaskScheduler; - - void num_decrease(int done); - void num_increase(); - - thread_mutex num_mutex; - thread_condition_variable num_cond; - - int num; - bool do_cancel; + tbb::task_group tbb_group; /* ** Statistics ** */ @@ -131,40 +86,19 @@ class TaskScheduler { static void exit(); static void free_memory(); - /* number of threads that can work on task */ - static int num_threads() - { - return threads.size(); - } - - /* test if any session is using the scheduler */ - static bool active() - { - return users != 0; - } + /* Approximate number of threads that will work on task, which may be lower + * or higher than the actual number of threads. Use as little as possible and + * leave splitting up tasks to the scheduler.. */ + static int num_threads(); protected: - friend class TaskPool; - - struct Entry { - Task *task; - TaskPool *pool; - }; - static thread_mutex mutex; static int users; - static vector<thread *> threads; - static bool do_exit; + static int active_num_threads; - static list<Entry> queue; - static thread_mutex queue_mutex; - static thread_condition_variable queue_cond; - - static void thread_run(int thread_id); - static bool thread_wait_pop(Entry &entry); - - static void push(Entry &entry, bool front); - static void clear(TaskPool *pool); +#ifdef WITH_TBB_GLOBAL_CONTROL + static tbb::global_control *global_control; +#endif }; /* Dedicated Task Pool @@ -179,12 +113,10 @@ class DedicatedTaskPool { DedicatedTaskPool(); ~DedicatedTaskPool(); - void push(Task *task, bool front = false); - void push(const TaskRunFunction &run, bool front = false); + void push(TaskRunFunction &&run, bool front = false); void wait(); /* wait until all tasks are done */ void cancel(); /* cancel all tasks, keep worker thread running */ - void stop(); /* stop worker thread */ bool canceled(); /* for worker thread, test if canceled */ @@ -193,14 +125,14 @@ class DedicatedTaskPool { void num_increase(); void thread_run(); - bool thread_wait_pop(Task *&entry); + bool thread_wait_pop(TaskRunFunction &task); void clear(); thread_mutex num_mutex; thread_condition_variable num_cond; - list<Task *> queue; + list<TaskRunFunction> queue; thread_mutex queue_mutex; thread_condition_variable queue_cond; |