diff --git a/libc/cmake/modules/LLVMLibCRules.cmake b/libc/cmake/modules/LLVMLibCRules.cmake --- a/libc/cmake/modules/LLVMLibCRules.cmake +++ b/libc/cmake/modules/LLVMLibCRules.cmake @@ -355,7 +355,7 @@ ${LIBC_UNITTEST_DEPENDS} ) - target_link_libraries(${target_name} PRIVATE LibcUnitTest) + target_link_libraries(${target_name} PRIVATE LibcUnitTest libc_test_utils) add_custom_command( TARGET ${target_name} diff --git a/libc/utils/CMakeLists.txt b/libc/utils/CMakeLists.txt --- a/libc/utils/CMakeLists.txt +++ b/libc/utils/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory(HdrGen) add_subdirectory(UnitTest) add_subdirectory(benchmarks) +add_subdirectory(testutils) diff --git a/libc/utils/testutils/CMakeLists.txt b/libc/utils/testutils/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/CMakeLists.txt @@ -0,0 +1,11 @@ + +add_library( + libc_test_utils + Support.h + Thread.cpp + Thread.h +) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) + target_sources(libc_test_utils PRIVATE ${LIBC_TARGET_OS}/sleep.cpp) +endif() diff --git a/libc/utils/testutils/Support.h b/libc/utils/testutils/Support.h new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/Support.h @@ -0,0 +1,16 @@ +//===------------ Declaration of test support utilities ---------*- 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 +// +//===----------------------------------------------------------------------===// + +namespace __llvm_libc { +namespace testutils { + +extern "C" void abort(); +int sleep(unsigned int seconds); + +} // namespace testutils +} // namespace __llvm_libc diff --git a/libc/utils/testutils/Thread.h b/libc/utils/testutils/Thread.h new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/Thread.h @@ -0,0 +1,63 @@ +//===---- A Thread class which is a wrapper over std::thread ----*- 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 LLVM_LIBC_UTILS_TESTING_THREAD_H +#define LLVM_LIBC_UTILS_TESTING_THREAD_H + +namespace __llvm_libc { +namespace testutils { + +class Thread { +private: + class ClassWithCall { + public: + virtual ~ClassWithCall() {} + virtual void call() = 0; + }; + + class Caller { + private: + ClassWithCall *Callee; + + public: + explicit Caller(ClassWithCall *C) : Callee(C) {} + + void operator()() { Callee->call(); } + }; + + template + class GenericClassWithCall : public ClassWithCall { + public: + explicit GenericClassWithCall(Callable C) : TheCallable(C) {} + + void call() override { TheCallable(); } + + private: + Callable TheCallable; + }; + + void startThread(Caller C); + +public: + template + Thread(Callable C) : WithCallObj(new GenericClassWithCall(C)) { + Caller TheCaller(WithCallObj); + startThread(TheCaller); + } + + void join(); + +private: + ClassWithCall *WithCallObj; + void *StdThreadPtr; +}; + +} // namespace testutils +} // namespace __llvm_libc + +#endif // LLVM_LIBC_UTILS_TESTING_THREAD_H diff --git a/libc/utils/testutils/Thread.cpp b/libc/utils/testutils/Thread.cpp new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/Thread.cpp @@ -0,0 +1,27 @@ +//===-- Implementation of the Thread class as a wrapper over std::thread --===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "Thread.h" + +#include + +namespace __llvm_libc { +namespace testutils { + +void Thread::startThread(Thread::Caller C) { + StdThreadPtr = new std::thread(C); +} + +void Thread::join() { + reinterpret_cast(StdThreadPtr)->join(); + delete reinterpret_cast(StdThreadPtr); + delete WithCallObj; +} + +} // namespace testutils +} // namespace __llvm_libc diff --git a/libc/utils/testutils/linux/sleep.cpp b/libc/utils/testutils/linux/sleep.cpp new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/linux/sleep.cpp @@ -0,0 +1,17 @@ +//===---------------- Wrapper over the linux sleep function ---------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +namespace __llvm_libc { +namespace testutils { + +int sleep(unsigned int Seconds) { return ::sleep(Seconds); } + +} // namespace testutils +} // namespace __llvm_libc