CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
94 lines
3.1 KiB
C++
94 lines
3.1 KiB
C++
//===-- tsd.h ---------------------------------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SCUDO_TSD_H_
|
|
#define SCUDO_TSD_H_
|
|
|
|
#include "atomic_helpers.h"
|
|
#include "common.h"
|
|
#include "mutex.h"
|
|
#include "thread_annotations.h"
|
|
|
|
#include <limits.h> // for PTHREAD_DESTRUCTOR_ITERATIONS
|
|
#include <pthread.h>
|
|
|
|
// With some build setups, this might still not be defined.
|
|
#ifndef PTHREAD_DESTRUCTOR_ITERATIONS
|
|
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
|
|
#endif
|
|
|
|
namespace scudo {
|
|
|
|
template <class Allocator> struct alignas(SCUDO_CACHE_LINE_SIZE) TSD {
|
|
using ThisT = TSD<Allocator>;
|
|
u8 DestructorIterations = 0;
|
|
|
|
void init(Allocator *Instance) NO_THREAD_SAFETY_ANALYSIS {
|
|
DCHECK_EQ(DestructorIterations, 0U);
|
|
DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
|
|
Instance->initAllocator(&SizeClassAllocator);
|
|
DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS;
|
|
}
|
|
|
|
inline bool tryLock() NO_THREAD_SAFETY_ANALYSIS {
|
|
if (Mutex.tryLock()) {
|
|
atomic_store_relaxed(&Precedence, 0);
|
|
return true;
|
|
}
|
|
if (atomic_load_relaxed(&Precedence) == 0)
|
|
atomic_store_relaxed(
|
|
&Precedence,
|
|
static_cast<uptr>(getMonotonicTime() >> FIRST_32_SECOND_64(16, 0)));
|
|
return false;
|
|
}
|
|
inline void lock() NO_THREAD_SAFETY_ANALYSIS {
|
|
atomic_store_relaxed(&Precedence, 0);
|
|
Mutex.lock();
|
|
}
|
|
inline void unlock() NO_THREAD_SAFETY_ANALYSIS { Mutex.unlock(); }
|
|
inline uptr getPrecedence() { return atomic_load_relaxed(&Precedence); }
|
|
|
|
void commitBack(Allocator *Instance) { Instance->commitBack(this); }
|
|
|
|
// As the comments attached to `getCache()`, the TSD doesn't always need to be
|
|
// locked. In that case, we would only skip the check before we have all TSDs
|
|
// locked in all paths.
|
|
void assertLocked(bool BypassCheck) ASSERT_CAPABILITY(Mutex) {
|
|
if (SCUDO_DEBUG && !BypassCheck)
|
|
Mutex.assertHeld();
|
|
}
|
|
|
|
// Ideally, we may want to assert that all the operations on
|
|
// Cache/QuarantineCache always have the `Mutex` acquired. However, the
|
|
// current architecture of accessing TSD is not easy to cooperate with the
|
|
// thread-safety analysis because of pointer aliasing. So now we just add the
|
|
// assertion on the getters of Cache/QuarantineCache.
|
|
//
|
|
// TODO(chiahungduan): Ideally, we want to do `Mutex.assertHeld` but acquiring
|
|
// TSD doesn't always require holding the lock. Add this assertion while the
|
|
// lock is always acquired.
|
|
typename Allocator::SizeClassAllocatorT &getSizeClassAllocator()
|
|
REQUIRES(Mutex) {
|
|
return SizeClassAllocator;
|
|
}
|
|
typename Allocator::QuarantineCacheT &getQuarantineCache() REQUIRES(Mutex) {
|
|
return QuarantineCache;
|
|
}
|
|
|
|
private:
|
|
HybridMutex Mutex;
|
|
atomic_uptr Precedence = {};
|
|
|
|
typename Allocator::SizeClassAllocatorT SizeClassAllocator GUARDED_BY(Mutex);
|
|
typename Allocator::QuarantineCacheT QuarantineCache GUARDED_BY(Mutex);
|
|
};
|
|
|
|
} // namespace scudo
|
|
|
|
#endif // SCUDO_TSD_H_
|