Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Cross-platform lightweight thread local data wrappers. */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef mozilla_ThreadLocal_h |
michael@0 | 10 | #define mozilla_ThreadLocal_h |
michael@0 | 11 | |
michael@0 | 12 | #if defined(XP_WIN) |
michael@0 | 13 | // This file will get included in any file that wants to add a profiler mark. |
michael@0 | 14 | // In order to not bring <windows.h> together we could include windef.h and |
michael@0 | 15 | // winbase.h which are sufficient to get the prototypes for the Tls* functions. |
michael@0 | 16 | // # include <windef.h> |
michael@0 | 17 | // # include <winbase.h> |
michael@0 | 18 | // Unfortunately, even including these headers causes us to add a bunch of ugly |
michael@0 | 19 | // stuff to our namespace e.g #define CreateEvent CreateEventW |
michael@0 | 20 | extern "C" { |
michael@0 | 21 | __declspec(dllimport) void* __stdcall TlsGetValue(unsigned long); |
michael@0 | 22 | __declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*); |
michael@0 | 23 | __declspec(dllimport) unsigned long __stdcall TlsAlloc(); |
michael@0 | 24 | } |
michael@0 | 25 | #else |
michael@0 | 26 | # include <pthread.h> |
michael@0 | 27 | # include <signal.h> |
michael@0 | 28 | #endif |
michael@0 | 29 | |
michael@0 | 30 | #include "mozilla/Assertions.h" |
michael@0 | 31 | #include "mozilla/Attributes.h" |
michael@0 | 32 | #include "mozilla/NullPtr.h" |
michael@0 | 33 | |
michael@0 | 34 | namespace mozilla { |
michael@0 | 35 | |
michael@0 | 36 | // sig_safe_t denotes an atomic type which can be read or stored in a single |
michael@0 | 37 | // instruction. This means that data of this type is safe to be manipulated |
michael@0 | 38 | // from a signal handler, or other similar asynchronous execution contexts. |
michael@0 | 39 | #if defined(XP_WIN) |
michael@0 | 40 | typedef unsigned long sig_safe_t; |
michael@0 | 41 | #else |
michael@0 | 42 | typedef sig_atomic_t sig_safe_t; |
michael@0 | 43 | #endif |
michael@0 | 44 | |
michael@0 | 45 | /* |
michael@0 | 46 | * Thread Local Storage helpers. |
michael@0 | 47 | * |
michael@0 | 48 | * Usage: |
michael@0 | 49 | * |
michael@0 | 50 | * Only static-storage-duration (e.g. global variables, or static class members) |
michael@0 | 51 | * objects of this class should be instantiated. This class relies on |
michael@0 | 52 | * zero-initialization, which is implicit for static-storage-duration objects. |
michael@0 | 53 | * It doesn't have a custom default constructor, to avoid static initializers. |
michael@0 | 54 | * |
michael@0 | 55 | * API usage: |
michael@0 | 56 | * |
michael@0 | 57 | * // Create a TLS item. |
michael@0 | 58 | * // |
michael@0 | 59 | * // Note that init() should be invoked exactly once, before any usage of set() |
michael@0 | 60 | * // or get(). |
michael@0 | 61 | * mozilla::ThreadLocal<int> tlsKey; |
michael@0 | 62 | * if (!tlsKey.init()) { |
michael@0 | 63 | * // deal with the error |
michael@0 | 64 | * } |
michael@0 | 65 | * |
michael@0 | 66 | * // Set the TLS value |
michael@0 | 67 | * tlsKey.set(123); |
michael@0 | 68 | * |
michael@0 | 69 | * // Get the TLS value |
michael@0 | 70 | * int value = tlsKey.get(); |
michael@0 | 71 | */ |
michael@0 | 72 | template<typename T> |
michael@0 | 73 | class ThreadLocal |
michael@0 | 74 | { |
michael@0 | 75 | #if defined(XP_WIN) |
michael@0 | 76 | typedef unsigned long key_t; |
michael@0 | 77 | #else |
michael@0 | 78 | typedef pthread_key_t key_t; |
michael@0 | 79 | #endif |
michael@0 | 80 | |
michael@0 | 81 | union Helper { |
michael@0 | 82 | void* ptr; |
michael@0 | 83 | T value; |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | public: |
michael@0 | 87 | MOZ_WARN_UNUSED_RESULT inline bool init(); |
michael@0 | 88 | |
michael@0 | 89 | inline T get() const; |
michael@0 | 90 | |
michael@0 | 91 | inline void set(const T value); |
michael@0 | 92 | |
michael@0 | 93 | bool initialized() const { |
michael@0 | 94 | return inited; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | private: |
michael@0 | 98 | key_t key; |
michael@0 | 99 | bool inited; |
michael@0 | 100 | }; |
michael@0 | 101 | |
michael@0 | 102 | template<typename T> |
michael@0 | 103 | inline bool |
michael@0 | 104 | ThreadLocal<T>::init() |
michael@0 | 105 | { |
michael@0 | 106 | static_assert(sizeof(T) <= sizeof(void*), |
michael@0 | 107 | "mozilla::ThreadLocal can't be used for types larger than " |
michael@0 | 108 | "a pointer"); |
michael@0 | 109 | MOZ_ASSERT(!initialized()); |
michael@0 | 110 | #ifdef XP_WIN |
michael@0 | 111 | key = TlsAlloc(); |
michael@0 | 112 | inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES |
michael@0 | 113 | #else |
michael@0 | 114 | inited = !pthread_key_create(&key, nullptr); |
michael@0 | 115 | #endif |
michael@0 | 116 | return inited; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | template<typename T> |
michael@0 | 120 | inline T |
michael@0 | 121 | ThreadLocal<T>::get() const |
michael@0 | 122 | { |
michael@0 | 123 | MOZ_ASSERT(initialized()); |
michael@0 | 124 | Helper h; |
michael@0 | 125 | #ifdef XP_WIN |
michael@0 | 126 | h.ptr = TlsGetValue(key); |
michael@0 | 127 | #else |
michael@0 | 128 | h.ptr = pthread_getspecific(key); |
michael@0 | 129 | #endif |
michael@0 | 130 | return h.value; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | template<typename T> |
michael@0 | 134 | inline void |
michael@0 | 135 | ThreadLocal<T>::set(const T value) |
michael@0 | 136 | { |
michael@0 | 137 | MOZ_ASSERT(initialized()); |
michael@0 | 138 | Helper h; |
michael@0 | 139 | h.value = value; |
michael@0 | 140 | bool succeeded; |
michael@0 | 141 | #ifdef XP_WIN |
michael@0 | 142 | succeeded = TlsSetValue(key, h.ptr); |
michael@0 | 143 | #else |
michael@0 | 144 | succeeded = !pthread_setspecific(key, h.ptr); |
michael@0 | 145 | #endif |
michael@0 | 146 | if (!succeeded) |
michael@0 | 147 | MOZ_CRASH(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | } // namespace mozilla |
michael@0 | 151 | |
michael@0 | 152 | #endif /* mozilla_ThreadLocal_h */ |