AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
AbstractThread.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include <thread>
11#include <vector>
12#include "function.hpp"
13
14#define AsyncFw_STATIC_INIT_PRIORITY 65530
15
16#ifndef _WIN32
17 #define POLLIN_ 0x001
18 #define POLLPRI_ 0x002
19 #define POLLOUT_ 0x004
20 #define POLLERR_ 0x008
21 #define POLLHUP_ 0x010
22 #define POLLNVAL_ 0x020
23#else
24 #define POLLIN_ 0x0100
25 #define POLLPRI_ 0x0400
26 #define POLLOUT_ 0x0010
27 #define POLLERR_ 0x0001
28 #define POLLHUP_ 0x0002
29 #define POLLNVAL_ 0x0004
30#endif
31
32#if !defined LS_NO_ERROR
33 #define AsyncFw_THREAD this
34 #define checkCurrentThread() \
35 if (std::this_thread::get_id() != AsyncFw_THREAD->id()) lsError() << "executed from different thread"
36 #define checkDifferentThread() \
37 if (std::this_thread::get_id() == AsyncFw_THREAD->id()) lsError() << "executed from own thread"
38#else
39 #define checkCurrentThread()
40 #define checkDifferentThread()
41#endif
42
43namespace AsyncFw {
44class LogStream;
47 friend class Thread;
48 friend LogStream &operator<<(LogStream &, const AbstractThread &);
49 struct Private;
50
51public:
52 enum PollEvents : uint16_t { PollNo = 0, PollIn = POLLIN_, PollPri = POLLPRI_, PollOut = POLLOUT_, PollErr = POLLERR_, PollHup = POLLHUP_, PollNval = POLLNVAL_ };
54 using LockGuard = std::lock_guard<std::mutex>;
56 using AbstractTask = AbstractFunction<>;
58 using AbstractPollTask = AbstractFunction<AbstractThread::PollEvents>;
59
61 class Holder {
62 public:
63 void complete();
65 void wait();
66
67 private:
68 AbstractThread *thread;
69 bool waiting;
70 };
71
73 template <typename M>
74 typename std::enable_if<std::is_void<typename std::invoke_result<M>::type>::value, bool>::type invokeMethod(M method, bool sync = false) const {
75 if (!sync) {
76 AbstractTask *_t = new Task(std::forward<M>(method));
77 if (!invokeTask(_t)) {
78 delete _t;
79 return false;
80 }
81 return true;
82 }
83 if (std::this_thread::get_id() == id()) {
84 processTasks();
85 method();
86 return true;
87 }
88 std::atomic_flag finished;
89 AbstractTask *_t = new Task([&method, &finished]() mutable {
90 method();
91 finished.test_and_set();
92 finished.notify_one();
93 });
94 if (!invokeTask(_t)) {
95 delete _t;
96 return false;
97 }
98 for (;;) {
99 finished.wait(false);
100 if (finished.test()) break;
101 }
102 return true;
103 }
104
105 template <typename M>
106 bool appendPollTask(int fd, PollEvents events, M method) {
107 return appendPollDescriptor(fd, events, new PollTask(std::forward<M>(method)));
108 }
109
110 template <typename M>
111 int appendTimerTask(int timeout, M method) {
112 return appendTimer(timeout, new Task(std::forward<M>(method)));
113 }
114
118 static AbstractThread::LockGuard threads(std::vector<AbstractThread *> **);
119
121 virtual void startedEvent();
123 virtual void finishedEvent();
124
126 virtual bool running() const;
128 virtual bool invokeTask(AbstractTask *) const;
130 virtual int appendTimer(int, AbstractTask *);
132 virtual bool modifyTimer(int, int);
134 virtual void removeTimer(int);
136 virtual bool appendPollDescriptor(int, PollEvents, AbstractPollTask *);
138 virtual bool modifyPollDescriptor(int, PollEvents);
140 virtual void removePollDescriptor(int);
141
143 void start();
145 void requestInterrupt();
147 bool interruptRequested() const;
149 void waitInterrupted() const;
151 void quit();
153 void waitFinished() const;
155 int workLoad() const;
156
158 std::thread::id id() const;
160 std::string name() const;
161
163 LockGuard lockGuard() const;
164
165protected:
166 template <typename M>
167 class Task : public AbstractTask {
168 public:
169 Task(M &&method) : method(std::move(method)) {}
170 virtual void operator()() override { method(); }
171
172 private:
173 M method;
174 };
175
176 template <typename M>
177 class PollTask : private AbstractPollTask {
178 friend class AbstractThread;
179
180 private:
181 PollTask(M &&method) : method(std::move(method)) {}
182 virtual void operator()(AbstractThread::PollEvents _e) override { method(_e); }
183 M method;
184 };
185
187 AbstractThread(const std::string &);
188 virtual ~AbstractThread() = 0;
189
190 void setId();
191 void clearId();
193 void exec();
194
195private:
196 void processTasks() const;
197 Private &private_;
198};
199
200constexpr AbstractThread::PollEvents operator|(AbstractThread::PollEvents e1, AbstractThread::PollEvents e2) { return static_cast<AbstractThread::PollEvents>(static_cast<uint8_t>(e1) | static_cast<uint8_t>(e2)); }
201constexpr bool operator==(AbstractThread::PollEvents, AbstractThread::PollEvents) = delete;
202constexpr bool operator!=(AbstractThread::PollEvents, AbstractThread::PollEvents) = delete;
203} // namespace AsyncFw
The AbstractTask class.
Definition Task.h:16
The Holder class.
Definition AbstractThread.h:61
void wait()
Runs nested exec() and wait for it completed.
Definition AbstractThread.cpp:178
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:46
bool appendPollTask(int fd, PollEvents events, M method)
Append poll task.
Definition AbstractThread.h:106
std::string name() const
Returns name of managed thread.
Definition AbstractThread.cpp:671
void requestInterrupt()
Request the interruption of the thread.
Definition AbstractThread.cpp:320
void exec()
Run manage loop.
Definition AbstractThread.cpp:399
virtual int appendTimer(int, AbstractTask *)
Append timer.
Definition AbstractThread.cpp:675
std::thread::id id() const
Returns unique identifier of managed thread.
Definition AbstractThread.cpp:669
static AbstractThread::LockGuard threads(std::vector< AbstractThread * > **)
Assigns a pointer to the list of all threads.
Definition AbstractThread.cpp:306
int workLoad() const
Returns the number of tasks in the queue, plus one if there are running task.
Definition AbstractThread.cpp:661
virtual void removePollDescriptor(int)
Remove poll descriptor.
Definition AbstractThread.cpp:800
void waitFinished() const
Wait for the thread's exec() function finished.
Definition AbstractThread.cpp:360
virtual bool modifyTimer(int, int)
Modify timer.
Definition AbstractThread.cpp:694
std::lock_guard< std::mutex > LockGuard
The LockGuard type.
Definition AbstractThread.h:54
void waitInterrupted() const
Wait for thread interrupted. This means that requestInterrupt() has been called and all queue tasks h...
Definition AbstractThread.cpp:337
int appendTimerTask(int timeout, M method)
Append timer task.
Definition AbstractThread.h:111
LockGuard lockGuard() const
Locks the managed thread and returns a LockGuard variable. The thread is unblocked after this variabl...
Definition AbstractThread.cpp:673
AbstractThread(const std::string &)
Constructs a thread.
Definition AbstractThread.cpp:201
virtual bool appendPollDescriptor(int, PollEvents, AbstractPollTask *)
Append poll descriptor.
Definition AbstractThread.cpp:731
virtual void startedEvent()
This call from thread when it starts executing.
Definition AbstractThread.cpp:311
virtual bool running() const
Returns true if the managed thread is running.
Definition AbstractThread.cpp:315
std::enable_if< std::is_void< typenamestd::invoke_result< M >::type >::value, bool >::type invokeMethod(M method, bool sync=false) const
Runs a method in a managed thread.
Definition AbstractThread.h:74
bool interruptRequested() const
Returns true if an interrupt is requested.
Definition AbstractThread.cpp:332
AbstractFunction<> AbstractTask
The AbstractTask type.
Definition AbstractThread.h:56
virtual bool invokeTask(AbstractTask *) const
Runs a task in a managed thread.
Definition AbstractThread.cpp:619
static AbstractThread * currentThread()
Returns a pointer to the AsyncFw::AbstractThread that manages the currently executing thread.
Definition AbstractThread.cpp:295
virtual void finishedEvent()
This call from the thread when it finishing execution.
Definition AbstractThread.cpp:313
AbstractFunction< AbstractThread::PollEvents > AbstractPollTask
The AbstractPollTask type.
Definition AbstractThread.h:58
virtual void removeTimer(int)
Remove timer.
Definition AbstractThread.cpp:712
virtual bool modifyPollDescriptor(int, PollEvents)
Modify poll descriptor.
Definition AbstractThread.cpp:768
void start()
Create a managed thread and run exec().
Definition AbstractThread.cpp:635
void quit()
Tells the thread's exec() to exit.
Definition AbstractThread.cpp:348
The LogStream class.
Definition LogStream.h:44
The Task class.
Definition Task.h:25