Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/JsEngine.cpp

Issue 29389576: Issue 4694 - JsEngine::{SetEventCallback,RemoveEventCallback,TriggerEvent} should be thread safe. (Closed)
Patch Set: Created March 20, 2017, 1:59 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/JsEngine.cpp
diff --git a/src/JsEngine.cpp b/src/JsEngine.cpp
index 657242ee38bb7ef14184a213ea515d3bd510a8ab..f874cf97c7486193aa51c1ee55a6b8c45ebf1adf 100644
--- a/src/JsEngine.cpp
+++ b/src/JsEngine.cpp
@@ -117,19 +117,27 @@ AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
AdblockPlus::JsEngine::EventCallback callback)
{
+ std::lock_guard<std::mutex> lock(eventCallbacksMutex);
eventCallbacks[eventName] = callback;
}
void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName)
{
+ std::lock_guard<std::mutex> lock(eventCallbacksMutex);
eventCallbacks.erase(eventName);
}
void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPlus::JsValueList& params)
{
- EventMap::iterator it = eventCallbacks.find(eventName);
- if (it != eventCallbacks.end())
- it->second(params);
+ EventCallback callback;
+ {
+ std::lock_guard<std::mutex> lock(eventCallbacksMutex);
+ auto it = eventCallbacks.find(eventName);
+ if (it == eventCallbacks.end())
+ return;
+ callback = it->second;
+ }
+ callback(params);
Oleksandr 2017/03/20 20:00:42 Nit: Maybe check if callback is not null JIC?
sergei 2017/03/20 20:08:13 What about such check in SetEventCallback? Maybe e
sergei 2017/03/20 20:23:24 Actually, I think it should be in another commit.
Oleksandr 2017/03/20 20:25:25 Fair enough. It isn't that big of an issue IMO, so
}
void AdblockPlus::JsEngine::Gc()
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld