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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/threading/thread_local_storage.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     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 +#ifndef BASE_THREADING_THREAD_LOCAL_STORAGE_H_
     1.9 +#define BASE_THREADING_THREAD_LOCAL_STORAGE_H_
    1.10 +
    1.11 +#include "base/base_export.h"
    1.12 +#include "base/basictypes.h"
    1.13 +
    1.14 +#if defined(OS_POSIX)
    1.15 +#include <pthread.h>
    1.16 +#endif
    1.17 +
    1.18 +namespace base {
    1.19 +
    1.20 +// Wrapper for thread local storage.  This class doesn't do much except provide
    1.21 +// an API for portability.
    1.22 +class BASE_EXPORT ThreadLocalStorage {
    1.23 + public:
    1.24 +
    1.25 +  // Prototype for the TLS destructor function, which can be optionally used to
    1.26 +  // cleanup thread local storage on thread exit.  'value' is the data that is
    1.27 +  // stored in thread local storage.
    1.28 +  typedef void (*TLSDestructorFunc)(void* value);
    1.29 +
    1.30 +  // StaticSlot uses its own struct initializer-list style static
    1.31 +  // initialization, as base's LINKER_INITIALIZED requires a constructor and on
    1.32 +  // some compilers (notably gcc 4.4) this still ends up needing runtime
    1.33 +  // initialization.
    1.34 +  #define TLS_INITIALIZER {0}
    1.35 +
    1.36 +  // A key representing one value stored in TLS.
    1.37 +  // Initialize like
    1.38 +  //   ThreadLocalStorage::StaticSlot my_slot = TLS_INITIALIZER;
    1.39 +  // If you're not using a static variable, use the convenience class
    1.40 +  // ThreadLocalStorage::Slot (below) instead.
    1.41 +  struct BASE_EXPORT StaticSlot {
    1.42 +    // Set up the TLS slot.  Called by the constructor.
    1.43 +    // 'destructor' is a pointer to a function to perform per-thread cleanup of
    1.44 +    // this object.  If set to NULL, no cleanup is done for this TLS slot.
    1.45 +    // Returns false on error.
    1.46 +    bool Initialize(TLSDestructorFunc destructor);
    1.47 +
    1.48 +    // Free a previously allocated TLS 'slot'.
    1.49 +    // If a destructor was set for this slot, removes
    1.50 +    // the destructor so that remaining threads exiting
    1.51 +    // will not free data.
    1.52 +    void Free();
    1.53 +
    1.54 +    // Get the thread-local value stored in slot 'slot'.
    1.55 +    // Values are guaranteed to initially be zero.
    1.56 +    void* Get() const;
    1.57 +
    1.58 +    // Set the thread-local value stored in slot 'slot' to
    1.59 +    // value 'value'.
    1.60 +    void Set(void* value);
    1.61 +
    1.62 +    bool initialized() const { return initialized_; }
    1.63 +
    1.64 +    // The internals of this struct should be considered private.
    1.65 +    bool initialized_;
    1.66 +#if defined(OS_WIN)
    1.67 +    int slot_;
    1.68 +#elif defined(OS_POSIX)
    1.69 +    pthread_key_t key_;
    1.70 +#endif
    1.71 +
    1.72 +  };
    1.73 +
    1.74 +  // A convenience wrapper around StaticSlot with a constructor. Can be used
    1.75 +  // as a member variable.
    1.76 +  class BASE_EXPORT Slot : public StaticSlot {
    1.77 +   public:
    1.78 +    // Calls StaticSlot::Initialize().
    1.79 +    explicit Slot(TLSDestructorFunc destructor = NULL);
    1.80 +
    1.81 +   private:
    1.82 +    using StaticSlot::initialized_;
    1.83 +#if defined(OS_WIN)
    1.84 +    using StaticSlot::slot_;
    1.85 +#elif defined(OS_POSIX)
    1.86 +    using StaticSlot::key_;
    1.87 +#endif
    1.88 +    DISALLOW_COPY_AND_ASSIGN(Slot);
    1.89 +  };
    1.90 +
    1.91 +  DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
    1.92 +};
    1.93 +
    1.94 +}  // namespace base
    1.95 +
    1.96 +#endif  // BASE_THREADING_THREAD_LOCAL_STORAGE_H_

mercurial