From: Cheng Zhao Date: Fri, 10 Mar 2017 08:33:27 +0000 (+0900) Subject: Make Archive thread safe X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e496e18f6e3a34ab97cc6a12aa757c8e48d2066f;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Make Archive thread safe --- diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index c175b3d..d3f8237 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -14,6 +14,7 @@ #include "atom/browser/javascript_environment.h" #include "atom/browser/node_debugger.h" #include "atom/common/api/atom_bindings.h" +#include "atom/common/asar/asar_util.h" #include "atom/common/node_bindings.h" #include "atom/common/node_includes.h" #include "base/command_line.h" @@ -71,6 +72,7 @@ AtomBrowserMainParts::AtomBrowserMainParts() } AtomBrowserMainParts::~AtomBrowserMainParts() { + asar::ClearArchives(); // Leak the JavascriptEnvironment on exit. // This is to work around the bug that V8 would be waiting for background // tasks to finish on exit, while somehow it waits forever in Electron, more diff --git a/atom/common/asar/asar_util.cc b/atom/common/asar/asar_util.cc index 1eee099..0ffbfc6 100644 --- a/atom/common/asar/asar_util.cc +++ b/atom/common/asar/asar_util.cc @@ -12,6 +12,7 @@ #include "base/files/file_util.h" #include "base/lazy_instance.h" #include "base/stl_util.h" +#include "base/threading/thread_local.h" namespace asar { @@ -19,14 +20,17 @@ namespace { // The global instance of ArchiveMap, will be destroyed on exit. typedef std::map> ArchiveMap; -static base::LazyInstance g_archive_map = LAZY_INSTANCE_INITIALIZER; +base::LazyInstance>::Leaky + g_archive_map_tls = LAZY_INSTANCE_INITIALIZER; const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar"); } // namespace std::shared_ptr GetOrCreateAsarArchive(const base::FilePath& path) { - ArchiveMap& archive_map = *g_archive_map.Pointer(); + if (!g_archive_map_tls.Pointer()->Get()) + g_archive_map_tls.Pointer()->Set(new ArchiveMap); + ArchiveMap& archive_map = *g_archive_map_tls.Pointer()->Get(); if (!ContainsKey(archive_map, path)) { std::shared_ptr archive(new Archive(path)); if (!archive->Init()) @@ -36,6 +40,11 @@ std::shared_ptr GetOrCreateAsarArchive(const base::FilePath& path) { return archive_map[path]; } +void ClearArchives() { + if (g_archive_map_tls.Pointer()->Get()) + delete g_archive_map_tls.Pointer()->Get(); +} + bool GetAsarArchivePath(const base::FilePath& full_path, base::FilePath* asar_path, base::FilePath* relative_path) { diff --git a/atom/common/asar/asar_util.h b/atom/common/asar/asar_util.h index 4cb5b88..90ffb9b 100644 --- a/atom/common/asar/asar_util.h +++ b/atom/common/asar/asar_util.h @@ -19,6 +19,9 @@ class Archive; // Gets or creates a new Archive from the path. std::shared_ptr GetOrCreateAsarArchive(const base::FilePath& path); +// Destroy cached Archive objects. +void ClearArchives(); + // Separates the path to Archive out. bool GetAsarArchivePath(const base::FilePath& full_path, base::FilePath* asar_path, diff --git a/atom/common/native_mate_converters/callback.h b/atom/common/native_mate_converters/callback.h index f42ee87..28bff3c 100644 --- a/atom/common/native_mate_converters/callback.h +++ b/atom/common/native_mate_converters/callback.h @@ -10,8 +10,8 @@ #include "atom/common/api/locker.h" #include "base/bind.h" #include "base/callback.h" -#include "base/message_loop/message_loop.h" #include "base/memory/weak_ptr.h" +#include "base/message_loop/message_loop.h" #include "content/public/browser/browser_thread.h" #include "native_mate/function_template.h" #include "native_mate/scoped_persistent.h" diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 37a16ee..c4f739c 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -13,6 +13,7 @@ #include "atom/common/api/atom_bindings.h" #include "atom/common/api/event_emitter_caller.h" #include "atom/common/atom_constants.h" +#include "atom/common/asar/asar_util.h" #include "atom/common/color_util.h" #include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/node_bindings.h" @@ -228,6 +229,7 @@ AtomRendererClient::AtomRendererClient() } AtomRendererClient::~AtomRendererClient() { + asar::ClearArchives(); } void AtomRendererClient::RenderThreadStarted() { diff --git a/atom/renderer/web_worker_observer.cc b/atom/renderer/web_worker_observer.cc index 323c9be..954b2f2 100644 --- a/atom/renderer/web_worker_observer.cc +++ b/atom/renderer/web_worker_observer.cc @@ -6,6 +6,7 @@ #include "atom/common/api/atom_bindings.h" #include "atom/common/api/event_emitter_caller.h" +#include "atom/common/asar/asar_util.h" #include "atom/common/node_bindings.h" #include "base/lazy_instance.h" #include "base/threading/thread_local.h" @@ -35,6 +36,8 @@ WebWorkerObserver::WebWorkerObserver() WebWorkerObserver::~WebWorkerObserver() { lazy_tls.Pointer()->Set(nullptr); + node::FreeEnvironment(node_bindings_->uv_env()); + asar::ClearArchives(); } void WebWorkerObserver::ContextCreated(v8::Local context) { @@ -64,8 +67,6 @@ void WebWorkerObserver::ContextWillDestroy(v8::Local context) { if (env) mate::EmitEvent(env->isolate(), env->process_object(), "exit"); - // Destroy the node environment. - node::FreeEnvironment(env); delete this; }