security/sandbox/chromium/base/threading/thread_id_name_manager.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/threading/thread_id_name_manager.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/threading/thread_id_name_manager.h"
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <string.h>
    1.12 +
    1.13 +#include "base/logging.h"
    1.14 +#include "base/memory/singleton.h"
    1.15 +#include "base/strings/string_util.h"
    1.16 +
    1.17 +namespace base {
    1.18 +namespace {
    1.19 +
    1.20 +static const char kDefaultName[] = "";
    1.21 +static std::string* g_default_name;
    1.22 +
    1.23 +}
    1.24 +
    1.25 +ThreadIdNameManager::ThreadIdNameManager()
    1.26 +    : main_process_name_(NULL),
    1.27 +      main_process_id_(kInvalidThreadId) {
    1.28 +  g_default_name = new std::string(kDefaultName);
    1.29 +
    1.30 +  AutoLock locked(lock_);
    1.31 +  name_to_interned_name_[kDefaultName] = g_default_name;
    1.32 +}
    1.33 +
    1.34 +ThreadIdNameManager::~ThreadIdNameManager() {
    1.35 +}
    1.36 +
    1.37 +ThreadIdNameManager* ThreadIdNameManager::GetInstance() {
    1.38 +  return Singleton<ThreadIdNameManager,
    1.39 +      LeakySingletonTraits<ThreadIdNameManager> >::get();
    1.40 +}
    1.41 +
    1.42 +const char* ThreadIdNameManager::GetDefaultInternedString() {
    1.43 +  return g_default_name->c_str();
    1.44 +}
    1.45 +
    1.46 +void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle,
    1.47 +                                         PlatformThreadId id) {
    1.48 +  AutoLock locked(lock_);
    1.49 +  thread_id_to_handle_[id] = handle;
    1.50 +  thread_handle_to_interned_name_[handle] =
    1.51 +      name_to_interned_name_[kDefaultName];
    1.52 +}
    1.53 +
    1.54 +void ThreadIdNameManager::SetName(PlatformThreadId id, const char* name) {
    1.55 +  std::string str_name(name);
    1.56 +
    1.57 +  AutoLock locked(lock_);
    1.58 +  NameToInternedNameMap::iterator iter = name_to_interned_name_.find(str_name);
    1.59 +  std::string* leaked_str = NULL;
    1.60 +  if (iter != name_to_interned_name_.end()) {
    1.61 +    leaked_str = iter->second;
    1.62 +  } else {
    1.63 +    leaked_str = new std::string(str_name);
    1.64 +    name_to_interned_name_[str_name] = leaked_str;
    1.65 +  }
    1.66 +
    1.67 +  ThreadIdToHandleMap::iterator id_to_handle_iter =
    1.68 +      thread_id_to_handle_.find(id);
    1.69 +
    1.70 +  // The main thread of a process will not be created as a Thread object which
    1.71 +  // means there is no PlatformThreadHandler registered.
    1.72 +  if (id_to_handle_iter == thread_id_to_handle_.end()) {
    1.73 +    main_process_name_ = leaked_str;
    1.74 +    main_process_id_ = id;
    1.75 +    return;
    1.76 +  }
    1.77 +  thread_handle_to_interned_name_[id_to_handle_iter->second] = leaked_str;
    1.78 +}
    1.79 +
    1.80 +const char* ThreadIdNameManager::GetName(PlatformThreadId id) {
    1.81 +  AutoLock locked(lock_);
    1.82 +
    1.83 +  if (id == main_process_id_)
    1.84 +    return main_process_name_->c_str();
    1.85 +
    1.86 +  ThreadIdToHandleMap::iterator id_to_handle_iter =
    1.87 +      thread_id_to_handle_.find(id);
    1.88 +  if (id_to_handle_iter == thread_id_to_handle_.end())
    1.89 +    return name_to_interned_name_[kDefaultName]->c_str();
    1.90 +
    1.91 +  ThreadHandleToInternedNameMap::iterator handle_to_name_iter =
    1.92 +      thread_handle_to_interned_name_.find(id_to_handle_iter->second);
    1.93 +  return handle_to_name_iter->second->c_str();
    1.94 +}
    1.95 +
    1.96 +void ThreadIdNameManager::RemoveName(PlatformThreadHandle::Handle handle,
    1.97 +                                     PlatformThreadId id) {
    1.98 +  AutoLock locked(lock_);
    1.99 +  ThreadHandleToInternedNameMap::iterator handle_to_name_iter =
   1.100 +      thread_handle_to_interned_name_.find(handle);
   1.101 +
   1.102 +  DCHECK(handle_to_name_iter != thread_handle_to_interned_name_.end());
   1.103 +  thread_handle_to_interned_name_.erase(handle_to_name_iter);
   1.104 +
   1.105 +  ThreadIdToHandleMap::iterator id_to_handle_iter =
   1.106 +      thread_id_to_handle_.find(id);
   1.107 +  DCHECK((id_to_handle_iter!= thread_id_to_handle_.end()));
   1.108 +  // The given |id| may have been re-used by the system. Make sure the
   1.109 +  // mapping points to the provided |handle| before removal.
   1.110 +  if (id_to_handle_iter->second != handle)
   1.111 +    return;
   1.112 +
   1.113 +  thread_id_to_handle_.erase(id_to_handle_iter);
   1.114 +}
   1.115 +
   1.116 +}  // namespace base

mercurial