ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
jobs.inl
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/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software 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 \alib{threadmodel;ThreadPool} and
20/// \alib{threadmodel;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 \alib{threadmodel;JPromise} may be used as the "shared data"
29/// between the sender and the processing thread.
30/// The constructor of \b %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 \alib{monomem;TPoolAllocator;pool-allocated}.
37/// Allocation, construction, destruction and deletion are always under the control of either
38/// type \alib{threadmodel;ThreadPool} or \alib{threadmodel;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/// \ref 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/// \alib{threadmodel;ThreadPool}.
47/// In case a derived type is (exclusively) used with type \alib{threadmodel;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 \b DedicatedWorker has to override the method
50/// \alib{threadmodel::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{
56 /// The identifier of the job. Any custom type can be given. Jobs with complex
57 /// shared data types, which are used only for this specific job, usually use the
58 /// shared data type itself here.
59 const std::type_info& ID;
60
61 /// Protected constructor.
62 /// @param id Assigned to #ID.
63 Job( const std::type_info& id )
64 : ID (id) {}
65
66 /// Protected destructor.
67 virtual ~Job() =default;
68
69 /// This method is called by the worker thread in the case this job was scheduled
70 /// for deferred deletion using \alib{threadmodel;ThreadPool::DeleteJobDeferred}
71 /// or \alib{threadmodel;DedicatedWorker::DeleteJobDeferred}.
72 /// The method is intended to prepare the deletion of this job without further actions
73 /// that the sender of a job otherwise would perform.
74 ///
75 /// Overriding this method must be considered well. It is up to the contract between
76 /// the caller and the implementation of a custom worker, how to handle this situation.
77 ///
78 /// As a sample, within the implementation of this module, it is used with class
79 /// \alib{threadmodel;JPromise} to disable the warnings of deleting an unfulfilled and
80 /// un-awaited promise in debug-compilations.
81 ///
82 /// @note This method is called by the worker thread, so it is not allowed to be called
83 /// by any other methods of the worker or caller
84 virtual void PrepareDeferredDeletion() {}
85
86 /// Virtual method that returns the size of the type.<br>
87 /// Derived types that add fields or use multi-inheritance, have to override this virtual
88 /// function like done in this foundational version, but providing the custom type:
89 /// virtual size_t SizeOf() override
90 /// {
91 /// return sizeof(MyJobType);
92 /// }
93 ///
94 /// @return The sizeof this type. Derived types have to return their size.
95 virtual size_t SizeOf() { return sizeof(Job); }
96
97 /// Tests if this job's type ID equals the explicitly provided (non-deducible) template
98 /// type \p{TOther}.
99 /// @return \c true, if this instance represents job \p{TOther}, \c false otherwise.
100 template<typename TOther>
101 bool Is() { return ID == typeid(TOther); }
102
103 /// Tests if this instance is not initialized.
104 /// @return \c true, if this instance represents non-job-type <c>void</c>, and
105 /// \c false otherwise.
106 template<typename TOther>
107 bool IsNull() { return ID == typeid(void); }
108
109 /// Returns the shared data dynamically cast to provided (non-deducible) template type
110 /// \p{TJob}.<br>
111 /// With debug-compilations it is asserted that the requested job type \p{TJob} matches
112 /// to what it really is.
113 ///
114 /// @tparam TJob The true type <c>this</c> points to.
115 /// @return A reference to the shared data stored with this job.
116 template<typename TJob>
117 TJob& Cast() {
118 ALIB_ASSERT_ERROR( typeid(TJob) == ID, "TMOD",
119 "Bad job casting.\n"
120 " Job type: <{}>\n"
121 " Requested type: <{}>", &ID, &typeid(TJob) )
122
123 auto* result= dynamic_cast<TJob*>(this);
124
125 ALIB_ASSERT_ERROR( result != nullptr, "TMOD",
126 "Job casting failed.\n"
127 " Job type: <{}>\n"
128 " Requested type: <{}>", &ID, &typeid(TJob) )
129 return *result;
130 }
131
132 /// Virtual function that may be overridden to execute the job when the job is executed by an
133 /// \alib{threadmodel;DedicatedWorker}.
134 /// In case it is executed by a \alib{threadmodel;ThreadPool} it has to be overridden.
135 ///
136 /// @return This default implementation returns \c false, which indicates that it is not
137 /// implemented.
138 /// Implementations need to return \c true.
139 virtual bool Do() { return false; }
140
141}; // struct Job
142
143/// A simple encapsulated promise.
144struct JPromise : public Job
145 , public Promise
146{
147 /// Constructor.
148 /// @param id The type-info of the derived type. Passed to parent class \b %Job.
149 JPromise(const std::type_info& id ) : Job(id) {}
150
151 /// Virtual destructor.
152 virtual ~JPromise() override =default;
153
154 /// Overrides the parent function as necessary.
155 /// @return The sizeof this derived type.
156 virtual size_t SizeOf() override { return sizeof(JPromise); }
157
158 #if ALIB_DEBUG
159 /// Overrides the parent function only with debug-compilations.
160 /// Avoids warnings about destroying unused promise
162 #endif
163};
164
165
166/// Possible priorities of jobs assigned to an \alib{threadmodel;DedicatedWorker}.
167enum class Priority
168{
169 Lowest = 0, ///< As the name indicates.
170 DeferredDeletion = 500, ///< As the name indicates.
171 Low = 1000, ///< As the name indicates.
172 Standard = 2000, ///< As the name indicates.
173 High = 3000, ///< As the name indicates.
174 Highest = 4000, ///< As the name indicates.
175};
176
177} // namespace alib[::threadmodel]
178
179/// Type alias in namespace \b alib.
181
182/// Type alias in namespace \b alib.
184
185} // namespace [alib]
void DbgOmitDestructionWarning()
Definition promise.inl:111
Promise()
Default constructor. Sets the state to State::Unfulfilled.
Definition promise.inl:91
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
Priority
Possible priorities of jobs assigned to an DedicatedWorker.
Definition jobs.inl:168
@ DeferredDeletion
As the name indicates.
Definition jobs.inl:170
@ Low
As the name indicates.
Definition jobs.inl:171
@ Highest
As the name indicates.
Definition jobs.inl:174
@ High
As the name indicates.
Definition jobs.inl:173
@ Lowest
As the name indicates.
Definition jobs.inl:169
@ Standard
As the name indicates.
Definition jobs.inl:172
threadmodel::JPromise JPromise
Type alias in namespace alib.
Definition jobs.inl:183
threadmodel::Job Job
Type alias in namespace alib.
Definition jobs.inl:180
A simple encapsulated promise.
Definition jobs.inl:146
virtual ~JPromise() override=default
Virtual destructor.
virtual size_t SizeOf() override
Definition jobs.inl:156
virtual void PrepareDeferredDeletion() override
Definition jobs.inl:161
JPromise(const std::type_info &id)
Definition jobs.inl:149
virtual void PrepareDeferredDeletion()
Definition jobs.inl:84
virtual size_t SizeOf()
Definition jobs.inl:95
const std::type_info & ID
Definition jobs.inl:59
Job(const std::type_info &id)
Definition jobs.inl:63
virtual ~Job()=default
Protected destructor.
virtual bool Do()
Definition jobs.inl:139