bool Worker::addProcessor(int id, Runnable *processor) { bool ret = false; std::map<int, Runnable*> runnables; Runnable* current; std::lock_guard<std::mutex> guard(mtx); while (!processors.empty()){ current = processors.top(); processors.pop(); runnables[current->getId()] = current; } if (runnables.count(id) == 0) { processor->setId(id); runnables[id] = processor; ret = true; } for (auto it : runnables){ processors.push(it.second); } return ret; }
bool Worker::removeProcessor(int id) { bool ret = false; std::map<int, Runnable*> runnables; Runnable* current; std::lock_guard<std::mutex> guard(mtx); while (!processors.empty()){ current = processors.top(); processors.pop(); runnables[current->getId()] = current; } while (runnables.count(id) > 0) { runnables.erase(id); ret = true; } for (auto it : runnables){ processors.push(it.second); } return ret; }
//TODO: avoid void functions void Worker::getState(Jzon::Object &workerNode) { Jzon::Array pList; std::map<int, Runnable*> runnables; Runnable* current; { std::lock_guard<std::mutex> guard(mtx); while (!processors.empty()){ current = processors.top(); processors.pop(); runnables[current->getId()] = current; } for (auto it : runnables){ processors.push(it.second); } } for (auto it : runnables) { pList.Add(it.first); } workerNode.Add("type", utils::getWorkerTypeAsString(type)); workerNode.Add("processors", pList); }