76{
return sysconf(_SC_NPROCESSORS_ONLN);}
91 wait_to_execute_a_task(queue::priv*);
105 bool bring_workers_down;
110 pthread_mutex_t tasks_todo_mutex;
115 pthread_cond_t tasks_todo_cond;
118 pthread_mutex_t tasks_done_mutex;
121 pthread_cond_t tasks_done_cond;
123 std::queue<task_sptr> tasks_todo;
125 std::vector<task_sptr> tasks_done;
131 static task_done_notify default_notify;
135 task_done_notify& notify;
137 std::vector<worker> workers;
148 task_done_notify& n = default_notify)
149 : bring_workers_down(),
150 num_workers(nb_workers),
163 for (
unsigned i = 0; i < num_workers; ++i)
168 (
void*(*)(
void*))&worker::wait_to_execute_a_task,
170 workers.push_back(w);
186 schedule_task(
const task_sptr& t)
188 if (workers.empty() || !t)
191 pthread_mutex_lock(&tasks_todo_mutex);
193 pthread_mutex_unlock(&tasks_todo_mutex);
194 pthread_cond_signal(&tasks_todo_cond);
209 for (tasks_type::const_iterator t = tasks.begin(); t != tasks.end(); ++t)
210 is_ok &= schedule_task(*t);
229 do_bring_workers_down()
235 pthread_mutex_lock(&tasks_todo_mutex);
236 while (!tasks_todo.empty())
237 pthread_cond_wait(&tasks_done_cond, &tasks_todo_mutex);
239 bring_workers_down =
true;
240 pthread_mutex_unlock(&tasks_todo_mutex);
244 ABG_ASSERT(pthread_cond_broadcast(&tasks_todo_cond) == 0);
246 for (std::vector<worker>::const_iterator i = workers.begin();
255 {do_bring_workers_down();}
260queue::task_done_notify queue::priv::default_notify;
276 : p_(new priv(number_of_workers))
293 : p_(new priv(number_of_workers, notifier))
302{
return p_->tasks_todo.size();}
316{
return p_->schedule_task(t);}
328{
return p_->schedule_tasks(tasks);}
341{p_->do_bring_workers_down();}
346std::vector<task_sptr>&
348{
return p_->tasks_done;}
376worker::wait_to_execute_a_task(queue::priv* p)
380 pthread_mutex_lock(&p->tasks_todo_mutex);
383 while (p->tasks_todo.empty() && !p->bring_workers_down)
384 pthread_cond_wait(&p->tasks_todo_cond, &p->tasks_todo_mutex);
389 if (!p->tasks_todo.empty())
391 t = p->tasks_todo.front();
394 pthread_mutex_unlock(&p->tasks_todo_mutex);
410 pthread_mutex_lock(&p->tasks_done_mutex);
411 p->tasks_done.push_back(t);
413 pthread_mutex_unlock(&p->tasks_done_mutex);
414 pthread_cond_signal(&p->tasks_done_cond);
418 bool drop_out =
false;
419 pthread_mutex_lock(&p->tasks_todo_mutex);
420 drop_out = p->bring_workers_down;
421 pthread_mutex_unlock(&p->tasks_todo_mutex);