AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
UnixIoThread.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//
   13// Created by alex2772 on 4/19/22.
   14//
   15
   16#pragma once
   17
   18#include <AUI/Thread/AThread.h>
   19#include <sys/poll.h>
   20#include "UnixEventFd.h"
   21#include <AUI/Thread/AFuture.h>
   22#include <AUI/Util/ABitField.h>
   23#include <unordered_map>
   24
   25
   26#ifdef __linux
   27#include <sys/epoll.h>
   28AUI_ENUM_FLAG(UnixPollEvent) {
   29    IN  = EPOLLIN,
   30    OUT = EPOLLOUT,
   31};
   32#else
   33AUI_ENUM_FLAG(UnixPollEvent) {
   34    IN  = POLLIN,
   35    OUT = POLLOUT,
   36};
   37#endif
   38
   39
   43class API_AUI_CORE UnixIoThread {
   44public:
   45    using Callback = std::function<void(ABitField<UnixPollEvent> triggeredFlags)>;
   46
   47    static UnixIoThread& inst() noexcept;
   48
   49    void registerCallback(int fd, ABitField<UnixPollEvent> flags, Callback callback) noexcept;
   50    void unregisterCallback(int fd) noexcept;
   51
   52private:
   53    friend class UnixIoEventLoop;
   54    _<AThread> mThread;
   55    UnixEventFd mNotifyEvent;
   56
   57#ifdef __linux
   58    int mEpollFd;
   59    struct FDInfo {
   60        struct CallbackEntry {
   61            ABitField<UnixPollEvent> mask;
   62            Callback callback;
   63        };
   64        AVector<CallbackEntry> callbacks;
   65    };
   66    AMutex mSync;
   67    std::unordered_map<int /* fd */, FDInfo> mFdInfo;
   68#else
   69    AVector<pollfd> mPollFd;
   70    AVector<Callback> mCallbacks;
   71#endif
   72    AMessageQueue<> mMessageQueue;
   73
   74    template<aui::invocable Callback>
   75    void executeOnIoThreadBlocking(Callback&& callback) {
   76        if (AThread::current() == mThread) {
   77            callback();
   78            return;
   79        }
   80
   81        AFuture<> cs;
   82        mMessageQueue.enqueue([&] {
   83            callback();
   84            cs.supplyValue();
   85        });
   86        mNotifyEvent.set();
   87        cs.wait();
   88    }
   89
   90    UnixIoThread() noexcept;
   91};
   92
   93
   94
   95
Bit field implementation.
Definition ABitField.h:20
Represents a value that will be available at some point in the future.
Definition AFuture.h:621
void supplyValue(T v) const noexcept
Pushes the result to AFuture.
Definition AFuture.h:651
Universal thread-safe message (callback) queue implementation.
Definition AMessageQueue.h:29
static _< AAbstractThread > current()
A std::vector with AUI extensions.
Definition AVector.h:39
File-descriptor object to trigger select/poll/epoll/kqueue.
Definition UnixEventFd.h:27
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
void wait(AFutureWait flags=AFutureWait::DEFAULT) const
Sleeps if the supplyValue is not currently available.
Definition AFuture.h:465
#define AUI_ENUM_FLAG(name)
Make a bitfield-style enum class.
Definition AEnumerate.h:227
Basic syscall-based synchronization primitive.
Definition AMutex.h:33