AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
UnixIoThread.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//
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 {
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:620
void supplyValue(T v) const noexcept
Pushes the result to AFuture.
Definition: AFuture.h:650
Universal thread-safe message (callback) queue implementation.
Definition: AMessageQueue.h:29
void enqueue(Message message)
Add message to the queue to process in processMessages().
Definition: AMessageQueue.h:36
static _< AAbstractThread > current()
Definition: AThread.cpp:221
A std::vector with AUI extensions.
Definition: AVector.h:38
File-descriptor object to trigger select/poll/epoll/kqueue.
Definition: UnixEventFd.h:27
Definition: UnixIoThread.cpp:121
Poll-based event loop to handle events of file descriptors.
Definition: UnixIoThread.h:43
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
void wait(AFutureWait flags=AFutureWait::DEFAULT) const
Sleeps if the supplyValue is not currently available.
Definition: AFuture.h:465
AUI_ENUM_FLAG(ASide)
Describes sides of a 2D rectangle.
Definition: ASide.h:24
Basic syscall-based synchronization primitive.
Definition: AMutex.h:33