[libcxx-commits] [libcxxabi] f011a53 - [libcxxabi] Added convenience classes to cxa_guard
Daniel McIntosh via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 12 14:31:40 PST 2022
Author: Daniel McIntosh
Date: 2022-01-12T17:31:36-05:00
New Revision: f011a53c148a8095e0bb63a92a0a640db16d1d54
URL: https://github.com/llvm/llvm-project/commit/f011a53c148a8095e0bb63a92a0a640db16d1d54
DIFF: https://github.com/llvm/llvm-project/commit/f011a53c148a8095e0bb63a92a0a640db16d1d54.diff
LOG: [libcxxabi] Added convenience classes to cxa_guard
This is the 5th of 5 changes to overhaul cxa_guard.
See D108343 for what the final result will be.
Depends on D115368
Reviewed By: ldionne, #libc_abi
Differential Revision: https://reviews.llvm.org/D115369
Added:
Modified:
libcxxabi/src/cxa_guard_impl.h
libcxxabi/test/guard_test_basic.pass.cpp
Removed:
################################################################################
diff --git a/libcxxabi/src/cxa_guard_impl.h b/libcxxabi/src/cxa_guard_impl.h
index d8d991be062f..c789bae47621 100644
--- a/libcxxabi/src/cxa_guard_impl.h
+++ b/libcxxabi/src/cxa_guard_impl.h
@@ -589,6 +589,26 @@ struct GuardObject {
}
};
+//===----------------------------------------------------------------------===//
+// Convenience Classes
+//===----------------------------------------------------------------------===//
+
+/// NoThreadsGuard - Manages initialization without performing any inter-thread
+/// synchronization.
+using NoThreadsGuard = GuardObject<InitByteNoThreads>;
+
+/// GlobalMutexGuard - Manages initialization using a global mutex and
+/// condition variable.
+template <class Mutex, class CondVar, Mutex& global_mutex, CondVar& global_cond,
+ uint32_t (*GetThreadID)() = PlatformThreadID>
+using GlobalMutexGuard = GuardObject<InitByteGlobalMutex<Mutex, CondVar, global_mutex, global_cond, GetThreadID>>;
+
+/// FutexGuard - Manages initialization using atomics and the futex syscall for
+/// waiting and waking.
+template <void (*Wait)(int*, int) = PlatformFutexWait, void (*Wake)(int*) = PlatformFutexWake,
+ uint32_t (*GetThreadIDArg)() = PlatformThreadID>
+using FutexGuard = GuardObject<InitByteFutex<Wait, Wake, GetThreadIDArg>>;
+
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
@@ -605,23 +625,20 @@ enum class Implementation { NoThreads, GlobalMutex, Futex };
template <Implementation Impl>
struct SelectImplementation;
-/// Manage initialization without performing any inter-thread synchronization.
template <>
struct SelectImplementation<Implementation::NoThreads> {
- using type = GuardObject<InitByteNoThreads>;
+ using type = NoThreadsGuard;
};
-/// Manage initialization using a global mutex and condition variable.
template <>
struct SelectImplementation<Implementation::GlobalMutex> {
- using type = GuardObject<InitByteGlobalMutex<LibcppMutex, LibcppCondVar, GlobalStatic<LibcppMutex>::instance,
- GlobalStatic<LibcppCondVar>::instance, PlatformThreadID>>;
+ using type = GlobalMutexGuard<LibcppMutex, LibcppCondVar, GlobalStatic<LibcppMutex>::instance,
+ GlobalStatic<LibcppCondVar>::instance, PlatformThreadID>;
};
-/// Manage initialization using atomics and the futex syscall for waiting and waking.
template <>
struct SelectImplementation<Implementation::Futex> {
- using type = GuardObject<InitByteFutex<PlatformFutexWait, PlatformFutexWake, PlatformThreadID>>;
+ using type = FutexGuard<PlatformFutexWait, PlatformFutexWake, PlatformThreadID>;
};
// TODO(EricWF): We should prefer the futex implementation when available. But
diff --git a/libcxxabi/test/guard_test_basic.pass.cpp b/libcxxabi/test/guard_test_basic.pass.cpp
index aa243cc8f749..2ad8e1122247 100644
--- a/libcxxabi/test/guard_test_basic.pass.cpp
+++ b/libcxxabi/test/guard_test_basic.pass.cpp
@@ -119,14 +119,13 @@ int main(int, char**) {
{
#if defined(_LIBCXXABI_HAS_NO_THREADS)
static_assert(CurrentImplementation == Implementation::NoThreads, "");
- static_assert(std::is_same<SelectedImplementation, GuardObject<InitByteNoThreads>>::value, "");
+ static_assert(std::is_same<SelectedImplementation, NoThreadsGuard>::value, "");
#else
static_assert(CurrentImplementation == Implementation::GlobalMutex, "");
- static_assert(
- std::is_same<SelectedImplementation,
- GuardObject<InitByteGlobalMutex<LibcppMutex, LibcppCondVar, GlobalStatic<LibcppMutex>::instance,
- GlobalStatic<LibcppCondVar>::instance>>>::value,
- "");
+ static_assert(std::is_same<SelectedImplementation,
+ GlobalMutexGuard<LibcppMutex, LibcppCondVar, GlobalStatic<LibcppMutex>::instance,
+ GlobalStatic<LibcppCondVar>::instance>>::value,
+ "");
#endif
}
{
@@ -139,17 +138,16 @@ int main(int, char**) {
}
}
{
- Tests<uint32_t, GuardObject<InitByteNoThreads>>::test();
- Tests<uint64_t, GuardObject<InitByteNoThreads>>::test();
+ Tests<uint32_t, NoThreadsGuard>::test();
+ Tests<uint64_t, NoThreadsGuard>::test();
}
{
- using MutexImpl =
- GuardObject<InitByteGlobalMutex<NopMutex, NopCondVar, global_nop_mutex, global_nop_cond, MockGetThreadID>>;
+ using MutexImpl = GlobalMutexGuard<NopMutex, NopCondVar, global_nop_mutex, global_nop_cond, MockGetThreadID>;
Tests<uint32_t, MutexImpl>::test();
Tests<uint64_t, MutexImpl>::test();
}
{
- using FutexImpl = GuardObject<InitByteFutex<&NopFutexWait, &NopFutexWake, &MockGetThreadID>>;
+ using FutexImpl = FutexGuard<&NopFutexWait, &NopFutexWake, &MockGetThreadID>;
Tests<uint32_t, FutexImpl>::test();
Tests<uint64_t, FutexImpl>::test();
}
More information about the libcxx-commits
mailing list