void ThreadPoolPrivate::workLoop() { qi::os::setCurrentThreadName("tp-idle"); while (true) { boost::function<void(void)> task; { /* -- Locked Section -- */ boost::mutex::scoped_lock lock(_tasksMutex); if (_tasks.empty()) { /* Should the thread be destroyed ? */ if ((((*_workers - *_activeWorkers) > _maxIdleWorkers) && *_workers > _minWorkers) || (*_workers > _maxWorkers)) { break; } /* Notify user that all tasks are done */ if (*_activeWorkers == 0) _userCondition.notify_all(); /* Wait for a task to be scheduled */ _tasksCondition.wait(lock); } else /* Retrieve the task */ { task = _tasks.front(); _tasks.pop(); } } /* -- End Locked Section -- */ if (task) { qi::os::setCurrentThreadName("tp-worker"); ++_activeWorkers; task(); --_activeWorkers; qi::os::setCurrentThreadName("tp-idle"); } } /* Thread destruction */ --_workers; { boost::mutex::scoped_lock lock(_terminatedThreadsMutex); _terminatedThreads.push(boost::this_thread::get_id()); } notifyManager(); }
bool ThreadPoolPrivate::schedule(const boost::function<void(void)>& f) { { boost::mutex::scoped_lock lock(_tasksMutex); _tasks.push(f); } _tasksCondition.notify_one(); /* Do we need more threads ? */ notifyManager(); return true; }
void ProgramManager::startProgramChange(bool isr){ if(isr) notifyManagerFromISR(STOP_PROGRAM_NOTIFICATION|PROGRAM_CHANGE_NOTIFICATION); else notifyManager(STOP_PROGRAM_NOTIFICATION|PROGRAM_CHANGE_NOTIFICATION); }
/* exit and restart program */ void ProgramManager::resetProgram(bool isr){ if(isr) notifyManagerFromISR(STOP_PROGRAM_NOTIFICATION|START_PROGRAM_NOTIFICATION); else notifyManager(STOP_PROGRAM_NOTIFICATION|START_PROGRAM_NOTIFICATION); }