12#include "function.hpp"
14#define AsyncFw_STATIC_INIT_PRIORITY 65530
18 #define POLLPRI_ 0x002
19 #define POLLOUT_ 0x004
20 #define POLLERR_ 0x008
21 #define POLLHUP_ 0x010
22 #define POLLNVAL_ 0x020
24 #define POLLIN_ 0x0100
25 #define POLLPRI_ 0x0400
26 #define POLLOUT_ 0x0010
27 #define POLLERR_ 0x0001
28 #define POLLHUP_ 0x0002
29 #define POLLNVAL_ 0x0004
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"
39 #define checkCurrentThread()
40 #define checkDifferentThread()
52 enum PollEvents : uint16_t { PollNo = 0, PollIn = POLLIN_, PollPri = POLLPRI_, PollOut = POLLOUT_, PollErr = POLLERR_, PollHup = POLLHUP_, PollNval = POLLNVAL_ };
74 typename std::enable_if<std::is_void<typename std::invoke_result<M>::type>::value,
bool>::type
invokeMethod(M method,
bool sync =
false)
const {
83 if (std::this_thread::get_id() ==
id()) {
88 std::atomic_flag finished;
89 AbstractTask *_t =
new Task([&method, &finished]()
mutable {
91 finished.test_and_set();
92 finished.notify_one();
100 if (finished.test())
break;
105 template <
typename M>
110 template <
typename M>
112 return appendTimer(timeout,
new Task(std::forward<M>(method)));
158 std::thread::id
id()
const;
160 std::string
name()
const;
166 template <
typename M>
169 Task(M &&method) : method(std::move(method)) {}
170 virtual void operator()()
override { method(); }
176 template <
typename M>
178 friend class AbstractThread;
181 PollTask(M &&method) : method(std::move(method)) {}
182 virtual void operator()(AbstractThread::PollEvents _e)
override { method(_e); }
196 void processTasks()
const;
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;
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