security/sandbox/chromium/base/threading/thread_local_storage.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Copyright (c) 2012 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 #ifndef BASE_THREADING_THREAD_LOCAL_STORAGE_H_
michael@0 6 #define BASE_THREADING_THREAD_LOCAL_STORAGE_H_
michael@0 7
michael@0 8 #include "base/base_export.h"
michael@0 9 #include "base/basictypes.h"
michael@0 10
michael@0 11 #if defined(OS_POSIX)
michael@0 12 #include <pthread.h>
michael@0 13 #endif
michael@0 14
michael@0 15 namespace base {
michael@0 16
michael@0 17 // Wrapper for thread local storage. This class doesn't do much except provide
michael@0 18 // an API for portability.
michael@0 19 class BASE_EXPORT ThreadLocalStorage {
michael@0 20 public:
michael@0 21
michael@0 22 // Prototype for the TLS destructor function, which can be optionally used to
michael@0 23 // cleanup thread local storage on thread exit. 'value' is the data that is
michael@0 24 // stored in thread local storage.
michael@0 25 typedef void (*TLSDestructorFunc)(void* value);
michael@0 26
michael@0 27 // StaticSlot uses its own struct initializer-list style static
michael@0 28 // initialization, as base's LINKER_INITIALIZED requires a constructor and on
michael@0 29 // some compilers (notably gcc 4.4) this still ends up needing runtime
michael@0 30 // initialization.
michael@0 31 #define TLS_INITIALIZER {0}
michael@0 32
michael@0 33 // A key representing one value stored in TLS.
michael@0 34 // Initialize like
michael@0 35 // ThreadLocalStorage::StaticSlot my_slot = TLS_INITIALIZER;
michael@0 36 // If you're not using a static variable, use the convenience class
michael@0 37 // ThreadLocalStorage::Slot (below) instead.
michael@0 38 struct BASE_EXPORT StaticSlot {
michael@0 39 // Set up the TLS slot. Called by the constructor.
michael@0 40 // 'destructor' is a pointer to a function to perform per-thread cleanup of
michael@0 41 // this object. If set to NULL, no cleanup is done for this TLS slot.
michael@0 42 // Returns false on error.
michael@0 43 bool Initialize(TLSDestructorFunc destructor);
michael@0 44
michael@0 45 // Free a previously allocated TLS 'slot'.
michael@0 46 // If a destructor was set for this slot, removes
michael@0 47 // the destructor so that remaining threads exiting
michael@0 48 // will not free data.
michael@0 49 void Free();
michael@0 50
michael@0 51 // Get the thread-local value stored in slot 'slot'.
michael@0 52 // Values are guaranteed to initially be zero.
michael@0 53 void* Get() const;
michael@0 54
michael@0 55 // Set the thread-local value stored in slot 'slot' to
michael@0 56 // value 'value'.
michael@0 57 void Set(void* value);
michael@0 58
michael@0 59 bool initialized() const { return initialized_; }
michael@0 60
michael@0 61 // The internals of this struct should be considered private.
michael@0 62 bool initialized_;
michael@0 63 #if defined(OS_WIN)
michael@0 64 int slot_;
michael@0 65 #elif defined(OS_POSIX)
michael@0 66 pthread_key_t key_;
michael@0 67 #endif
michael@0 68
michael@0 69 };
michael@0 70
michael@0 71 // A convenience wrapper around StaticSlot with a constructor. Can be used
michael@0 72 // as a member variable.
michael@0 73 class BASE_EXPORT Slot : public StaticSlot {
michael@0 74 public:
michael@0 75 // Calls StaticSlot::Initialize().
michael@0 76 explicit Slot(TLSDestructorFunc destructor = NULL);
michael@0 77
michael@0 78 private:
michael@0 79 using StaticSlot::initialized_;
michael@0 80 #if defined(OS_WIN)
michael@0 81 using StaticSlot::slot_;
michael@0 82 #elif defined(OS_POSIX)
michael@0 83 using StaticSlot::key_;
michael@0 84 #endif
michael@0 85 DISALLOW_COPY_AND_ASSIGN(Slot);
michael@0 86 };
michael@0 87
michael@0 88 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
michael@0 89 };
michael@0 90
michael@0 91 } // namespace base
michael@0 92
michael@0 93 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_

mercurial