Cycles: Report thread ID from worker thread to callbacks

Main use case of this ID will be to emulate TLS which otherwise
would require having some platform-specific implementations which
is not always really optimal.

See notes about the argument in util_task.h.
This commit is contained in:
Sergey Sharybin
2016-04-01 15:23:08 +02:00
parent c6b27dd4fa
commit f318e8322f
2 changed files with 20 additions and 6 deletions

View File

@@ -89,7 +89,7 @@ void TaskPool::wait_work()
/* if found task, do it, otherwise wait until other tasks are done */
if(found_entry) {
/* run task */
work_entry.task->run();
work_entry.task->run(0);
/* delete task */
delete work_entry.task;
@@ -192,7 +192,7 @@ void TaskScheduler::init(int num_threads)
threads.resize(num_threads);
for(size_t i = 0; i < threads.size(); i++)
threads[i] = new thread(function_bind(&TaskScheduler::thread_run, i));
threads[i] = new thread(function_bind(&TaskScheduler::thread_run, i + 1));
}
users++;
@@ -243,7 +243,7 @@ bool TaskScheduler::thread_wait_pop(Entry& entry)
return true;
}
void TaskScheduler::thread_run(int /*thread_id*/)
void TaskScheduler::thread_run(int thread_id)
{
Entry entry;
@@ -252,7 +252,7 @@ void TaskScheduler::thread_run(int /*thread_id*/)
/* keep popping off tasks */
while(thread_wait_pop(entry)) {
/* run task */
entry.task->run();
entry.task->run(thread_id);
/* delete task */
delete entry.task;
@@ -419,7 +419,7 @@ void DedicatedTaskPool::thread_run()
/* keep popping off tasks */
while(thread_wait_pop(task)) {
/* run task */
task->run();
task->run(0);
/* delete task */
delete task;

View File

@@ -27,7 +27,21 @@ class Task;
class TaskPool;
class TaskScheduler;
typedef function<void(void)> TaskRunFunction;
/* 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
*