ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
mthread.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_threads of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2019 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_AWORX_ALIB_THREADS_MODEL_MTHREAD
9#define HPP_AWORX_ALIB_THREADS_MODEL_MTHREAD
10
11
12#if !defined(ALIB_DOX) // todo remove
13
14#if !defined(HPP_ALIB_THREADS_SLEEPER)
16#endif
17
18#if !defined (HPP_ALIB_THREADS_THREADLOCK)
20#endif
21
22#if !defined (HPP_AWORX_ALIB_THREADS_MODEL_TRIGGERED)
24#endif
25
26#if !defined (HPP_ALIB_BOXING_ENUM)
27# include "alib/boxing/enum.hpp"
28#endif
29
30#include <vector>
31#include <list>
32#include <map>
33#include <semaphore.h>
34
35namespace alib { namespace threads::model {
36
37class TRequest;
38class ThreadManager;
39
40/** ************************************************************************************************
41 * TODOX
42 * Implements a distinct thread communication model...with priority queues...
43 **************************************************************************************************/
44class MThread : public alib::Thread
45{
46 friend class ThreadManager;
47
48 protected:
49 enum class InternalCommands
50 {
51 NoCommand , ///< Returned when peeked pipe is empty.
52 Stop , ///< Stop this thread.
53 };
54
55
56 /**
57 * A priority queue that holds the commands to be processed.
58 * Commands are of type internal commands
59 */
60 class CmdPipe : public alib::Sleeper
61 {
62 friend class ThreadManager;
63 public:
64
65 /**
66 * Commands that a thread may receive. Values of this type are pushed (copied) and popped
67 * into the \ref CmdPipe.
68 */
69 class Command
70 {
71 public:
72 /** Constructor. */
73 Command(alib::Enum pCmd, int pPriority, Box pData, Box pData2= nullptr)
74 {
75 cmd= pCmd;
76 priority= pPriority;
77 data= pData;
78 data2= pData2;
79 }
80
81 /** ************************************************************************************
82 * Deleted copy constructor.
83 **************************************************************************************/
84 Command( const Command& ) = default;
85
86 virtual ~Command() {}
87
88 /** Simply compares fields #priority of this and the \b rhs object.
89 * \param rhs The object to compare this object's priority with.
90 * \c true if this object's priority is lower or equal as the priority of \b other. */
91 bool operator<=(const Command& rhs ) const { return (priority<= rhs.priority); }
92
93 /** Simply compares fields #priority of this and the \b rhs object.
94 * \param rhs The object to compare this object's priority with.
95 * \c true if this object's priority is higher as the priority of \b other. */
96 bool operator> (const Command& rhs ) const { return ( priority < rhs.priority); }
97
98 alib::Enum cmd; ///< Command identifier.
99 int priority; ///< Priority of message.
100 Box data; ///< Data packet transferred with the message.
101 Box data2; ///< Data packet transferred with the message.
102
103 }; // class Command
104
105 protected:
106 std::list<Command> pipe;
107
108
109 public:
110 alib::uinteger usageCounter;
111
112 public:
113 /** Constructor. */
114 CmdPipe() : usageCounter(0) {}
115
116 /** Destructor. */
117 ~CmdPipe() {}
118
119
120 public:
121 void Push(alib::Enum cmd, int priority, Box data= nullptr, Box data2= nullptr);
122 Command Pop();
123
124 int PeekSize() { return static_cast<int>( pipe.size()); }
125 Command PeekCmd(int n);
126
127 };
128
129
130 //----------------------------------------------------------------------------------------------
131 // The MThread part
132 // Todo: The whole pipe should probably be a superclass (multi inheritance) or
133 // just being integrated here. The advantage of the latter could be that the pipe
134 // "knows" the thread internals, e.g. a future monomem
135 //----------------------------------------------------------------------------------------------
136 MThread(const alib::String& threadName);
137 virtual ~MThread() override;
138
140 virtual
141 bool IsStopped() { return stoppedFlag; }
142 void Stop();
143
144 public:
145 virtual void Run() final override;
146
147 protected:
148
149 /** ****************************************************************************************
150 * This method is overwritten as protected, and thus removed from the interface.
151 * <b>MThread</b>s are started by registering them with the #link ThreadManager
152 * singleton.
153 ******************************************************************************************/
155 virtual void Start() override { Thread::Start(); }
156
157 protected:
158 virtual void processCommand( CmdPipe::Command& cmd ) =0;
159
160 // thread management:
161 alib::Ticks lastStatusChange; // when the thread started the last task
162
163
164 // protected fields
165 protected:
166 bool stoppedFlag;
167 CmdPipe cmdPipe; ///< The thread's communication tool.
168
169}; // class MThread
170
171
172} // namespace alib[::threads::model]
173
174/// Type alias in namespace #alib.
175using MThread = alib::threads::model::MThread;
176
177} // namespace [alib]
178
179
180#endif // !defined(ALIB_DOX)
181
182#endif // HPP_AWORX_ALIB_THREADS_MODEL_MTHREAD
#define ALIB_API
Definition alib.hpp:538
constexpr bool operator<=(TEnum lhs, typename std::underlying_type< TEnum >::type rhs) noexcept(true)
Definition alib.cpp:57
lang::uinteger uinteger
Type alias in namespace alib.
Definition integers.hpp:289
boxing::Box Box
Type alias in namespace alib.