From: Pawel Andruszkiewicz Date: Fri, 1 Apr 2016 12:05:22 +0000 (+0200) Subject: [SystemInfo] Wait for all existing asynchronous operations. X-Git-Tag: submit/tizen/20160509.020631^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5cd87e246002ea8cc6f468b3e0da8f027f3748ae;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [SystemInfo] Wait for all existing asynchronous operations. Running cordova test with WiFi disabled and mobile data enabled caused segmentation fault. This commit fixed that issue. [Verification] TCT pass rate: 100% (297/297/0/0/0) Change-Id: Ibdc42f439f878605c7edc0e5a9c2084d8528226d Signed-off-by: Pawel Andruszkiewicz --- diff --git a/src/systeminfo/systeminfo_manager.cc b/src/systeminfo/systeminfo_manager.cc index 35c2c2f8..c3323831 100644 --- a/src/systeminfo/systeminfo_manager.cc +++ b/src/systeminfo/systeminfo_manager.cc @@ -16,6 +16,7 @@ #include "systeminfo/systeminfo_manager.h" +#include #include #include @@ -423,6 +424,38 @@ class SysteminfoManager::TapiManager { SimDetailsManager sim_manager_; }; +class SysteminfoManager::AsynchronousOperation { + public: + AsynchronousOperation(int count_ = 0) + : count_(count_) { + } + + ~AsynchronousOperation() { + std::unique_lock lock(mutex_); + + while (0 != count_) { + cv_.wait(lock); + } + } + + inline void start() { + std::unique_lock lock(mutex_); + ++count_; + cv_.notify_one(); + } + + inline void finish() { + std::unique_lock lock(mutex_); + --count_; + cv_.notify_one(); + } + + private: + std::mutex mutex_; + std::condition_variable cv_; + int count_; +}; + SysteminfoManager::SysteminfoManager(SysteminfoInstance* instance) : instance_(instance), prop_manager_(*this), @@ -437,7 +470,8 @@ SysteminfoManager::SysteminfoManager(SysteminfoInstance* instance) tapi_manager_(new TapiManager()), cpu_event_id_(0), storage_event_id_(0), - connection_handle_(nullptr) { + connection_handle_(nullptr), + async_op_(new AsynchronousOperation()) { LoggerD("Entered"); int error = wifi_initialize(); if (WIFI_ERROR_NONE != error) { @@ -601,6 +635,8 @@ void SysteminfoManager::GetPropertyValue(const picojson::value& args, picojson:: return; } ReportSuccess(result, response->get()); + + async_op_->finish(); }; auto get_response = [this, callback_id](const std::shared_ptr& response) -> void { @@ -614,6 +650,8 @@ void SysteminfoManager::GetPropertyValue(const picojson::value& args, picojson:: auto data = std::shared_ptr(new picojson::value(picojson::object())); TaskQueue::GetInstance().Queue(get, get_response, data); + + async_op_->start(); } void SysteminfoManager::GetPropertyValueArray(const picojson::value& args, picojson::object* out) { @@ -636,6 +674,8 @@ void SysteminfoManager::GetPropertyValueArray(const picojson::value& args, picoj return; } ReportSuccess(result, response->get()); + + async_op_->finish(); }; auto get_response = [this, callback_id](const std::shared_ptr& response) -> void { @@ -648,6 +688,8 @@ void SysteminfoManager::GetPropertyValueArray(const picojson::value& args, picoj auto data = std::shared_ptr(new picojson::value(picojson::object())); TaskQueue::GetInstance().Queue(get, get_response, data); + + async_op_->start(); } void SysteminfoManager::GetTotalMemory(const picojson::value& args, picojson::object* out) { diff --git a/src/systeminfo/systeminfo_manager.h b/src/systeminfo/systeminfo_manager.h index 5251633c..c34dd4e2 100644 --- a/src/systeminfo/systeminfo_manager.h +++ b/src/systeminfo/systeminfo_manager.h @@ -86,6 +86,7 @@ class SysteminfoManager { void CallStorageListenerCallback(); private: class TapiManager; + class AsynchronousOperation; void PostListenerResponse(const std::string& property_id, const picojson::value& result, int property_index = 0); @@ -150,6 +151,7 @@ class SysteminfoManager { connection_h connection_handle_; std::mutex connection_mutex_; + std::unique_ptr async_op_; }; } // namespace systeminfo } // namespace webapi