Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
FibersAsync.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Common/CompilerMacrosExport.h"
6#ifndef SC_EXPORT_LIBRARY_FIBERS_ASYNC
7#define SC_EXPORT_LIBRARY_FIBERS_ASYNC 0
8#endif
9#define SC_FIBER_ASYNC_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_FIBERS_ASYNC)
10
11#include "../Async/Async.h"
12#include "../Common/Assert.h"
13#include "../Fibers/Fibers.h"
14#include "../Threading/Atomic.h"
15
18
21namespace SC
22{
23SC_DECLARE_ASSERT_PROVIDER(FiberAsyncAssert, SC_FIBER_ASYNC_EXPORT);
24
25#define SC_FIBER_ASYNC_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::FiberAsyncAssert, e)
26#define SC_FIBER_ASYNC_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::FiberAsyncAssert, e)
27#define SC_FIBER_ASYNC_TRUST_RESULT(expression) SC_FIBER_ASYNC_ASSERT_RELEASE(expression)
28
29struct FiberAsyncIO;
30
32{
33 using Procedure = Function<Result()>;
34
35 Procedure execute;
36};
37
39{
40 size_t numBytes = 0;
41};
42
44{
45 Span<char> data;
46 bool disconnected = false;
47};
48
50{
51 Span<char> data;
52 SocketIPAddress sourceAddress;
53};
54
56{
57 Span<char> data;
58 bool endOfFile = false;
59};
60
62{
63 size_t numBytes = 0;
64};
65
67{
68 int64_t offset = 0;
69 size_t length = 0;
70 size_t pipeSize = 0;
71};
72
74{
75 size_t bytesTransferred = 0;
76 bool usedZeroCopy = false;
77};
78
80{
81 int exitStatus = -1;
82};
83
85{
86 int signalNumber = 0;
87 uint32_t deliveryCount = 0;
88};
89
91struct SC_FIBER_ASYNC_EXPORT FiberAsyncIO
92{
93 FiberAsyncIO(FiberScheduler& fiberScheduler, AsyncEventLoop& asyncEventLoop,
94 Span<FiberAsyncCommand> commandStorage = {});
95
96 [[nodiscard]] FiberScheduler& fiberScheduler();
97 [[nodiscard]] const FiberScheduler& fiberScheduler() const;
98 [[nodiscard]] AsyncEventLoop& asyncEventLoop();
99 [[nodiscard]] const AsyncEventLoop& asyncEventLoop() const;
100 [[nodiscard]] bool isOwnerThread() const;
101
102 Result run();
103 Result runOnce();
104 Result runNoWait();
105 Result runUntilComplete();
106 Result runUntilIdle();
107 Result runOwner();
108 Result runOwnerOnce();
109 Result runOwnerNoWait();
110 Result runOwnerUntilComplete();
111 Result runOwnerUntilIdle();
112
113 Result cancelAll();
114
115 Result sleep(TimeMs duration);
116 Result accept(const SocketDescriptor& serverSocket, SocketDescriptor& outClient);
117 Result connect(const SocketDescriptor& socket, SocketIPAddress address);
118 Result send(const SocketDescriptor& socket, Span<const char> data, FiberAsyncSocketSendResult* outResult = nullptr);
119 Result receive(const SocketDescriptor& socket, Span<char> buffer, FiberAsyncSocketReceiveResult& outResult);
120 Result sendAll(const SocketDescriptor& socket, Span<const char> data,
121 FiberAsyncSocketSendResult* outResult = nullptr);
122 Result sendTo(const SocketDescriptor& socket, SocketIPAddress address, Span<const char> data,
123 FiberAsyncSocketSendResult* outResult = nullptr);
124 Result receiveFrom(const SocketDescriptor& socket, Span<char> buffer, FiberAsyncSocketReceiveFromResult& outResult);
125 Result fileRead(const FileDescriptor& file, Span<char> buffer, FiberAsyncFileReadResult& outResult);
126 Result fileReadAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
127 FiberAsyncFileReadResult& outResult);
128 Result fileReadExact(const FileDescriptor& file, Span<char> buffer, FiberAsyncFileReadResult& outResult);
129 Result fileReadExactAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
130 FiberAsyncFileReadResult& outResult);
131 Result filePoll(const FileDescriptor& file);
132 Result fileWrite(const FileDescriptor& file, Span<const char> data, FiberAsyncFileWriteResult* outResult = nullptr);
133 Result fileWriteAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
134 FiberAsyncFileWriteResult* outResult = nullptr);
135 Result fileWriteAll(const FileDescriptor& file, Span<const char> data,
136 FiberAsyncFileWriteResult* outResult = nullptr);
137 Result fileWriteAllAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
138 FiberAsyncFileWriteResult* outResult = nullptr);
139 Result fileSend(const FileDescriptor& file, const SocketDescriptor& socket, FiberAsyncFileSendOptions options = {},
140 FiberAsyncFileSendResult* outResult = nullptr);
141 Result processExit(FileDescriptor::Handle process, FiberAsyncProcessExitResult& outResult);
142 Result signal(int signalNumber, FiberAsyncSignalResult& outResult);
143
144 private:
145 FiberScheduler& scheduler;
146 AsyncEventLoop& eventLoop;
147
148 Span<FiberAsyncCommand> commands;
149 size_t commandHead = 0;
150 size_t commandCount = 0;
151 mutable Atomic<int32_t> commandLock = 0;
152
153 Atomic<int32_t> pendingOperations = 0;
154 uint64_t ownerThreadID = 0;
155
156 Result checkOwnerThread() const;
157 void operationStarted();
158 void operationFinished();
159 void lockCommands() const;
160 void unlockCommands() const;
161 Result enqueueCommand(FiberAsyncCommand& command);
162 Result drainCommandQueue();
163 bool hasPendingCommands() const;
164 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, FiberAsyncFileReadResult& outResult,
165 uint64_t offset, bool useOffset);
166 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, FiberAsyncFileReadResult& outResult,
167 uint64_t offset, bool useOffset, AsyncFileRead& request);
168 Result fileReadExactImpl(const FileDescriptor& file, Span<char> buffer, FiberAsyncFileReadResult& outResult,
169 uint64_t offset, bool useOffset);
170 Result fileWriteImpl(const FileDescriptor& file, Span<const char> data, FiberAsyncFileWriteResult* outResult,
171 uint64_t offset, bool useOffset);
172 Result startOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
173 Function<Result(AsyncEventLoop&)>& startProcedure);
174 Result executeStartCommand(void* startState);
175 Result waitForOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
176 void* startState = nullptr);
177 Result stopOperation(FiberCounter& operationCounter, AsyncRequest& request);
178 Result executeStopCommand(void* stopState);
179};
180} // namespace SC
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1486
Starts a file read operation, reading bytes from a file (or pipe).
Definition Async.h:819
Base class for all async requests, holding state and type.
Definition Async.h:138
Atomic variables (only for int and bool for now).
Definition Atomic.h:42
Definition FibersAsync.h:32
Definition FibersAsync.h:56
Definition FibersAsync.h:67
Definition FibersAsync.h:74
Definition FibersAsync.h:62
Synchronous-looking fiber I/O wrapper around an externally owned AsyncEventLoop.
Definition FibersAsync.h:92
Definition FibersAsync.h:80
Definition FibersAsync.h:85
Definition FibersAsync.h:50
Definition FibersAsync.h:44
Definition FibersAsync.h:39
Counter used to suspend fibers until a group of operations completes.
Definition Fibers.h:712
Cooperative fiber scheduler with explicit workers and caller-owned storage.
Definition Fibers.h:978
[UniqueHandleDeclaration2Snippet]
Definition File.h:130
Low-level OS socket handle.
Definition Socket.h:167
Native representation of an IP Address.
Definition Socket.h:114