2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef __UTIL_TASK_H__
20 #define __UTIL_TASK_H__
22 #include "util_list.h"
23 #include "util_thread.h"
24 #include "util_vector.h"
32 typedef boost::function<void(void)> TaskRunFunction;
36 * Base class for tasks to be executed in threads. */
42 Task(const TaskRunFunction& run_) : run(run_) {}
51 * Pool of tasks that will be executed by the central TaskScheduler.For each
52 * pool, we can wait for all tasks to be done, or cancel them before they are
55 * The run callback that actually executes the task may be create like this:
56 * function_bind(&MyClass::task_execute, this, _1, _2) */
64 void push(Task *task, bool front = false);
65 void push(const TaskRunFunction& run, bool front = false);
67 void wait_work(); /* work and wait until all tasks are done */
68 void cancel(); /* cancel all tasks, keep worker threads running */
69 void stop(); /* stop all worker threads */
71 bool cancelled(); /* for worker threads, test if cancelled */
74 friend class TaskScheduler;
76 void num_decrease(int done);
79 thread_mutex num_mutex;
80 thread_condition_variable num_cond;
83 volatile bool do_cancel;
88 * Central scheduler that holds running threads ready to execute tasks. A singe
89 * queue holds the task from all pools. */
94 static void init(int num_threads = 0);
97 /* number of threads that can work on tasks, main thread counts too */
98 static int num_threads() { return threads.size() + 1; }
100 /* test if any session is using the scheduler */
101 static bool active() { return users != 0; }
104 friend class TaskPool;
111 static thread_mutex mutex;
113 static vector<thread*> threads;
114 static vector<int> thread_level;
115 static volatile bool do_exit;
117 static list<Entry> queue;
118 static thread_mutex queue_mutex;
119 static thread_condition_variable queue_cond;
121 static void thread_run(int thread_id);
122 static bool thread_wait_pop(Entry& entry);
124 static void push(Entry& entry, bool front);
125 static void clear(TaskPool *pool);