Skip to content
Snippets Groups Projects
Commit 2f006c57 authored by Guillaume Roguez's avatar Guillaume Roguez Committed by Stepan Salenikovich
Browse files

manager: make addTask method thread safe

The code of Manager::addTask() has a comment
about the fact that this function is not thread-safe
and must be called by the same thread calling Manager::pollEvent().
But many part of code seem not taking in account this notice
and call it outside of the correct thread.

This ticket fixes this by using a mutex.

Change-Id: I82d4cc2d6f26c1157caae0e4826dbf96a73561c6
Tuleap: #837
parent 1afa10bc
No related branches found
No related tags found
No related merge requests found
...@@ -1355,14 +1355,14 @@ Manager::removeAudio(Call& call) ...@@ -1355,14 +1355,14 @@ Manager::removeAudio(Call& call)
getRingBufferPool().unBindAll(call_id); getRingBufferPool().unBindAll(call_id);
} }
// Not thread-safe, SHOULD be called in same thread that run poolEvents() // Not thread-safe, SHOULD be called in same thread that run pollEvents()
void void
Manager::registerEventHandler(uintptr_t handlerId, EventHandler handler) Manager::registerEventHandler(uintptr_t handlerId, EventHandler handler)
{ {
eventHandlerMap_[handlerId] = handler; eventHandlerMap_[handlerId] = handler;
} }
// Not thread-safe, SHOULD be called in same thread that run poolEvents() // Not thread-safe, SHOULD be called in same thread that run pollEvents()
void void
Manager::unregisterEventHandler(uintptr_t handlerId) Manager::unregisterEventHandler(uintptr_t handlerId)
{ {
...@@ -1375,10 +1375,10 @@ Manager::unregisterEventHandler(uintptr_t handlerId) ...@@ -1375,10 +1375,10 @@ Manager::unregisterEventHandler(uintptr_t handlerId)
} }
} }
// Not thread-safe, SHOULD be called in same thread that run poolEvents()
void void
Manager::addTask(const std::function<bool()>&& task) Manager::addTask(const std::function<bool()>&& task)
{ {
std::lock_guard<std::mutex> lock(scheduledTasksMutex_);
pendingTaskList_.emplace_back(std::move(task)); pendingTaskList_.emplace_back(std::move(task));
} }
...@@ -1441,8 +1441,11 @@ void Manager::pollEvents() ...@@ -1441,8 +1441,11 @@ void Manager::pollEvents()
//-- Tasks //-- Tasks
{ {
auto tmpList = std::move(pendingTaskList_); decltype(pendingTaskList_) tmpList;
pendingTaskList_.clear(); {
std::lock_guard<std::mutex> lock(scheduledTasksMutex_);
std::swap(pendingTaskList_, tmpList);
}
auto iter = std::begin(tmpList); auto iter = std::begin(tmpList);
while (iter != tmpList.cend()) { while (iter != tmpList.cend()) {
if (finished_) if (finished_)
...@@ -1460,9 +1463,12 @@ void Manager::pollEvents() ...@@ -1460,9 +1463,12 @@ void Manager::pollEvents()
tmpList.erase(iter); tmpList.erase(iter);
iter = next; iter = next;
} }
{
std::lock_guard<std::mutex> lock(scheduledTasksMutex_);
pendingTaskList_.splice(std::end(pendingTaskList_), tmpList); pendingTaskList_.splice(std::end(pendingTaskList_), tmpList);
} }
} }
}
//THREAD=Main //THREAD=Main
void void
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment