sequence_local_storage_map.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_
  5. #define BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_
  6. #include "base/base_export.h"
  7. #include "base/containers/flat_map.h"
  8. #include "base/macros.h"
  9. namespace base {
  10. namespace internal {
  11. // A SequenceLocalStorageMap holds (slot_id) -> (value, destructor) items for a
  12. // sequence. When a task runs, it is expected that a pointer to its sequence's
  13. // SequenceLocalStorageMap is set in TLS using
  14. // ScopedSetSequenceMapLocalStorageForCurrentThread. When a
  15. // SequenceLocalStorageMap is destroyed, it invokes the destructors associated
  16. // with values stored within it.
  17. // The Get() and Set() methods should not be accessed directly.
  18. // Use SequenceLocalStorageSlot to Get() and Set() values in the current
  19. // sequence's SequenceLocalStorageMap.
  20. class BASE_EXPORT SequenceLocalStorageMap {
  21. public:
  22. SequenceLocalStorageMap();
  23. ~SequenceLocalStorageMap();
  24. // Returns the SequenceLocalStorage bound to the current thread. It is invalid
  25. // to call this outside the scope of a
  26. // ScopedSetSequenceLocalStorageForCurrentThread.
  27. static SequenceLocalStorageMap& GetForCurrentThread();
  28. // Holds a pointer to a value alongside a destructor for this pointer.
  29. // Calls the destructor on the value upon destruction.
  30. class BASE_EXPORT ValueDestructorPair {
  31. public:
  32. using DestructorFunc = void(void*);
  33. ValueDestructorPair(void* value, DestructorFunc* destructor);
  34. ~ValueDestructorPair();
  35. ValueDestructorPair(ValueDestructorPair&& value_destructor_pair);
  36. ValueDestructorPair& operator=(ValueDestructorPair&& value_destructor_pair);
  37. void* value() const { return value_; }
  38. private:
  39. void* value_;
  40. DestructorFunc* destructor_;
  41. DISALLOW_COPY_AND_ASSIGN(ValueDestructorPair);
  42. };
  43. // Returns the value stored in |slot_id| or nullptr if no value was stored.
  44. void* Get(int slot_id);
  45. // Stores |value_destructor_pair| in |slot_id|. Overwrites and destroys any
  46. // previously stored value.
  47. void Set(int slot_id, ValueDestructorPair value_destructor_pair);
  48. private:
  49. // Map from slot id to ValueDestructorPair.
  50. // flat_map was chosen because there are expected to be relatively few entries
  51. // in the map. For low number of entries, flat_map is known to perform better
  52. // than other map implementations.
  53. base::flat_map<int, ValueDestructorPair> sls_map_;
  54. DISALLOW_COPY_AND_ASSIGN(SequenceLocalStorageMap);
  55. };
  56. // Within the scope of this object,
  57. // SequenceLocalStorageMap::GetForCurrentThread() will return a reference to the
  58. // SequenceLocalStorageMap object passed to the constructor. There can be only
  59. // one ScopedSetSequenceLocalStorageMapForCurrentThread instance per scope.
  60. class BASE_EXPORT ScopedSetSequenceLocalStorageMapForCurrentThread {
  61. public:
  62. ScopedSetSequenceLocalStorageMapForCurrentThread(
  63. SequenceLocalStorageMap* sequence_local_storage);
  64. ~ScopedSetSequenceLocalStorageMapForCurrentThread();
  65. private:
  66. DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceLocalStorageMapForCurrentThread);
  67. };
  68. } // namespace internal
  69. } // namespace base
  70. #endif // BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_