5#include "../Foundation/Function.h"
6#include "../Foundation/OpaqueObject.h"
7#include "../Foundation/Span.h"
8#include "../Threading/Atomic.h"
9#include "../Threading/ThreadPool.h"
10#include "../Time/Time.h"
13#include "../File/FileDescriptor.h"
14#include "../Process/ProcessDescriptor.h"
15#include "../Socket/SocketDescriptor.h"
48struct AsyncKernelEvents;
49struct AsyncEventLoopListeners;
51struct AsyncEventLoopMonitor;
55template <
typename T,
typename C>
57struct AsyncCompletionData;
60template <
typename AsyncType>
68struct AsyncWinOverlapped;
69struct AsyncWinOverlappedDefinition
71 static constexpr int Windows =
sizeof(
void*) * 4 +
sizeof(
uint64_t);
72 static constexpr size_t Alignment =
alignof(
void*);
74 using Object = AsyncWinOverlapped;
76using WinOverlappedOpaque = OpaqueObject<AsyncWinOverlappedDefinition>;
78struct AsyncWinWaitDefinition
80 using Handle = FileDescriptor::Handle;
81 static constexpr Handle Invalid = FileDescriptor::Invalid;
83 static Result releaseHandle(Handle& waitHandle);
85struct WinWaitHandle :
public UniqueHandle<AsyncWinWaitDefinition>
137 void setDebugName(
const char* newDebugName);
196 [[nodiscard]]
Result validateAsync();
211 [[nodiscard]]
static const char* TypeToString(
Type type);
222#if SC_CONFIGURATION_DEBUG
223 const char* debugName =
"None";
258 bool shouldBeReactivated =
false;
259 bool shouldCallCallback =
true;
267template <
typename T,
typename C>
270 T& getAsync() {
return static_cast<T&
>(AsyncResult::async); }
271 const T& getAsync()
const {
return static_cast<const T&
>(AsyncResult::async); }
292 void freeTask() { async =
nullptr; }
293 bool isFree()
const {
return async ==
nullptr; }
307template <
typename AsyncType>
310 typename AsyncType::CompletionData asyncCompletionData;
450 status = completionData.exitStatus;
465 ProcessDescriptor::Handle handle = ProcessDescriptor::Invalid;
466#if SC_PLATFORM_WINDOWS
468 detail::WinWaitHandle waitHandle;
469#elif SC_PLATFORM_LINUX
501 return client.assign(
move(completionData.acceptedClient));
516 SocketDescriptor::Handle handle = SocketDescriptor::Invalid;
518#if SC_PLATFORM_WINDOWS
519 void (*pAcceptEx)() =
nullptr;
522 uint8_t acceptBuffer[288] = {0};
523#elif SC_PLATFORM_LINUX
560 SocketDescriptor::Handle handle = SocketDescriptor::Invalid;
562#if SC_PLATFORM_WINDOWS
563 void (*pConnectEx)() =
nullptr;
608 SocketDescriptor::Handle
handle = SocketDescriptor::Invalid;
613#if SC_PLATFORM_WINDOWS
616 size_t totalBytesSent = 0;
639 bool disconnected =
false;
652 SC_TRY(getAsync().
buffer.sliceStartLength(0, completionData.numBytes, outData));
676 SocketDescriptor::Handle handle = SocketDescriptor::Invalid;
679#if SC_PLATFORM_WINDOWS
714 SocketDescriptor::Handle handle = SocketDescriptor::Invalid;
745 bool endOfFile =
false;
755 SC_TRY(getAsync().
buffer.sliceStartLength(0, completionData.numBytes, data));
791 bool useOffset =
false;
793#if SC_PLATFORM_WINDOWS
831 [[nodiscard]]
SC::Result get(
size_t& writtenSizeInBytes)
833 writtenSizeInBytes = completionData.numBytes;
869 bool useOffset =
false;
870 uint64_t offset = 0xffffffffffffffff;
871#if SC_PLATFORM_WINDOWS
900 FileDescriptor::Handle fileDescriptor = FileDescriptor::Invalid;
920#if SC_PLATFORM_WINDOWS
921 [[nodiscard]]
auto& getOverlappedOpaque() {
return overlapped; }
929 FileDescriptor::Handle fileDescriptor = FileDescriptor::Invalid;
930#if SC_PLATFORM_WINDOWS
947 int numberOfEvents = 0;
1108 struct InternalDefinition
1110 static constexpr int Windows = 488;
1111 static constexpr int Apple = 480;
1112 static constexpr int Linux = 688;
1113 static constexpr int Default = Linux;
1115 static constexpr size_t Alignment = 8;
1117 using Object = Internal;
1124 InternalOpaque internalOpaque;
1162#pragma warning(push)
1163#pragma warning(disable : 4324)
1181 bool wakeUpHasBeenCalled =
false;
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition: Compiler.h:269
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition: PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:48
short int16_t
Platform independent (2) bytes signed int.
Definition: PrimitiveTypes.h:45
A buffer of bytes with given alignment.
Definition: AlignedStorage.h:25
Empty base struct for all AsyncRequest-derived CompletionData (internal) structs.
Definition: Async.h:233
Options given to AsyncEventLoop::create.
Definition: Async.h:968
ApiType apiType
Criteria to choose Async IO API.
Definition: Async.h:975
ApiType
Definition: Async.h:970
@ Automatic
Platform specific backend chooses the best API.
@ ForceUseEpoll
(Linux only) Tries to use epoll
@ ForceUseIOURing
(Linux only) Tries to use io_uring (failing if it's not found on the system)
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:965
Time::Monotonic getLoopTime() const
Get Loop time.
Result associateExternallyCreatedFileDescriptor(FileDescriptor &outDescriptor)
Associates a File descriptor created externally with the eventLoop.
Result wakeUpFromExternalThread()
Wake up the event loop from a thread different than the one where run() is called (and potentially bl...
Result runNoWait()
Process active requests if any, dispatching their completions, or returns immediately without blockin...
void updateTime()
Updates loop time to "now".
static bool isExcludedFromActiveCount(const AsyncRequest &async)
Checks if excludeFromActiveCount() has been called on the given request.
Result blockingPoll(AsyncKernelEvents &kernelEvents)
Blocks until at least one event happens, ensuring forward progress, without executing completions.
int getNumberOfSubmittedRequests() const
Obtain the total number of submitted requests.
Result submitRequests(AsyncKernelEvents &kernelEvents)
Submits all queued async requests.
void enumerateRequests(Function< void(AsyncRequest &)> enumerationCallback)
Enumerates all requests objects associated with this loop.
AsyncLoopTimeout * findEarliestLoopTimeout() const
Returns the next AsyncLoopTimeout that will be executed (shortest relativeTimeout)
void setListeners(AsyncEventLoopListeners *listeners)
Sets reference to listeners that will signal different events in loop lifetime.
Result dispatchCompletions(AsyncKernelEvents &kernelEvents)
Invokes completions for the AsyncKernelEvents collected by a call to AsyncEventLoop::blockingPoll.
void interrupt()
Interrupts the event loop even if it has active request on it.
Result wakeUpFromExternalThread(AsyncLoopWakeUp &wakeUp)
Wake up the event loop from a thread different than the one where run() is called (and potentially bl...
bool isInitialized() const
Returns true if create has been already called (successfully)
Result create(Options options=Options())
Creates the event loop kernel object.
static bool tryLoadingLiburing()
Check if liburing is loadable (only on Linux)
Result createAsyncTCPSocket(SocketFlags::AddressFamily family, SocketDescriptor &outDescriptor)
Helper to creates a TCP socket with AsyncRequest flags of the given family (IPV4 / IPV6).
Result associateExternallyCreatedTCPSocket(SocketDescriptor &outDescriptor)
Associates a TCP Socket created externally (without using createAsyncTCPSocket) with the eventLoop.
Result close()
Closes the event loop kernel object.
Result runOnce()
Blocks until at least one request proceeds, ensuring forward progress, dispatching all completions.
void excludeFromActiveCount(AsyncRequest &async)
Excludes the request from active handles count (to avoid it keeping event loop alive)
Result run()
Blocks until there are no more active queued requests, dispatching all completions.
void includeInActiveCount(AsyncRequest &async)
Reverses the effect of excludeFromActiveCount for the request.
int getNumberOfActiveRequests() const
Obtain the total number of active requests.
Allow library user to provide callbacks signaling different phases of async event loop cycle.
Definition: Async.h:953
Monitors Async I/O events from a background thread using a blocking kernel function (no CPU usage on ...
Definition: Async.h:1136
Result create(AsyncEventLoop &loop)
Create the monitoring thread for an AsyncEventLoop.
Function< void(void)> onNewEventsAvailable
Informs to call dispatchCompletions on GUI Event Loop.
Definition: Async.h:1137
Result startMonitoring()
Queue all async requests submissions and start monitoring loop events on a background thread.
Result close()
Stop monitoring the AsyncEventLoop, disposing all resources.
Result stopMonitoringAndDispatchCompletions()
Stops monitoring events on the background thread and dispatches callbacks for completed requests.
Starts a file close operation, closing the OS file descriptor.
Definition: Async.h:882
int code
Return code of close socket operation.
Definition: Async.h:894
Function< void(Result &)> callback
Callback called after fully closing the file descriptor.
Definition: Async.h:896
Starts an handle polling operation.
Definition: Async.h:908
SC::Result start(AsyncEventLoop &loop, FileDescriptor::Handle fileDescriptor)
Starts a file descriptor poll operation, monitoring its readiness with appropriate OS API.
Completion data for AsyncFileRead.
Definition: Async.h:743
Callback result for AsyncFileRead.
Definition: Async.h:750
Starts a file read operation, reading bytes from a file (or pipe).
Definition: Async.h:738
Span< char > buffer
Callback called when some data has been read from the file into the buffer.
Definition: Async.h:774
FileDescriptor::Handle fileDescriptor
The writeable span of memory where to data will be written.
Definition: Async.h:775
SC::Result start(AsyncEventLoop &eventLoop)
Starts a file receive operation, that completes when data has been read from file / pipe.
void setOffset(uint64_t fileOffset)
Sets the offset in bytes at which start reading.
Definition: Async.h:783
uint64_t getOffset() const
The file/pipe descriptor handle to read data from.
Definition: Async.h:779
Completion data for AsyncFileWrite.
Definition: Async.h:822
Callback result for AsyncFileWrite.
Definition: Async.h:828
Starts a file write operation, writing bytes to a file (or pipe).
Definition: Async.h:817
uint64_t getOffset() const
The file/pipe descriptor to write data to.
Definition: Async.h:857
FileDescriptor::Handle fileDescriptor
The read-only span of memory where to read the data from.
Definition: Async.h:853
void setOffset(uint64_t fileOffset)
Sets the offset in bytes at which start writing.
Definition: Async.h:861
SC::Result start(AsyncEventLoop &eventLoop)
Starts a file write operation that completes when it's ready to receive more bytes.
Span< const char > buffer
Callback called when descriptor is ready to be written with more data.
Definition: Async.h:852
Allows user to supply a block of memory that will store kernel I/O events retrieved from AsyncEventLo...
Definition: Async.h:943
Span< uint8_t > eventsMemory
User supplied block of memory used to store kernel I/O events.
Definition: Async.h:944
Starts a Timeout that is invoked only once after expiration (relative) time has passed.
Definition: Async.h:324
SC::Result start(AsyncEventLoop &eventLoop, Time::Milliseconds relativeTimeout)
Starts a Timeout that is invoked (only once) after the specific relative expiration time has passed.
Time::Absolute getExpirationTime() const
Gets computed absolute expiration time that determines when this timeout get executed.
Definition: Async.h:351
Function< void(Result &)> callback
Called after given expiration time since AsyncLoopTimeout::start has passed.
Definition: Async.h:346
Time::Milliseconds relativeTimeout
First timer expiration (relative) time in milliseconds.
Definition: Async.h:348
SC::Result start(AsyncEventLoop &eventLoop)
Starts a Timeout that is invoked (only once) after the specific relative expiration time has passed.
Starts a wake-up operation, allowing threads to execute callbacks on loop thread.
Definition: Async.h:371
SC::Result start(AsyncEventLoop &eventLoop, EventObject *eventObject=nullptr)
Starts a wake up request, that will be fulfilled when an external thread calls AsyncLoopWakeUp::wakeU...
Function< void(Result &)> callback
Callback called by SC::AsyncEventLoop::run after SC::AsyncLoopWakeUp::wakeUp.
Definition: Async.h:389
SC::Result wakeUp()
Wakes up event loop, scheduling AsyncLoopWakeUp::callback on next AsyncEventLoop::run (or its variati...
Executes work in a thread pool and then invokes a callback on the event loop thread.
Definition: Async.h:404
Function< void(Result &)> callback
Called to execute the work in a background threadpool thread.
Definition: Async.h:423
SC::Result setThreadPool(ThreadPool &threadPool)
Sets the ThreadPool that will supply the thread to run the async work on.
SC::Result start(AsyncEventLoop &eventLoop)
Schedule work to be executed on a background thread, notifying the event loop when it's finished.
Completion data for AsyncProcessExit.
Definition: Async.h:439
Callback result for AsyncProcessExit.
Definition: Async.h:445
Starts monitoring a process, notifying about its termination.
Definition: Async.h:434
SC::Result start(AsyncEventLoop &eventLoop, ProcessDescriptor::Handle process)
Starts monitoring a process, notifying about its termination.
Function< void(Result &)> callback
Called when process has exited.
Definition: Async.h:461
Base class for all async requests, holding state and type.
Definition: Async.h:133
bool isCancelling() const
Returns true if this request is being cancelled.
AsyncRequest(Type type)
Constructs a free async request of given type.
Definition: Async.h:174
AsyncEventLoop * getEventLoop() const
Get the event loop associated with this AsyncRequest.
Definition: Async.h:140
void resetThreadPoolAndTask()
Resets anything previously set with setThreadPoolAndTask.
Result stop(Function< void(AsyncResult &)> *afterStopped=nullptr)
Ask to stop current async operation.
bool isActive() const
Returns true if this request is active or being reactivated.
Result setThreadPoolAndTask(ThreadPool &pool, AsyncTask &task)
Sets the thread pool and task to use for this request.
void cacheInternalEventLoop(AsyncEventLoop &loop)
Caches the event loop associated with this AsyncRequest.
Definition: Async.h:144
bool isFree() const
Returns true if this request is free.
Type getType() const
Returns request type.
Definition: Async.h:193
Type
Type of async request.
Definition: Async.h:156
@ FileClose
Request is an AsyncFileClose object.
@ SocketSend
Request is an AsyncSocketSend object.
@ SocketReceive
Request is an AsyncSocketReceive object.
@ SocketAccept
Request is an AsyncSocketAccept object.
@ FileWrite
Request is an AsyncFileWrite object.
@ LoopTimeout
Request is an AsyncLoopTimeout object.
@ ProcessExit
Request is an AsyncProcessExit object.
@ FileRead
Request is an AsyncFileRead object.
@ FilePoll
Request is an AsyncFilePoll object.
@ LoopWakeUp
Request is an AsyncLoopWakeUp object.
@ SocketClose
Request is an AsyncSocketClose object.
@ SocketConnect
Request is an AsyncSocketConnect object.
@ LoopWork
Request is an AsyncLoopWork object.
Base class for all async results (argument of completion callbacks).
Definition: Async.h:239
void reactivateRequest(bool value)
Ask the event loop to re-activate this request after it was already completed.
const SC::Result & isValid() const
Check if the returnCode of this result is valid.
Definition: Async.h:251
AsyncResult(AsyncRequest &request)
Constructs an async result from a request.
Definition: Async.h:244
AsyncResult(AsyncRequest &request, SC::Result &&res)
Constructs an async result from a request and a result.
Definition: Async.h:241
Helper holding CompletionData for a specific AsyncRequest-derived class.
Definition: Async.h:269
Completion data for AsyncSocketAccept.
Definition: Async.h:489
Callback result for AsyncSocketAccept.
Definition: Async.h:495
Starts a socket accept operation, obtaining a new socket from a listening socket.
Definition: Async.h:484
Function< void(Result &)> callback
Called when a new socket has been accepted.
Definition: Async.h:512
SC::Result start(AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor)
Starts a socket accept operation, that returns a new socket connected to the given listening endpoint...
Starts a socket close operation.
Definition: Async.h:690
SC::Result start(AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor)
Starts a socket close operation.
int code
Return code of close socket operation.
Definition: Async.h:707
Function< void(Result &)> callback
Callback called after fully closing the socket.
Definition: Async.h:709
Starts a socket connect operation, connecting to a remote endpoint.
Definition: Async.h:538
Function< void(Result &)> callback
Called after socket is finally connected to endpoint.
Definition: Async.h:556
SC::Result start(AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor, SocketIPAddress ipAddress)
Starts a socket connect operation.
Completion data for AsyncSocketReceive.
Definition: Async.h:637
Callback result for AsyncSocketReceive.
Definition: Async.h:644
SC::Result get(Span< char > &outData)
Get a Span of the actually read data.
Definition: Async.h:650
Starts a socket receive operation, receiving bytes from a remote endpoint.
Definition: Async.h:632
SC::Result start(AsyncEventLoop &eventLoop)
Starts a socket receive operation.
SC::Result start(AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor, Span< char > data)
Starts a socket receive operation.
Span< char > buffer
The writeable span of memory where to data will be written.
Definition: Async.h:675
Function< void(Result &)> callback
Called after data has been received.
Definition: Async.h:673
Completion data for AsyncSocketSend.
Definition: Async.h:582
Starts a socket send operation, sending bytes to a remote endpoint.
Definition: Async.h:577
Function< void(Result &)> callback
Called when socket is ready to send more data.
Definition: Async.h:605
SC::Result start(AsyncEventLoop &eventLoop)
Starts a socket send operation.
Span< const char > buffer
Span of bytes to send.
Definition: Async.h:607
SocketDescriptor::Handle handle
The socket to send data to.
Definition: Async.h:608
SC::Result start(AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor, Span< const char > data)
Starts a socket send operation.
Holds (reference to) a SC::ThreadPool and SC::ThreadPool::Task to execute an SC::AsyncRequest in a ba...
Definition: Async.h:285
Create an async Callback result for a given AsyncRequest-derived class.
Definition: Async.h:309
Atomic variables (only for int and bool for now).
Definition: Atomic.h:97
An automatically reset event object to synchronize two threads.
Definition: Threading.h:174
File Descriptor (use SC::File to open and use it with strings and buffers).
Definition: FileDescriptor.h:52
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:19
Hides implementation details from public headers (static PIMPL).
Definition: OpaqueObject.h:77
Definition: ProcessDescriptor.h:44
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
Low-level OS socket handle.
Definition: SocketDescriptor.h:147
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition: SocketDescriptor.h:73
@ AddressFamilyIPV4
IP Address is IPV4.
Definition: SocketDescriptor.h:74
Native representation of an IP Address.
Definition: SocketDescriptor.h:110
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
A native OS thread.
Definition: Threading.h:118
Simple thread pool that executes tasks in a fixed number of worker threads.
Definition: ThreadPool.h:41
A small task containing a function to execute that can be queued in the thread pool.
Definition: ThreadPool.h:19
Absolute time as realtime or monotonically increasing clock.
Definition: Time.h:114
Type-safe wrapper of uint64 used to represent milliseconds.
Definition: Time.h:44
Represent monotonically increasing time (use Monotonic::now for current time)
Definition: Time.h:180