AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AThread.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13#include <thread>
14#include <utility>
15#include "AUI/Common/ADeque.h"
16#include "AMutex.h"
17#include "AUI/Common/SharedPtrTypes.h"
18#include "AUI/Common/AString.h"
19#include "AUI/Util/AMessageQueue.h"
20#include <AUI/Platform/AStacktrace.h>
21#include <functional>
22
23class IEventLoop;
24class AString;
26
32class API_AUI_CORE AAbstractThread
33{
34 friend class IEventLoop;
35 friend class AThread;
36 friend class AConditionVariable;
37 friend void setupUIThread() noexcept; // OSDesktop.cpp
38public:
39
43 typedef std::thread::id id;
44
45
46public:
50 id getId() const;
51
57 void enqueue(AMessageQueue<>::Message f);
58
59 virtual ~AAbstractThread();
60
64 virtual bool isInterrupted();
65
69 virtual void resetInterruptFlag();
70
77 virtual void interrupt();
78
84 return mCurrentEventLoop;
85 }
86
87
93 template <class Callable>
94 inline void operator<<(Callable fun) {
95 enqueue(fun);
96 }
97
103 template <class Callable>
104 inline void operator*(Callable fun)
105 {
106 enqueue(fun);
107 }
108
109 [[nodiscard]]
110 const AString& threadName() const noexcept {
111 return mThreadName;
112 }
113
117 [[nodiscard]]
118 AStacktrace threadStacktrace() const;
119
120 [[nodiscard]]
121 bool messageQueueEmpty() noexcept;
122
123protected:
127 id mId;
128
129 AString mThreadName;
130
131 AMessageQueue<> mMessageQueue;
132
133 AAbstractThread(const id& id) noexcept;
134 void updateThreadName() noexcept;
135 virtual void processMessagesImpl();
136
137
138private:
139
143 struct {
144 AMutex mutex;
145 AConditionVariable* cv = nullptr;
146 } mCurrentCV;
147
151 IEventLoop* mCurrentEventLoop = nullptr;
152
156 AMutex mEventLoopLock;
157
158 AAbstractThread() = default;
159
160
164 static _<AAbstractThread>& threadStorage();
165};
166
167#include "AUI/Common/AObject.h"
168
173class API_AUI_CORE AThread : public AAbstractThread, public AObject
174{
175public:
181 {
182 public:
188 void needRethrow() const noexcept {
189 AThread::current()->interrupt();
190 }
191 };
192
193private:
197 std::thread* mThread = nullptr;
198
199
204 std::function<void()> mFunctor;
205
209 std::atomic_bool mInterrupted = false;
210
211public:
212
213 AThread(std::function<void()> functor);
214
215 virtual ~AThread();
216
217 void detach();
218
222 void start();
223
224
231 static void sleep(std::chrono::milliseconds duration);
232
236 static _<AAbstractThread> current();
237
247 static void interruptionPoint();
248
249
250
255 static void setName(AString name) noexcept {
256 auto cur = current();
257 cur->mThreadName = std::move(name);
258 cur->updateThreadName();
259 }
264 static void processMessages() {
265 current()->processMessagesImpl();
266 }
267
268 bool isInterrupted() override;
269 void resetInterruptFlag() override;
270 void interrupt() override;
271
275 void join();
276
277};
278
279#include "AConditionVariable.h"
Represents an abstract thread. Not all threads are created through AThread - these are interfaced wit...
Definition: AThread.h:33
void operator*(Callable fun)
Enqueue message to make. Helper function for async, asyncX, ui, uiX.
Definition: AThread.h:104
IEventLoop * getCurrentEventLoop() const
Get current event loop for this thread.
Definition: AThread.h:83
void operator<<(Callable fun)
Enqueue message to make.
Definition: AThread.h:94
std::thread::id id
Thread ID type.
Definition: AThread.h:43
Represents a condition variable.
Definition: AConditionVariable.h:24
Universal thread-safe message (callback) queue implementation.
Definition: AMessageQueue.h:29
A base object class.
Definition: AObject.h:49
Stacktrace consisting of a collection of stack function frames.
Definition: AStacktrace.h:28
Represents a Unicode character string.
Definition: AString.h:37
Exception that is thrown by AThread::interruptionPoint(), if interruption is requested for this threa...
Definition: AThread.h:181
void needRethrow() const noexcept
Schedules AThread::Interrupted exception to the next interruption point. Sometimes you could not thro...
Definition: AThread.h:188
Represents a user-defined thread.
Definition: AThread.h:174
static _< AAbstractThread > current()
Definition: AThread.cpp:221
static void setName(AString name) noexcept
Definition: AThread.h:255
static void processMessages()
Processes messages from other threads of current thread. Called by framework itself using IEventLoop.
Definition: AThread.h:264
Definition: IEventLoop.h:17
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Basic syscall-based synchronization primitive.
Definition: AMutex.h:33