From 73ed4960e0f83f09edfab36d21c5faa426a1de15 Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Wed, 20 Mar 2019 10:53:52 +0100 Subject: [PATCH] Remove std::thread support, only use pthreads for now. See NOTES.md for details, basically std::thread can use dynamic memory allocation internaly and we want to prevent that. --- lib/pls/CMakeLists.txt | 10 ---------- lib/pls/include/pls/internal/base/choose_threading.h | 13 ------------- lib/pls/include/pls/internal/base/prohibit_new.h | 2 +- lib/pls/include/pls/internal/base/thread.h | 62 +------------------------------------------------------------- lib/pls/src/internal/base/thread.cpp | 5 ----- 5 files changed, 2 insertions(+), 90 deletions(-) delete mode 100644 lib/pls/include/pls/internal/base/choose_threading.h diff --git a/lib/pls/CMakeLists.txt b/lib/pls/CMakeLists.txt index a7e0624..270c8a3 100644 --- a/lib/pls/CMakeLists.txt +++ b/lib/pls/CMakeLists.txt @@ -1,20 +1,10 @@ # List all required files here (cmake best practice to NOT automate this step!) add_library(pls STATIC src/library.cpp include/pls/library.h - include/pls/internal/base/choose_threading.h src/internal/base/spin_lock.cpp include/pls/internal/base/spin_lock.h src/internal/base/thread.cpp include/pls/internal/base/thread.h include/pls/internal/base/prohibit_new.h) -# Settings for our project... -# ...pthreads or C++ 11 threads -option(USING_PTHREADS "Build the tests" ON) -if(USING_PTHREADS) - target_compile_definitions(pls PUBLIC PLS_USING_PTHREADS) -else() - target_compile_definitions(pls PUBLIC PLS_USING_CPP_THREADS) -endif() - # Add everything in `./include` to be in the include path of this project target_include_directories(pls PUBLIC diff --git a/lib/pls/include/pls/internal/base/choose_threading.h b/lib/pls/include/pls/internal/base/choose_threading.h deleted file mode 100644 index 05124d7..0000000 --- a/lib/pls/include/pls/internal/base/choose_threading.h +++ /dev/null @@ -1,13 +0,0 @@ -// Make sure exactly ONE threading library is active for -// all of our threading primitives -#ifndef PLS_CHOOSE_THREADING_H -#define PLS_CHOOSE_THREADING_H - -#if defined(PLS_USING_PTHREADS) && defined(PLS_USING_CPP_THREADS) - #error "Please activate exactly one threading library (currently both are activated)!" -#endif -#if !defined(PLS_USING_PTHREADS) && !defined(PLS_USING_CPP_THREADS) - #error "Please activate exactly one threading library (currently none are activated)!" -#endif - -#endif //PLS_CHOOSE_THREADING_H diff --git a/lib/pls/include/pls/internal/base/prohibit_new.h b/lib/pls/include/pls/internal/base/prohibit_new.h index baf8e01..4ea59cc 100644 --- a/lib/pls/include/pls/internal/base/prohibit_new.h +++ b/lib/pls/include/pls/internal/base/prohibit_new.h @@ -10,7 +10,7 @@ // Comment this out and set a debug point in the std::cout // line to find out where the new is located in case you // get an linker error. -//#define NEW_LINK_ERROR +#define NEW_LINK_ERROR #ifdef NEW_LINK_ERROR // This will cause a linker error if new is used in the code. diff --git a/lib/pls/include/pls/internal/base/thread.h b/lib/pls/include/pls/internal/base/thread.h index 7aebfdc..c4ed39b 100644 --- a/lib/pls/include/pls/internal/base/thread.h +++ b/lib/pls/include/pls/internal/base/thread.h @@ -7,16 +7,7 @@ #define PLS_THREAD_H #include - -// platform specific includes -#include "pls/internal/base/choose_threading.h" -#ifdef PLS_USING_PTHREADS - #include -#include - -#elif PLS_USING_CPP_THREADS - #include -#endif +#include namespace pls { namespace internal { @@ -26,42 +17,22 @@ namespace pls { class this_thread { template friend class thread; -#ifdef PLS_USING_PTHREADS static pthread_key_t local_storage_key_; static bool local_storage_key_initialized_; -#endif -#ifdef PLS_USING_CPP_THREADS - static thread_local void* local_storage_; -#endif public: static void yield() { -#ifdef PLS_USING_PTHREADS pthread_yield(); -#endif -#ifdef PLS_USING_CPP_THREADS - std::this_thread::yield(); -#endif } template static T* state() { -#ifdef PLS_USING_PTHREADS return reinterpret_cast(pthread_getspecific(local_storage_key_)); -#endif -#ifdef PLS_USING_CPP_THREADS - return reinterpret_cast(local_storage_); -#endif } template static void set_state(const T& state) { -#ifdef PLS_USING_PTHREADS *reinterpret_cast(pthread_getspecific(local_storage_key_)) = state; -#endif -#ifdef PLS_USING_CPP_THREADS - *reinterpret_cast(local_storage_) = state; -#endif } }; @@ -74,14 +45,8 @@ namespace pls { State state_; // Keep handle to native implementation -#ifdef PLS_USING_PTHREADS pthread_t pthread_thread_; -#endif -#ifdef PLS_USING_CPP_THREADS - std::thread std_thread_; -#endif -#ifdef PLS_USING_PTHREADS static void* start_pthread_internal(void* thread_pointer) { auto my_thread = reinterpret_cast(thread_pointer); pthread_setspecific(this_thread::local_storage_key_, (void*)&my_thread->state_); @@ -89,10 +54,7 @@ namespace pls { pthread_exit(nullptr); } -#endif - public: -#ifdef PLS_USING_PTHREADS explicit thread(const Function& function, const State& state): function_{function}, state_{state}, @@ -106,31 +68,9 @@ namespace pls { pthread_create(&pthread_thread_, nullptr, start_pthread_internal, (void *)(this)); } -#endif -#ifdef PLS_USING_CPP_THREADS - explicit thread(const Function& function, const State& state): - function_{function}, - state_{state}, - std_thread_{} {}; - - static void start_internal(void* my_thread_pointer) { - auto my_thread = reinterpret_cast(my_thread_pointer); - this_thread::local_storage_ = reinterpret_cast(&my_thread->state_); - my_thread->function_(); - } - - void start() { - std_thread_ = std::thread(start_internal, (void*)this); - } -#endif public: void join() { -#ifdef PLS_USING_PTHREADS pthread_join(pthread_thread_, nullptr); -#endif -#ifdef PLS_USING_CPP_THREADS - std_thread_.join(); -#endif } // make object move only diff --git a/lib/pls/src/internal/base/thread.cpp b/lib/pls/src/internal/base/thread.cpp index 4cc07d9..b2cd8d8 100644 --- a/lib/pls/src/internal/base/thread.cpp +++ b/lib/pls/src/internal/base/thread.cpp @@ -3,13 +3,8 @@ namespace pls { namespace internal { namespace base { -#ifdef PLS_USING_PTHREADS bool this_thread::local_storage_key_initialized_ = false; pthread_key_t this_thread::local_storage_key_; -#endif -#ifdef PLS_USING_CPP_THREADS - thread_local void* this_thread::local_storage_; -#endif // implementation in header (C++ templating) } } -- libgit2 0.26.0