MEL
Microthread & Execution library
ThreadRunnable.h
1 #pragma once
2 /*
3  * SPDX-FileCopyrightText: 2022 Daniel Barrientos <danivillamanin@gmail.com>
4  *
5  * SPDX-License-Identifier: MIT
6  */
7 #include <core/Thread.h>
9 #include <tasking/Runnable.h>
11 // can alternate between custom "clasic" events to C++11 condition_variables
12 // which seems to get better performance at least on Windows platforms
13 #ifndef USE_CUSTOM_EVENT
14 #include <condition_variable>
15 #endif
16 
17 namespace mel
18 {
19  namespace tasking
20  {
34  class MEL_API ThreadRunnable
35  : public Runnable
37  ,
38  public std::enable_shared_from_this<ThreadRunnable>
40  {
41  public:
42  enum EThreadState
43  {
44  THREAD_INIT = binary<1>::value,
45  THREAD_RUNNING = binary<10>::value,
46  THREAD_SUSPENDED = binary<100>::value,
47  THREAD_FINISHING = binary<1000>::value,
48  THREAD_FINISHING_DONE = binary<10000>::value,
49  THREAD_FINISHED = binary<100000>::value
50  };
60  static std::shared_ptr<ThreadRunnable>
61  create( bool autoRun = true,
62  Runnable::RunnableCreationOptions opts = sDefaultOpts )
63  {
64  auto th = new ThreadRunnable( std::move( opts ) );
65  th->start();
66  std::shared_ptr<ThreadRunnable> result( th );
67  if ( !autoRun ) // really means auto pause if no autorun, always
68  // create running
69  result->suspend();
70  return result;
71  }
72  ~ThreadRunnable();
81  void start();
82  // void finish() override { terminate(0); }
83  // bool finished() override {return getState() == THREAD_FINISHED;
84  // }
90  bool join( unsigned int millis = 0xFFFFFFFF );
91  bool setAffinity( uint64_t aff )
92  {
93  return mThread->setAffinity( aff );
94  }
95 
107  unsigned int suspend();
116  unsigned int resume();
117  EThreadState getState() const { return mState; }
118  void terminate();
119  bool getTerminateRequest() { return mEnd; }
127 
128  private:
129  static Runnable::RunnableCreationOptions sDefaultOpts;
130  std::unique_ptr<Thread> mThread;
131 #ifdef USE_CUSTOM_EVENT
132  Event mWaitForTasks;
133 #else
134  volatile bool mSignaled;
135  std::condition_variable mWaitForTasksCond;
136  std::mutex mCondMut;
137 #endif
138  volatile bool mEnd; // request
139  EThreadState mState;
140  ::mel::core::Event mPauseEV;
141 
143  _processAwaken( std::shared_ptr<Process> p );
144  void _execute();
150  _suspendInternal( uint64_t millis, Process* proc ) noexcept;
151  void _signalWakeup();
152 
153  protected:
159  Runnable::RunnableCreationOptions opts = sDefaultOpts );
164  virtual void onStart(){};
171  virtual void onThreadStart() {}
176  virtual void onThreadEnd() {}
181  virtual void onJoined() {}
182  void onCycleEnd();
183 
187  void onPostTask( std::shared_ptr<Process> process ) override;
188  };
189 
190  } // namespace tasking
191 } // namespace mel
Definition: Event.h:25
Wrapper around std::thread to prive more abstractions.
Definition: Thread.h:33
A periodic task, implementing a microthread.
Definition: Process.h:107
A class representing a "running" task, with added functionality to post events requesting execution o...
Definition: Runnable.h:183
Thread with Runnable behaviour.
Definition: ThreadRunnable.h:40
void start()
Start Thread Internally calls Runnablle::processTask in an infinite loop, with some other check for p...
ThreadRunnable(Runnable::RunnableCreationOptions opts=sDefaultOpts)
Create a thread with an empty loop, that continuosly processes posted tasks.
static ThreadRunnable * getCurrentThreadRunnable()
bool join(unsigned int millis=0xFFFFFFFF)
Does a join on the underlying Thread.
virtual void onThreadStart()
Called one time at the start of the thread loop, so, already in this thread context Don't confuse wit...
Definition: ThreadRunnable.h:171
virtual void onThreadEnd()
Called when at the end of the thread loop.
Definition: ThreadRunnable.h:176
static std::shared_ptr< ThreadRunnable > create(bool autoRun=true, Runnable::RunnableCreationOptions opts=sDefaultOpts)
Create new ThreadRunnable.
Definition: ThreadRunnable.h:61
virtual void onJoined()
Called by join() when done.
Definition: ThreadRunnable.h:181
void onPostTask(std::shared_ptr< Process > process) override
virtual void onStart()
Called by start() function Children can override it to add custom behaviour.
Definition: ThreadRunnable.h:164
ECallbackResult
Type resturned by callbacks subscribed to CallbackSubscriptors.
Definition: CallbackSubscriptor.h:32
EGenericProcessResult
Result from functor used in a GenericProcess.
Definition: GenericProcessDefs.h:14
Definition: Callback_Impl.h:11
Definition: binary.h:9