ALib C++ Library
Library Version: 2412 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/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_THREADMODEL_JOBS
9#define HPP_ALIB_THREADMODEL_JOBS
10#pragma once
11
12#if !defined(DOXYGEN)
13# include "alib/alib.hpp"
14#endif
15#include <queue>
16ALIB_ASSERT_MODULE(THREADMODEL)
17
18#include "alib/strings/localstring.hpp"
22
23namespace alib { namespace threadmodel
24{
25class DedicatedWorker;
26class ThreadPool;
27class DWManager;
28
29
30
31//==============================================================================================
32/// \attention This class belongs to module \alib_threadmodel, which is not in a stable and
33/// consistent state, yet.
34/// Also this type is considered experimental.
35///
36/// Defines jobs which are scheduled with instances of types \alib{threadmodel;ThreadPool} and
37/// \alib{threadmodel;DedicatedWorker}.<br>
38///
39/// The type is virtual and designed to be the base type for custom <em>"jobs"</em>.
40/// It holds a job-ID of type <c>std::type_info</c> which is used to identify the job type with
41/// processing.
42/// This ID usually represents - but does not need to! - the derived type.
43/// By including the ID into the job, one derived job-type object can be used with different
44/// job definitions.
45/// For example, built-in derived type \alib{threadmodel;JPromise} may be used as the "shared data"
46/// between the sender and the processing thread.
47/// The constructor of \b %JPromise accepts a type ID to store.
48///
49/// This explains that class Job has two main aspects:
50/// - Provide information to a worker thread about what is to be done.
51/// - Provide shared data between the sender and the worker.
52///
53/// Instances of this class are always \alib{monomem;TPoolAllocator;pool-allocated}.
54/// Allocation, construction, destruction and deletion are always under the control of either
55/// type \alib{threadmodel;ThreadPool} or \alib{threadmodel;DedicatedWorker}.
56/// Pool allocation requires passing the object size with deletion
57/// (all details on this topic is \ref alib_contmono_poolallocator_allocinfo "found here").
58/// For this, derived types that add new field members have to override method #SizeOf in a way
59/// described with the documentation of that virtual method, to return the correct size of the
60/// derived type.
61///
62/// Virtual method #Do has to be overridden in case a job is to be scheduled with class
63/// \alib{threadmodel;ThreadPool}.
64/// In case a derived type is (exclusively) used with type \alib{threadmodel;DedicatedWorker},
65/// it is up to the user of this module whether method #Do is implemented or not.
66/// If not, then a custom type derived from \b DedicatedWorker has to override method
67/// \alib{threadmodel::DedicatedWorker;process} and handle the job type there.
68///
69/// @see The Programmer's Manual of this module \alib_threadmodel for detailed information.
70//==============================================================================================
71struct Job
72{
73 /// The identifier of the job. Any custom type can be given. Jobs with complex
74 /// shared data types, which are used only for this specific job, usually use the
75 /// shared data type itself here.
76 const std::type_info& ID;
77
78 /// Protected constructor.
79 /// @param id Assigned to #ID.
80 Job( const std::type_info& id )
81 : ID (id) {}
82
83 /// Protected destructor.
84 virtual ~Job() = default;
85
86 /// This method is called by the worker thread in the case this job was scheduled
87 /// for deferred deletion using \alib{threadmodel;ThreadPool::DeleteJobDeferred}
88 /// or \alib{threadmodel;DedicatedWorker::DeleteJobDeferred}.
89 /// The method is intended to prepare the deletion of this job without further actions
90 /// that the sender of a job otherwise would perform.
91 ///
92 /// Overriding this method must be considered well. It is up to the contract between
93 /// the caller and the implementation of a custom worker, how to handle this situation.
94 ///
95 /// As a sample, within the implementation of this module, it is used with class
96 /// \alib{threadmodel;JPromise} to disable the warnings of deleting an unfulfilled and
97 /// un-awaited promise in debug-compilations.
98 ///
99 /// @note This method is called by the worker thread, so it is not allowed to be called
100 /// by any other methods of the worker or caller
101 virtual void PrepareDeferredDeletion() {}
102
103 /// Virtual method that returns the size of the type.<br>
104 /// Derived types that add fields or use multi-inheritance, have to override this virtual
105 /// function like done in this foundational version, but providing the custom type:
106 /// virtual size_t SizeOf() override
107 /// {
108 /// return sizeof(MyJobType);
109 /// }
110 ///
111 /// @return The sizeof this type. Derived types have to return their size.
112 virtual size_t SizeOf() { return sizeof(Job); }
113
114 /// Tests if this job's type ID equals the explicitly provided (non-deducible) template
115 /// type \p{TOther}.
116 /// @return \c true, if this instance represents job \p{TOther}, \c false otherwise.
117 template<typename TOther>
118 bool Is() { return ID == typeid(TOther); }
119
120 /// Tests if this instance is not initialized.
121 /// @return \c true, if this instance represents non-job-type <c>void</c>, and
122 /// \c false otherwise.
123 template<typename TOther>
124 bool IsNull() { return ID == typeid(void); }
125
126 /// Returns the shared data dynamically cast to provided (non-deducible) template type
127 /// \p{TJob}.<br>
128 /// With debug-compilations it is asserted that the requested job type \p{TJob} matches
129 /// to what it really is.
130 ///
131 /// @tparam TJob The true type <c>this</c> points to.
132 /// @return A reference to the shared data stored with this job.
133 template<typename TJob>
134 TJob& Cast()
135 {
136 ALIB_ASSERT_ERROR( typeid(TJob) == ID, "MGTHR", NString2K() <<
137 "Bad job casting.\n"
138 " Job type: <" << ID << ">\n"
139 " Requested type: <" << typeid(TJob) << ">" )
140
141 auto* result= dynamic_cast<TJob*>(this);
142
143 ALIB_ASSERT_ERROR( result != nullptr, "MGTHR", NString2K() <<
144 "Job casting failed.\n"
145 " Job type: <" << ID << ">\n"
146 " Requested type: <" << typeid(TJob) << ">" )
147
148 return *result;
149 }
150
151 /// Virtual function that may be overridden to execute the job when the job is executed by an
152 /// \alib{threadmodel;DedicatedWorker}.
153 /// In case it is executed by a \alib{threadmodel;ThreadPool} it has to be overridden.
154 ///
155 /// @return This default implementation returns \c false, which indicates that it is not
156 /// implemented.
157 /// Implementations need to return \c true.
158 virtual bool Do() { return false; }
159
160}; // struct Job
161
162/// A simple encapsulated promise.
163struct JPromise : public Job
164 , public threads::Promise
165{
166 /// Constructor.
167 /// @param id The type-info of the derived type. Passed to parent class \b %Job.
168 JPromise(const std::type_info& id ) : Job(id) {}
169
170 /// Virtual destructor.
171 virtual ~JPromise() override= default;
172
173 /// Overrides the parent function as necessary.
174 /// @return The sizeof this derived type.
175 virtual size_t SizeOf() override { return sizeof(JPromise); }
176
177 #if ALIB_DEBUG
178 /// Overrides the parent function only with debug-compilations.
179 /// Avoids warnings about destroying unused promise
181 #endif
182};
183
184
185/// Possible priorities of jobs assigned to an \alib{threadmodel;DedicatedWorker}.
186enum class Priority
187{
188 Lowest = 0, ///< As the name indicates.
189 DeferredDeletion = 500, ///< As the name indicates.
190 Low = 1000, ///< As the name indicates.
191 Standard = 2000, ///< As the name indicates.
192 High = 3000, ///< As the name indicates.
193 Highest = 4000, ///< As the name indicates.
194};
195
196
197
198#if !ALIB_CAMP || DOXYGEN
199//==================================================================================================
200/// This namespace function initializes the module \alib_threadmodel.
201///
202/// This function needs to be called with bootstrapping a process.
203/// The \ref alib_manual_bootstrapping "standard bootstrap" code of \alib, hence the (overloaded)
204/// functions \ref alib::Bootstrap will call this function.
205///
206/// \note
207/// In fact, if the module \alib_basecamp is included, then this function is empty,
208/// because the enumeration records will in this case be resourced in singleton
209/// \ref alib::BASECAMP of type \alib{lang::basecamp;BaseCamp}.
210///
211/// Multiple invocations of this method are forbidden.
212///
213/// \see
214/// For information about using this method, consult chapter
215/// \ref alib_manual_bootstrapping_nocamps of the \ref alib_manual.
216//==================================================================================================
219#else
220 inline void Bootstrap() {}
221#endif
222
223
224
225} // namespace alib[::threadmodel]
226
227/// Type alias in namespace \b alib.
229
230/// Type alias in namespace \b alib.
232
233
234} // namespace [alib]
235
236
237#endif // HPP_ALIB_THREADMODEL_JOBS
238
void DbgOmitDestructionWarning()
Definition promise.hpp:125
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_API
Definition alib.hpp:639
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
ALIB_API void Bootstrap()
Priority
Possible priorities of jobs assigned to an DedicatedWorker.
Definition jobs.hpp:187
@ DeferredDeletion
As the name indicates.
@ Low
As the name indicates.
@ Highest
As the name indicates.
@ High
As the name indicates.
@ Lowest
As the name indicates.
@ Standard
As the name indicates.
Definition alib.cpp:69
threadmodel::ThreadPool ThreadPool
Type alias in namespace alib.
threadmodel::JPromise JPromise
Type alias in namespace alib.
Definition jobs.hpp:231
NLocalString< 2048 > NString2K
Type alias name for TLocalString<nchar,2048>.
threadmodel::DWManager DWManager
Type alias in namespace alib.
threadmodel::DedicatedWorker DedicatedWorker
Type alias in namespace alib.
threadmodel::Job Job
Type alias in namespace alib.
Definition jobs.hpp:228
A simple encapsulated promise.
Definition jobs.hpp:165
virtual void PrepareDeferredDeletion() override
Definition jobs.hpp:180
virtual ~JPromise() override=default
Virtual destructor.
JPromise(const std::type_info &id)
Definition jobs.hpp:168
virtual size_t SizeOf() override
Definition jobs.hpp:175
virtual size_t SizeOf()
Definition jobs.hpp:112
virtual bool Do()
Definition jobs.hpp:158
virtual void PrepareDeferredDeletion()
Definition jobs.hpp:101
const std::type_info & ID
Definition jobs.hpp:76
Job(const std::type_info &id)
Definition jobs.hpp:80
virtual ~Job()=default
Protected destructor.