ipc/chromium/src/base/thread_local.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // WARNING: Thread local storage is a bit tricky to get right. Please make
michael@0 6 // sure that this is really the proper solution for what you're trying to
michael@0 7 // achieve. Don't prematurely optimize, most likely you can just use a Lock.
michael@0 8 //
michael@0 9 // These classes implement a warpper around the platform's TLS storage
michael@0 10 // mechanism. On construction, they will allocate a TLS slot, and free the
michael@0 11 // TLS slot on destruction. No memory management (creation or destruction) is
michael@0 12 // handled. This means for uses of ThreadLocalPointer, you must correctly
michael@0 13 // manage the memory yourself, these classes will not destroy the pointer for
michael@0 14 // you. There are no at-thread-exit actions taken by these classes.
michael@0 15 //
michael@0 16 // ThreadLocalPointer<Type> wraps a Type*. It performs no creation or
michael@0 17 // destruction, so memory management must be handled elsewhere. The first call
michael@0 18 // to Get() on a thread will return NULL. You can update the pointer with a
michael@0 19 // call to Set().
michael@0 20 //
michael@0 21 // ThreadLocalBoolean wraps a bool. It will default to false if it has never
michael@0 22 // been set otherwise with Set().
michael@0 23 //
michael@0 24 // Thread Safety: An instance of ThreadLocalStorage is completely thread safe
michael@0 25 // once it has been created. If you want to dynamically create an instance,
michael@0 26 // you must of course properly deal with safety and race conditions. This
michael@0 27 // means a function-level static initializer is generally inappropiate.
michael@0 28 //
michael@0 29 // Example usage:
michael@0 30 // // My class is logically attached to a single thread. We cache a pointer
michael@0 31 // // on the thread it was created on, so we can implement current().
michael@0 32 // MyClass::MyClass() {
michael@0 33 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() == NULL);
michael@0 34 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(this);
michael@0 35 // }
michael@0 36 //
michael@0 37 // MyClass::~MyClass() {
michael@0 38 // DCHECK(Singleton<ThreadLocalPointer<MyClass> >::get()->Get() != NULL);
michael@0 39 // Singleton<ThreadLocalPointer<MyClass> >::get()->Set(NULL);
michael@0 40 // }
michael@0 41 //
michael@0 42 // // Return the current MyClass associated with the calling thread, can be
michael@0 43 // // NULL if there isn't a MyClass associated.
michael@0 44 // MyClass* MyClass::current() {
michael@0 45 // return Singleton<ThreadLocalPointer<MyClass> >::get()->Get();
michael@0 46 // }
michael@0 47
michael@0 48 #ifndef BASE_THREAD_LOCAL_H_
michael@0 49 #define BASE_THREAD_LOCAL_H_
michael@0 50
michael@0 51 #include "base/basictypes.h"
michael@0 52
michael@0 53 #if defined(OS_POSIX)
michael@0 54 #include <pthread.h>
michael@0 55 #endif
michael@0 56
michael@0 57 namespace base {
michael@0 58
michael@0 59 // Helper functions that abstract the cross-platform APIs. Do not use directly.
michael@0 60 struct ThreadLocalPlatform {
michael@0 61 #if defined(OS_WIN)
michael@0 62 typedef int SlotType;
michael@0 63 #elif defined(OS_POSIX)
michael@0 64 typedef pthread_key_t SlotType;
michael@0 65 #endif
michael@0 66
michael@0 67 static void AllocateSlot(SlotType& slot);
michael@0 68 static void FreeSlot(SlotType& slot);
michael@0 69 static void* GetValueFromSlot(SlotType& slot);
michael@0 70 static void SetValueInSlot(SlotType& slot, void* value);
michael@0 71 };
michael@0 72
michael@0 73 template <typename Type>
michael@0 74 class ThreadLocalPointer {
michael@0 75 public:
michael@0 76 ThreadLocalPointer() : slot_() {
michael@0 77 ThreadLocalPlatform::AllocateSlot(slot_);
michael@0 78 }
michael@0 79
michael@0 80 ~ThreadLocalPointer() {
michael@0 81 ThreadLocalPlatform::FreeSlot(slot_);
michael@0 82 }
michael@0 83
michael@0 84 Type* Get() {
michael@0 85 return static_cast<Type*>(ThreadLocalPlatform::GetValueFromSlot(slot_));
michael@0 86 }
michael@0 87
michael@0 88 void Set(Type* ptr) {
michael@0 89 ThreadLocalPlatform::SetValueInSlot(slot_, ptr);
michael@0 90 }
michael@0 91
michael@0 92 private:
michael@0 93 typedef ThreadLocalPlatform::SlotType SlotType;
michael@0 94
michael@0 95 SlotType slot_;
michael@0 96
michael@0 97 DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer<Type>);
michael@0 98 };
michael@0 99
michael@0 100 class ThreadLocalBoolean {
michael@0 101 public:
michael@0 102 ThreadLocalBoolean() { }
michael@0 103 ~ThreadLocalBoolean() { }
michael@0 104
michael@0 105 bool Get() {
michael@0 106 return tlp_.Get() != NULL;
michael@0 107 }
michael@0 108
michael@0 109 void Set(bool val) {
michael@0 110 tlp_.Set(reinterpret_cast<void*>(val ? 1 : 0));
michael@0 111 }
michael@0 112
michael@0 113 private:
michael@0 114 ThreadLocalPointer<void> tlp_;
michael@0 115
michael@0 116 DISALLOW_COPY_AND_ASSIGN(ThreadLocalBoolean);
michael@0 117 };
michael@0 118
michael@0 119 } // namespace base
michael@0 120
michael@0 121 #endif // BASE_THREAD_LOCAL_H_

mercurial