AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AThread.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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;
   25class AConditionVariable;
   26
   32class API_AUI_CORE AAbstractThread {
   33    friend class IEventLoop;
   34    friend class AThread;
   35    friend class AConditionVariable;
   36    friend void setupUIThread() noexcept;   // OSDesktop.cpp
   37
   38public:
   42    typedef std::thread::id id;
   43
   44public:
   48    id getId() const;
   49
   55    void enqueue(AMessageQueue<>::Message f);
   56
   57    [[nodiscard]]
   58    const AMessageQueue<>& messageQueue() const {
   59        return mMessageQueue;
   60    }
   61
   62    virtual ~AAbstractThread();
   63
   67    virtual bool isInterrupted();
   68
   72    virtual void resetInterruptFlag();
   73
   80    virtual void interrupt();
   81
   86    IEventLoop* getCurrentEventLoop() const { return mCurrentEventLoop; }
   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        enqueue(fun);
  106    }
  107
  108    [[nodiscard]]
  109    const AString& threadName() const noexcept {
  110        return mThreadName;
  111    }
  112
  116    [[nodiscard]]
  118
  119    [[nodiscard]]
  120    bool messageQueueEmpty() noexcept;
  121
  122protected:
  126    id mId;
  127
  128    AString mThreadName;
  129
  130    AMessageQueue<> mMessageQueue;
  131
  132    AAbstractThread(const id& id) noexcept;
  133    void updateThreadName() noexcept;
  134    virtual void processMessagesImpl();
  135
  136private:
  140    struct {
  141        AMutex mutex;
  142        AConditionVariable* cv = nullptr;
  143    } mCurrentCV;
  144
  148    IEventLoop* mCurrentEventLoop = nullptr;
  149
  153    AMutex mEventLoopLock;
  154
  155    AAbstractThread() = default;
  156
  160    static _<AAbstractThread>& threadStorage();
  161};
  162
  163#include "AUI/Common/AObject.h"
  164
  169class API_AUI_CORE AThread : public AAbstractThread, public AObject {
  170public:
  175    class Interrupted {
  176    public:
  182        void needRethrow() const noexcept { AThread::current()->interrupt(); }
  183    };
  184
  185private:
  189    std::thread* mThread = nullptr;
  190
  195    std::function<void()> mFunctor;
  196
  200    std::atomic_bool mInterrupted = false;
  201
  202public:
  203    AThread(std::function<void()> functor);
  204
  205    virtual ~AThread();
  206
  207    void detach();
  208
  212    [[nodiscard]]
  213    static const _<AAbstractThread>& main() noexcept;
  214
  218    void start();
  219
  226    static void sleep(std::chrono::milliseconds duration);
  227
  231    static _<AAbstractThread> current();
  232
  242    static void interruptionPoint();
  243
  248    static void setName(AString name) noexcept {
  249        auto cur = current();
  250        cur->mThreadName = std::move(name);
  251        cur->updateThreadName();
  252    }
  253
  257    static void processMessages() { current()->processMessagesImpl(); }
  258
  259    bool isInterrupted() override;
  260    void resetInterruptFlag() override;
  261    void interrupt() override;
  262
  266    void join();
  267};
  268
  269#include "AConditionVariable.h"
Represents an abstract thread. Not all threads are created through AThread - these are interfaced wit...
Definition AThread.h:32
void enqueue(AMessageQueue<>::Message f)
Delivers task for execution (message) to this thread's event queue. Messages are processed by framewo...
id getId() const
void operator*(Callable fun)
Enqueue message to make. Helper function for async, asyncX, ui, uiX.
Definition AThread.h:104
virtual void interrupt()
Interrupt thread's execution.
virtual bool isInterrupted()
AStacktrace threadStacktrace() const
Retrieve stacktrace of the thread.
IEventLoop * getCurrentEventLoop() const
Get current event loop for this thread.
Definition AThread.h:86
void operator<<(Callable fun)
Enqueue message to make.
Definition AThread.h:94
virtual void resetInterruptFlag()
Reset interruption flag.
id mId
Thread ID.
Definition AThread.h:126
std::thread::id id
Thread ID type.
Definition AThread.h:42
Represents a condition variable.
Definition AConditionVariable.h:24
Universal thread-safe message (callback) queue implementation.
Definition AMessageQueue.h:29
Stacktrace consisting of a collection of stack function frames.
Definition AStacktrace.h:28
Represents a Unicode character string.
Definition AString.h:38
Exception that is thrown by AThread::interruptionPoint(), if interruption is requested for this threa...
Definition AThread.h:175
void needRethrow() const noexcept
Schedules AThread::Interrupted exception to the next interruption point. Sometimes you could not thro...
Definition AThread.h:182
Represents a user-defined thread.
Definition AThread.h:169
void resetInterruptFlag() override
Reset interruption flag.
static void interruptionPoint()
Interruption point.
static void sleep(std::chrono::milliseconds duration)
Sleep for specified duration. Most operation systems guarantee that elasped time will be greater than...
static void setName(AString name) noexcept
Definition AThread.h:248
static void processMessages()
Processes messages from other threads of current thread. Called by framework itself using IEventLoop.
Definition AThread.h:257
static const _< AAbstractThread > & main() noexcept
Returns main thread of the application.
static _< AAbstractThread > current()
void join()
Waits for thread to be finished.
void start()
Start thread execution.
void interrupt() override
Interrupt thread's execution.
bool isInterrupted() override
Definition IEventLoop.h:17
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Basic syscall-based synchronization primitive.
Definition AMutex.h:33