ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
jobs.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_threadmodel of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace threadmodel {
9
10class DedicatedWorker;
11class ThreadPool;
12class DWManager;
13
14//==================================================================================================
15/// \attention This class belongs to the module \alib_threadmodel, which is not in a stable and
16/// consistent state, yet.
17/// Also, this type is considered experimental.
18///
19/// Defines jobs which are scheduled with instances of types #"ThreadPool" and
20/// #"DedicatedWorker".<br>
21///
22/// The type is virtual and designed to be the base type for custom <em>"jobs"</em>.
23/// It holds a job-ID of type <c>std::type_info</c> which is used to identify the job type with
24/// processing.
25/// This ID usually represents - but does not need to! - the derived type.
26/// By including the ID in the job, one derived job-type object can be used with different
27/// job definitions.
28/// For example, built-in derived type #"JPromise" may be used as the "shared data"
29/// between the sender and the processing thread.
30/// The constructor of #"%JPromise" accepts a type ID to store.
31///
32/// This explains that class Job has two main aspects. It
33/// - provides information to a worker thread about what is to be done.
34/// - provides shared data between the sender and the worker.
35///
36/// Instances of this class are always #"TPoolAllocator;pool-allocated".
37/// Allocation, construction, destruction and deletion are always under the control of either
38/// type #"ThreadPool" or #"DedicatedWorker".
39/// Pool allocation requires passing the object size with deletion.
40/// For this, derived types that add new field members have to override method #"SizeOf" in a way
41/// described with the documentation of that virtual method, to return the correct size of the
42/// derived type. (All details on this topic are
43/// #"alib_contmono_poolallocator_allocinfo;found here").
44///
45/// Virtual method #"Do" has to be overridden in case a job is to be scheduled with the class
46/// #"ThreadPool".
47/// In case a derived type is (exclusively) used with type #"DedicatedWorker",
48/// it is up to the user of this module whether method #"Do" is implemented or not.
49/// If not, then a custom type derived from #"%DedicatedWorker" has to override the method
50/// #"DedicatedWorker::process" and handle the job type there.
51///
52/// @see The Programmer's Manual of this module \alib_threadmodel for detailed information.
53//==================================================================================================
54struct Job {
55 /// The identifier of the job. Any custom type can be given. Jobs with complex
56 /// shared data types, which are used only for this specific job, usually use the
57 /// shared data type itself here.
58 const std::type_info& ID;
59
60 /// Protected constructor.
61 /// @param id Assigned to #"ID".
62 Job( const std::type_info& id )
63 : ID (id) {}
64
65 /// Protected destructor.
66 virtual ~Job() =default;
67
68 /// This method is called by the worker thread in the case this job was scheduled
69 /// for deferred deletion using #"ThreadPool::DeleteJobDeferred;*"
70 /// or #"DedicatedWorker::DeleteJobDeferred;*".
71 /// The method is intended to prepare the deletion of this job without further actions
72 /// that the sender of a job otherwise would perform.
73 ///
74 /// Overriding this method must be considered well. It is up to the contract between
75 /// the caller and the implementation of a custom worker, how to handle this situation.
76 ///
77 /// As a sample, within the implementation of this module, it is used with class
78 /// #"JPromise" to disable the warnings of deleting an unfulfilled and
79 /// un-awaited promise in debug-compilations.
80 ///
81 /// @note This method is called by the worker thread, so it is not allowed to be called
82 /// by any other methods of the worker or caller
83 virtual void PrepareDeferredDeletion() {}
84
85 /// Virtual method that returns the size of the type.<br>
86 /// Derived types that add fields or use multi-inheritance, have to override this virtual
87 /// function like done in this foundational version, but providing the custom type:
88 /// virtual size_t SizeOf() override
89 /// {
90 /// return sizeof(MyJobType);
91 /// }
92 ///
93 /// @return The sizeof this type. Derived types have to return their size.
94 virtual size_t SizeOf() { return sizeof(Job); }
95
96 /// Tests if this job's type ID equals the explicitly provided (non-deducible) template
97 /// type \p{TOther}.
98 /// @return \c true, if this instance represents job \p{TOther}, \c false otherwise.
99 template<typename TOther>
100 bool Is() { return ID == typeid(TOther); }
101
102 /// Tests if this instance is not initialized.
103 /// @return \c true, if this instance represents non-job-type <c>void</c>, and
104 /// \c false otherwise.
105 template<typename TOther>
106 bool IsNull() { return ID == typeid(void); }
107
108 /// Returns the shared data dynamically cast to provided (non-deducible) template type
109 /// \p{TJob}.<br>
110 /// With debug-compilations it is asserted that the requested job type \p{TJob} matches
111 /// to what it really is.
112 ///
113 /// @tparam TJob The true type <c>this</c> points to.
114 /// @return A reference to the shared data stored with this job.
115 template<typename TJob>
116 TJob& Cast() {
117 ALIB_ASSERT_ERROR( typeid(TJob) == ID, "TMOD",
118 "Bad job casting.\n"
119 " Job type: <{}>\n"
120 " Requested type: <{}>", &ID, &typeid(TJob) )
121
122 auto* result= dynamic_cast<TJob*>(this);
123
124 ALIB_ASSERT_ERROR( result != nullptr, "TMOD",
125 "Job casting failed.\n"
126 " Job type: <{}>\n"
127 " Requested type: <{}>", &ID, &typeid(TJob) )
128 return *result;
129 }
130
131 /// Virtual function that may be overridden to execute the job when the job is executed by an
132 /// #"DedicatedWorker".
133 /// In case it is executed by a #"ThreadPool" it has to be overridden.
134 ///
135 /// @return This default implementation returns \c false, which indicates that it is not
136 /// implemented.
137 /// Implementations need to return \c true.
138 virtual bool Do() { return false; }
139
140}; // struct Job
141
142/// A simple encapsulated promise.
143struct JPromise : public Job
144 , public Promise
145{
146 /// Constructor.
147 /// @param id The type-info of the derived type. Passed to parent class #"%Job".
148 JPromise(const std::type_info& id ) : Job(id) {}
149
150 /// Virtual destructor.
151 virtual ~JPromise() override =default;
152
153 /// Overrides the parent function as necessary.
154 /// @return The sizeof this derived type.
155 virtual size_t SizeOf() override { return sizeof(JPromise); }
156
157 #if ALIB_DEBUG
158 /// Overrides the parent function only with debug-compilations.
159 /// Avoids warnings about destroying unused promise
161 #endif
162};
163
164
165/// Possible priorities of jobs assigned to an #"DedicatedWorker".
166enum class Priority {
167 Lowest = 0, ///< As the name indicates.
168 DeferredDeletion = 500, ///< As the name indicates.
169 Low = 1000, ///< As the name indicates.
170 Standard = 2000, ///< As the name indicates.
171 High = 3000, ///< As the name indicates.
172 Highest = 4000, ///< As the name indicates.
173};
174
175} // namespace alib[::threadmodel]
176
177/// Type alias in namespace #"%alib".
179
180/// Type alias in namespace #"%alib".
182
183} // namespace [alib]
#define ALIB_EXPORT
#define ALIB_ASSERT_ERROR(cond, domain,...)
void DbgOmitDestructionWarning()
Definition promise.hpp:109
Promise()
Default constructor.
Definition promise.hpp:89
Priority
Possible priorities of jobs assigned to an #"DedicatedWorker".
Definition jobs.hpp:166
@ DeferredDeletion
As the name indicates.
Definition jobs.hpp:168
@ Low
As the name indicates.
Definition jobs.hpp:169
@ Highest
As the name indicates.
Definition jobs.hpp:172
@ High
As the name indicates.
Definition jobs.hpp:171
@ Lowest
As the name indicates.
Definition jobs.hpp:167
@ Standard
As the name indicates.
Definition jobs.hpp:170
Definition alox.cpp:14
threadmodel::JPromise JPromise
Type alias in namespace #"%alib".
Definition jobs.hpp:181
threadmodel::Job Job
Type alias in namespace #"%alib".
Definition jobs.hpp:178
A simple encapsulated promise.
Definition jobs.hpp:145
virtual ~JPromise() override=default
Virtual destructor.
virtual size_t SizeOf() override
Definition jobs.hpp:155
virtual void PrepareDeferredDeletion() override
Definition jobs.hpp:160
JPromise(const std::type_info &id)
Definition jobs.hpp:148
virtual void PrepareDeferredDeletion()
Definition jobs.hpp:83
virtual size_t SizeOf()
Definition jobs.hpp:94
const std::type_info & ID
Definition jobs.hpp:58
Job(const std::type_info &id)
Definition jobs.hpp:62
virtual ~Job()=default
Protected destructor.
virtual bool Do()
Definition jobs.hpp:138