ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
threadmanager.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_THREADMANAGER
9#define HPP_AWORX_ALIB_THREADS_MODEL_THREADMANAGER
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_AWORX_ALIB_THREADS_MODEL_MTHREAD)
28#endif
29
30
31//todo: This introduces a dependency to ALib Boxing.
32// Without this, we have no general enum type in piped commands.
33// What do we do? Shouldn't we drop all these minor modules and say
34// "Hey, this is ALib core: Characters, Boxing, Memory, Singletons, Time, Boxing, Enum, Strings "
35// Optional remain: Threads (due to performance if left out)
36// Optional remain: Resources (People dont need it?): Otherwise: if it was there...
37// Well, I do not know. If we keep the current resulution, we have a problem here.
38
39#if !defined (HPP_ALIB_BOXING_ENUM)
40# include "alib/boxing/enum.hpp"
41#endif
42
43#include <vector>
44#include <list>
45#include <map>
46#include <semaphore.h>
47
48namespace alib { namespace threads::model {
49
50class ThreadManager;
51
52
53
54/** ************************************************************************************************
55 * TODOX
56 **************************************************************************************************/
57class ThreadManager : public alib::Thread,
58 public alib::Sleeper,
59 public alib::Singleton<ThreadManager>
60{
61 protected:
62 struct TriggerEntry
63 {
64 Triggered * triggered;
65 alib::Ticks nextWakeup;
66 };
67
68
69 std::map<std::thread::id, MThread* > threadMap; // todo: make (monomem-)list. No need for searching
70
71 std::list<TriggerEntry> triggerList; // the registered clients // todo: make monomem list
72
73 int nameSize;
74 int tabSize;
75
76 bool mStoppedFlag;
77
78 private:
79
80 /**
81 * Private constructor to implement strict singleton concept.
82 */
84 ThreadManager();
86
87 public:
89 virtual ~ThreadManager() override;
90
92 virtual void Run() override;
93
95 bool IsStopped() { return mStoppedFlag; }
96
97 /**
98 * Add a managed thread.
99 * @param thread The thread to add.
100 */
102 void Add( MThread& thread );
103
104 /**
105 * Remove a managed thread.
106 * @param thread The thread to remove.
107 */
109 bool Remove( MThread& thread );
110
111 /**
112 * Add a thread to be triggered.
113 * @param triggered The object to be triggered.
114 */
116 void AddTriggered( Triggered& triggered);
117
118 /**
119 * Remove a previously added triggered object.
120 * @param triggered The object to be removed from the list.
121 */
123 void RemoveTriggered( Triggered& triggered);
124
126 void StopAndTerminateAll();
127
128 public:
129
130 protected:
131 /**
132 * This method is overwritten as protected, and thus removed from the interface.
133 * The thread manager should be stopped and terminated using #StopAndTerminateAll.
134 */
136 virtual void Terminate() override { Thread::Terminate(); }
137};
138
139
140} // namespace alib[::threads::model]
141
142/// Type alias in namespace #alib.
143using MThread = alib::threads::model::MThread;
144
145/// Type alias in namespace #alib.
146using ThreadManager = alib::threads::model::ThreadManager;
147
148
149} // namespace [alib]
150
151
152#endif // !defined(ALIB_DOX)
153
154#endif // HPP_AWORX_ALIB_THREADS_MODEL_THREADMANAGER
#define ALIB_API
Definition alib.hpp:538
@ Add
Arithmetic addition ('+'). Precedence 800.
@ Remove
Denotes removals.
ALIB_API HashMap< std::thread::id, Thread * > threadMap
Definition alib.cpp:57