.. _program_listing_file__tmp_ws_src_ecl_core_ecl_threads_include_ecl_threads_thread_pos.hpp: Program Listing for File thread_pos.hpp ======================================= |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/ecl_core/ecl_threads/include/ecl/threads/thread_pos.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /***************************************************************************** ** Ifdefs *****************************************************************************/ #ifndef ECL_THREADS_THREAD_POS_HPP_ #define ECL_THREADS_THREAD_POS_HPP_ /***************************************************************************** ** Platform Check *****************************************************************************/ #include #if defined(ECL_IS_POSIX) /***************************************************************************** ** Includes *****************************************************************************/ #include #include "thread_exceptions_pos.hpp" #include #include #include #include #include #include "priority_pos.hpp" /***************************************************************************** ** Namespaces *****************************************************************************/ namespace ecl { namespace threads { /***************************************************************************** ** Interface [ThreadTask] *****************************************************************************/ class ECL_LOCAL ThreadTaskBase { public: virtual ~ThreadTaskBase() {}; protected: ThreadTaskBase(const Priority& priority) : priority_level(priority) {}; ecl::Priority priority_level; }; template class ECL_LOCAL ThreadTask : public ThreadTaskBase { public: ThreadTask(const F &f, const Priority &priority) : ThreadTaskBase(priority), function(f) { ecl_compile_time_concept_check(ecl::NullaryFunction); }; virtual ~ThreadTask() {}; static void* EntryPoint(void *ptr_this) { ThreadTask< F, false > *ptr = static_cast< ThreadTask< F, false > * >(ptr_this); ecl::set_priority(ptr->priority_level); (ptr->function)(); delete ptr; ptr_this = NULL; return ptr_this; } private: F function; }; template class ECL_LOCAL ThreadTask : public ThreadTaskBase { public: ThreadTask(const F &f, const Priority &priority) : ThreadTaskBase(priority), function(f.reference()) { ecl_compile_time_concept_check(ecl::NullaryFunction< typename F::type>); }; virtual ~ThreadTask() {}; static void* EntryPoint(void *ptr_this) { ThreadTask< F, true > *ptr = static_cast< ThreadTask< F, true > * >(ptr_this); ecl::set_priority(ptr->priority_level); (ptr->function)(); delete ptr; ptr_this = NULL; return ptr_this; } private: typename F::type &function; }; } // namespace threads /***************************************************************************** ** Interface [Thread] *****************************************************************************/ class ECL_PUBLIC Thread { public: Thread() : thread_task(NULL), has_started(false), join_requested(false) { (void) schedule_parameters; } Thread(VoidFunction function, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); Error start(VoidFunction function, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); template Thread(void (C::*function)(), C &c, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); template Error start(void (C::*function)(), C &c, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); template Thread(const F &function, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); template Error start(const F &function, const Priority &priority = DefaultPriority, const long &stack_size = -1 ); virtual ~Thread(); bool isRunning() const { if ( thread_task == NULL ) { return false; } else { return true; } } void cancel(); void join(); private: pthread_t thread_handle; pthread_attr_t attrs; sched_param schedule_parameters; threads::ThreadTaskBase *thread_task; bool has_started; bool join_requested; void initialise(const long &stack_size); enum ThreadProperties { DefaultStackSize = -1 }; }; /***************************************************************************** ** Template Implementation [Thread] *****************************************************************************/ template Thread::Thread(void (C::*function)(), C &c, const Priority &priority, const long &stack_size) : thread_task(NULL), has_started(false), join_requested(false) { start(function, c, priority, stack_size); } template Thread::Thread(const F &function, const Priority &priority, const long &stack_size) : thread_task(NULL), has_started(false), join_requested(false) { start(function, priority, stack_size); } template Error Thread::start(void (C::*function)(), C &c, const Priority &priority, const long &stack_size) { if ( has_started ) { ecl_debug_throw(StandardException(LOC,BusyError,"The thread has already been started.")); return Error(BusyError); // if in release mode, gracefully fall back to return values. } else { has_started = true; } initialise(stack_size); thread_task = new threads::ThreadTask< BoundNullaryMemberFunction >(generateFunctionObject( function, c ), priority); int result = pthread_create(&(this->thread_handle), &(this->attrs), threads::ThreadTask< BoundNullaryMemberFunction >::EntryPoint, thread_task); pthread_attr_destroy(&attrs); if ( result != 0 ) { delete thread_task; thread_task = NULL; ecl_debug_throw(threads::throwPthreadCreateException(LOC,result)); return threads::handlePthreadCreateError(result); // if in release mode, gracefully fall back to return values. } return Error(NoError); } template Error Thread::start(const F &function, const Priority &priority, const long &stack_size) { if ( has_started ) { ecl_debug_throw(StandardException(LOC,BusyError,"The thread has already been started.")); return Error(BusyError); // if in release mode, gracefully fall back to return values. } else { has_started = true; } initialise(stack_size); thread_task = new threads::ThreadTask::value >(function, priority); int result = pthread_create(&(this->thread_handle), &(this->attrs), threads::ThreadTask::value>::EntryPoint, thread_task); pthread_attr_destroy(&attrs); if ( result != 0 ) { delete thread_task; thread_task = NULL; ecl_debug_throw(threads::throwPthreadCreateException(LOC,result)); return threads::handlePthreadCreateError(result); // if in release mode, gracefully fall back to return values. } return Error(NoError); } } // namespace ecl #endif /* ECL_IS_POSIX */ #endif /* ECL_THREADS_THREAD_POS_HPP_ */