#ifndef BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_
#define BASE_THREADING_SEQUENCE_LOCAL_STORAGE_MAP_H_
#include <variant>
#include "base/auto_reset.h"
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "third_party/abseil-cpp/absl/meta/type_traits.h"
namespace base {
namespace internal {
class BASE_EXPORT SequenceLocalStorageMap {
public:
SequenceLocalStorageMap();
SequenceLocalStorageMap(const SequenceLocalStorageMap&) = delete;
SequenceLocalStorageMap& operator=(const SequenceLocalStorageMap&) = delete;
~SequenceLocalStorageMap();
static SequenceLocalStorageMap& GetForCurrentThread();
static bool IsSetForCurrentThread();
struct BASE_EXPORT ExternalValue {
RAW_PTR_EXCLUSION void* value;
template <class T>
void emplace(T* ptr) {
value = static_cast<void*>(ptr);
}
template <class T, class Deleter>
void Destroy() {
Deleter()(std::addressof(value_as<T>()));
}
template <typename T>
T& value_as() LIFETIME_BOUND {
return *static_cast<T*>(value);
}
template <typename T>
const T& value_as() const LIFETIME_BOUND {
return *static_cast<const T*>(value);
}
};
struct BASE_EXPORT alignas(sizeof(void*)) InlineValue {
char bytes[sizeof(void*)];
template <class T, class... Args>
void emplace(Args&&... args) {
static_assert(sizeof(T) <= sizeof(void*),
"Type T is too big for storage inline.");
static_assert(absl::is_trivially_relocatable<T>(),
"T doesn't qualify as trivially relocatable, which "
"precludes it from storage inline.");
static_assert(std::alignment_of<T>::value <= sizeof(T),
"Type T has alignment requirements that preclude its "
"storage inline.");
new (&bytes) T(std::forward<Args>(args)...);
}
template <class T>
void Destroy() {
value_as<T>().~T();
}
template <typename T>
T& value_as() {
return *reinterpret_cast<T*>(bytes);
}
template <typename T>
const T& value_as() const {
return *reinterpret_cast<const T*>(bytes);
}
};
union Value {
ExternalValue external_value;
InlineValue inline_value;
};
using DestructorFunc = void(Value*);
template <class T, class Deleter>
static DestructorFunc* MakeExternalDestructor() {
return [](Value* value) { value->external_value.Destroy<T, Deleter>(); };
}
template <class T>
static DestructorFunc* MakeInlineDestructor() {
return [](Value* value) { value->inline_value.Destroy<T>(); };
}
class BASE_EXPORT ValueDestructorPair {
public:
ValueDestructorPair();
ValueDestructorPair(ExternalValue value, DestructorFunc* destructor);
ValueDestructorPair(InlineValue value, DestructorFunc* destructor);
ValueDestructorPair(const ValueDestructorPair&) = delete;
ValueDestructorPair& operator=(const ValueDestructorPair&) = delete;
~ValueDestructorPair();
ValueDestructorPair(ValueDestructorPair&& value_destructor_pair);
ValueDestructorPair& operator=(ValueDestructorPair&& value_destructor_pair);
explicit operator bool() const;
Value* get() { return destructor_ != nullptr ? &value_ : nullptr; }
const Value* get() const {
return destructor_ != nullptr ? &value_ : nullptr;
}
Value* operator->() { return get(); }
const Value* operator->() const { return get(); }
private:
Value value_;
RAW_PTR_EXCLUSION DestructorFunc* destructor_;
};
bool Has(int slot_id) const;
void Reset(int slot_id);
Value* Get(int slot_id);
Value* Set(int slot_id, ValueDestructorPair value_destructor_pair);
private:
base::flat_map<int, ValueDestructorPair> sls_map_;
};
class BASE_EXPORT
[[maybe_unused,
nodiscard]] ScopedSetSequenceLocalStorageMapForCurrentThread {
public:
ScopedSetSequenceLocalStorageMapForCurrentThread(
SequenceLocalStorageMap* sequence_local_storage);
ScopedSetSequenceLocalStorageMapForCurrentThread(
const ScopedSetSequenceLocalStorageMapForCurrentThread&) = delete;
ScopedSetSequenceLocalStorageMapForCurrentThread& operator=(
const ScopedSetSequenceLocalStorageMapForCurrentThread&) = delete;
~ScopedSetSequenceLocalStorageMapForCurrentThread();
private:
const base::AutoReset<SequenceLocalStorageMap*> resetter_;
};
}
}
#endif