summaryrefslogtreecommitdiffstats
path: root/mojo/common
diff options
context:
space:
mode:
Diffstat (limited to 'mojo/common')
-rw-r--r--mojo/common/environment_data.cc51
-rw-r--r--mojo/common/environment_data.h52
-rw-r--r--mojo/common/handle_watcher.cc24
-rw-r--r--mojo/common/handle_watcher_unittest.cc7
4 files changed, 6 insertions, 128 deletions
diff --git a/mojo/common/environment_data.cc b/mojo/common/environment_data.cc
deleted file mode 100644
index b523d27..0000000
--- a/mojo/common/environment_data.cc
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/common/environment_data.h"
-
-#include "base/stl_util.h"
-
-namespace mojo {
-namespace common {
-
-// static
-EnvironmentData* EnvironmentData::instance_ = NULL;
-
-EnvironmentData::EnvironmentData() {
- DCHECK(!instance_);
- instance_ = this;
-}
-
-EnvironmentData::~EnvironmentData() {
- instance_ = NULL;
- DataMap data_map;
- data_map.swap(data_map_);
- STLDeleteContainerPairSecondPointers(data_map.begin(), data_map.end());
-}
-
-// static
-EnvironmentData* EnvironmentData::GetInstance() {
- return instance_;
-}
-
-void EnvironmentData::SetData(const void* key, scoped_ptr<Data> data) {
- Data* old = NULL;
- {
- base::AutoLock auto_lock(data_lock_);
- old = data_map_[key];
- if (data)
- data_map_[key] = data.release();
- else
- data_map_.erase(key);
- }
- delete old;
-}
-
-EnvironmentData::Data* EnvironmentData::GetData(const void* key) {
- base::AutoLock auto_lock(data_lock_);
- return data_map_.count(key) > 0 ? data_map_[key] : NULL;
-}
-
-} // namespace common
-} // namespace mojo
diff --git a/mojo/common/environment_data.h b/mojo/common/environment_data.h
deleted file mode 100644
index 4440df2..0000000
--- a/mojo/common/environment_data.h
+++ /dev/null
@@ -1,52 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef MOJO_COMMON_ENVIRONMENT_DATA_H_
-#define MOJO_COMMON_ENVIRONMENT_DATA_H_
-
-#include <map>
-
-#include "base/memory/scoped_ptr.h"
-#include "base/synchronization/lock.h"
-#include "mojo/common/mojo_common_export.h"
-
-namespace mojo {
-namespace common {
-
-// EnvironmentData is used to store arbitrary key/value pairs in the
-// environment. The key/value pairs are owned by the Environment and deleted
-// when it is deleted.
-class MOJO_COMMON_EXPORT EnvironmentData {
- public:
- class MOJO_COMMON_EXPORT Data {
- public:
- Data() {}
- virtual ~Data() {}
- };
-
- EnvironmentData();
- ~EnvironmentData();
-
- static EnvironmentData* GetInstance();
-
- void SetData(const void* key, scoped_ptr<Data> data);
-
- Data* GetData(const void* key);
-
- private:
- typedef std::map<const void*, Data*> DataMap;
-
- static EnvironmentData* instance_;
-
- base::Lock data_lock_;
-
- DataMap data_map_;
-
- DISALLOW_COPY_AND_ASSIGN(EnvironmentData);
-};
-
-} // namespace common
-} // namespace mojo
-
-#endif // MOJO_COMMON_ENVIRONMENT_DATA_H_
diff --git a/mojo/common/handle_watcher.cc b/mojo/common/handle_watcher.cc
index 1affa90..4ae86db 100644
--- a/mojo/common/handle_watcher.cc
+++ b/mojo/common/handle_watcher.cc
@@ -9,13 +9,13 @@
#include "base/atomic_sequence_num.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
+#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
-#include "mojo/common/environment_data.h"
#include "mojo/common/message_pump_mojo.h"
#include "mojo/common/message_pump_mojo_handler.h"
#include "mojo/common/time_helper.h"
@@ -29,8 +29,6 @@ namespace {
const char kWatcherThreadName[] = "handle-watcher-thread";
-const char kWatcherThreadManagerKey[] = "watcher-thread-manager";
-
// TODO(sky): this should be unnecessary once MessageLoop has been refactored.
MessagePumpMojo* message_pump_mojo = NULL;
@@ -176,6 +174,7 @@ class WatcherThreadManager {
void StopWatching(WatcherID watcher_id);
private:
+ friend struct DefaultSingletonTraits<WatcherThreadManager>;
WatcherThreadManager();
base::Thread thread_;
@@ -187,29 +186,12 @@ class WatcherThreadManager {
DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager);
};
-struct WatcherThreadManagerData : EnvironmentData::Data {
- scoped_ptr<WatcherThreadManager> thread_manager;
-};
-
WatcherThreadManager::~WatcherThreadManager() {
thread_.Stop();
}
-static base::LazyInstance<base::Lock> thread_lookup_lock =
- LAZY_INSTANCE_INITIALIZER;
-
WatcherThreadManager* WatcherThreadManager::GetInstance() {
- base::AutoLock auto_lock(thread_lookup_lock.Get());
- WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>(
- EnvironmentData::GetInstance()->GetData(kWatcherThreadManagerKey));
- if (!data) {
- data = new WatcherThreadManagerData;
- data->thread_manager.reset(new WatcherThreadManager);
- EnvironmentData::GetInstance()->SetData(
- kWatcherThreadManagerKey,
- scoped_ptr<EnvironmentData::Data>(data));
- }
- return data->thread_manager.get();
+ return Singleton<WatcherThreadManager>::get();
}
WatcherID WatcherThreadManager::StartWatching(
diff --git a/mojo/common/handle_watcher_unittest.cc b/mojo/common/handle_watcher_unittest.cc
index 37fde23..39667814 100644
--- a/mojo/common/handle_watcher_unittest.cc
+++ b/mojo/common/handle_watcher_unittest.cc
@@ -6,12 +6,12 @@
#include <string>
+#include "base/at_exit.h"
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/simple_test_tick_clock.h"
#include "mojo/common/time_helper.h"
-#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -116,7 +116,7 @@ class HandleWatcherTest : public testing::Test {
base::SimpleTestTickClock tick_clock_;
private:
- Environment environment_;
+ base::ShadowingAtExitManager at_exit_;
base::MessageLoop message_loop_;
DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest);
@@ -318,8 +318,7 @@ TEST(HandleWatcherCleanEnvironmentTest, AbortedOnMessageLoopDestruction) {
bool was_signaled = false;
MojoResult result = MOJO_RESULT_OK;
- Environment env;
-
+ base::ShadowingAtExitManager at_exit;
MessagePipe pipe;
HandleWatcher watcher;
{