Commit 73ed4960 by FritzFlorian

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.
parent 837d8e4e
# 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
......
// 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
......@@ -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.
......
......@@ -7,16 +7,7 @@
#define PLS_THREAD_H
#include <functional>
// platform specific includes
#include "pls/internal/base/choose_threading.h"
#ifdef PLS_USING_PTHREADS
#include <pthread.h>
#include <iostream>
#elif PLS_USING_CPP_THREADS
#include <thread>
#endif
#include <pthread.h>
namespace pls {
namespace internal {
......@@ -26,42 +17,22 @@ namespace pls {
class this_thread {
template<typename Function, typename State>
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<typename T>
static T* state() {
#ifdef PLS_USING_PTHREADS
return reinterpret_cast<T*>(pthread_getspecific(local_storage_key_));
#endif
#ifdef PLS_USING_CPP_THREADS
return reinterpret_cast<T*>(local_storage_);
#endif
}
template<typename T>
static void set_state(const T& state) {
#ifdef PLS_USING_PTHREADS
*reinterpret_cast<T*>(pthread_getspecific(local_storage_key_)) = state;
#endif
#ifdef PLS_USING_CPP_THREADS
*reinterpret_cast<T*>(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*>(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<thread*>(my_thread_pointer);
this_thread::local_storage_ = reinterpret_cast<void*>(&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
......
......@@ -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)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment