ThreadLocal.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the llvm::sys::ThreadLocal class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_THREADLOCAL_H
  18. #define LLVM_SUPPORT_THREADLOCAL_H
  19. #include "llvm/Support/DataTypes.h"
  20. #include "llvm/Support/Threading.h"
  21. #include <cassert>
  22. namespace llvm {
  23. namespace sys {
  24. // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
  25. // YOU SHOULD NEVER USE THIS DIRECTLY.
  26. class ThreadLocalImpl {
  27. typedef uint64_t ThreadLocalDataTy;
  28. /// Platform-specific thread local data.
  29. ///
  30. /// This is embedded in the class and we avoid malloc'ing/free'ing it,
  31. /// to make this class more safe for use along with CrashRecoveryContext.
  32. union {
  33. char data[sizeof(ThreadLocalDataTy)];
  34. ThreadLocalDataTy align_data;
  35. };
  36. public:
  37. ThreadLocalImpl();
  38. virtual ~ThreadLocalImpl();
  39. void setInstance(const void* d);
  40. void *getInstance();
  41. void removeInstance();
  42. };
  43. /// ThreadLocal - A class used to abstract thread-local storage. It holds,
  44. /// for each thread, a pointer a single object of type T.
  45. template<class T>
  46. class ThreadLocal : public ThreadLocalImpl {
  47. public:
  48. ThreadLocal() : ThreadLocalImpl() { }
  49. /// get - Fetches a pointer to the object associated with the current
  50. /// thread. If no object has yet been associated, it returns NULL;
  51. T* get() { return static_cast<T*>(getInstance()); }
  52. // set - Associates a pointer to an object with the current thread.
  53. void set(T* d) { setInstance(d); }
  54. // erase - Removes the pointer associated with the current thread.
  55. void erase() { removeInstance(); }
  56. };
  57. }
  58. }
  59. #endif
  60. #ifdef __GNUC__
  61. #pragma GCC diagnostic pop
  62. #endif