Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
AsyncFibers.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_ASYNC_FIBERS
7#define SC_EXPORT_LIBRARY_ASYNC_FIBERS 0
8#endif
9#define SC_ASYNC_FIBERS_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_ASYNC_FIBERS)
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(AsyncFibersAssert, SC_ASYNC_FIBERS_EXPORT);
24
25#define SC_ASYNC_FIBERS_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::AsyncFibersAssert, e)
26#define SC_ASYNC_FIBERS_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::AsyncFibersAssert, e)
27#define SC_ASYNC_FIBERS_TRUST_RESULT(expression) SC_ASYNC_FIBERS_ASSERT_RELEASE(expression)
28
29struct AsyncFiberIO;
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
92struct SC_ASYNC_FIBERS_EXPORT AsyncFiberIO
93{
94 AsyncFiberIO(FiberScheduler& fiberScheduler, AsyncEventLoop& asyncEventLoop,
95 Span<AsyncFiberCommand> commandStorage = {});
97
98 AsyncFiberIO(const AsyncFiberIO&) = delete;
99 AsyncFiberIO& operator=(const AsyncFiberIO&) = delete;
100 AsyncFiberIO(AsyncFiberIO&&) = delete;
101 AsyncFiberIO& operator=(AsyncFiberIO&&) = delete;
102
103 [[nodiscard]] FiberScheduler& fiberScheduler();
104 [[nodiscard]] const FiberScheduler& fiberScheduler() const;
105 [[nodiscard]] AsyncEventLoop& asyncEventLoop();
106 [[nodiscard]] const AsyncEventLoop& asyncEventLoop() const;
107 [[nodiscard]] bool isOwnerThread() const;
108
109 Result run();
110 Result runOnce();
111 Result runNoWait();
112 Result runUntilComplete();
113 Result runUntilIdle();
114 Result runOwner();
115 Result runOwnerOnce();
116 Result runOwnerNoWait();
117 Result runOwnerUntilComplete();
118 Result runOwnerUntilIdle();
119
120 Result cancelAll();
121
122 Result sleep(TimeMs duration);
123 Result accept(const SocketDescriptor& serverSocket, SocketDescriptor& outClient);
124 Result connect(const SocketDescriptor& socket, SocketIPAddress address);
125 Result send(const SocketDescriptor& socket, Span<const char> data, AsyncFiberSocketSendResult* outResult = nullptr);
126 Result receive(const SocketDescriptor& socket, Span<char> buffer, AsyncFiberSocketReceiveResult& outResult);
127 Result sendAll(const SocketDescriptor& socket, Span<const char> data,
128 AsyncFiberSocketSendResult* outResult = nullptr);
129 Result sendTo(const SocketDescriptor& socket, SocketIPAddress address, Span<const char> data,
130 AsyncFiberSocketSendResult* outResult = nullptr);
131 Result receiveFrom(const SocketDescriptor& socket, Span<char> buffer, AsyncFiberSocketReceiveFromResult& outResult);
132 Result fileRead(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult);
133 Result fileReadAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
134 AsyncFiberFileReadResult& outResult);
135 Result fileReadExact(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult);
136 Result fileReadExactAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
137 AsyncFiberFileReadResult& outResult);
138 Result filePoll(const FileDescriptor& file);
139 Result fileWrite(const FileDescriptor& file, Span<const char> data, AsyncFiberFileWriteResult* outResult = nullptr);
140 Result fileWriteAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
141 AsyncFiberFileWriteResult* outResult = nullptr);
142 Result fileWriteAll(const FileDescriptor& file, Span<const char> data,
143 AsyncFiberFileWriteResult* outResult = nullptr);
144 Result fileWriteAllAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
145 AsyncFiberFileWriteResult* outResult = nullptr);
146 Result fileSend(const FileDescriptor& file, const SocketDescriptor& socket, AsyncFiberFileSendOptions options = {},
147 AsyncFiberFileSendResult* outResult = nullptr);
148 Result processExit(FileDescriptor::Handle process, AsyncFiberProcessExitResult& outResult);
149 Result signal(int signalNumber, AsyncFiberSignalResult& outResult);
150
151 private:
152 FiberScheduler& scheduler;
153 AsyncEventLoop& eventLoop;
154
155 Span<AsyncFiberCommand> commands;
156 size_t commandHead = 0;
157 size_t commandCount = 0;
158 mutable Atomic<int32_t> commandLock = 0;
159
160 Atomic<int32_t> pendingOperations = 0;
161 uint64_t ownerThreadID = 0;
162
163 Result checkOwnerThread() const;
164 Result checkFiberContext() const;
165 void operationStarted();
166 void operationFinished();
167 void lockCommands() const;
168 void unlockCommands() const;
169 Result enqueueCommand(AsyncFiberCommand& command);
170 Result drainCommandQueue();
171 bool hasPendingCommands() const;
172 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
173 uint64_t offset, bool useOffset);
174 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
175 uint64_t offset, bool useOffset, AsyncFileRead& request);
176 Result fileReadExactImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
177 uint64_t offset, bool useOffset);
178 Result fileWriteImpl(const FileDescriptor& file, Span<const char> data, AsyncFiberFileWriteResult* outResult,
179 uint64_t offset, bool useOffset);
180 Result startOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
181 Function<Result(AsyncEventLoop&)>& startProcedure);
182 Result executeStartCommand(void* startState);
183 Result waitForOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
184 void* startState = nullptr);
185 Result stopOperation(FiberCounter& operationCounter, AsyncRequest& request);
186 Result executeStopCommand(void* stopState);
187};
188} // namespace SC
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1486
Definition AsyncFibers.h:32
Definition AsyncFibers.h:56
Definition AsyncFibers.h:67
Definition AsyncFibers.h:74
Definition AsyncFibers.h:62
Synchronous-looking fiber I/O wrapper around an externally owned AsyncEventLoop.
Definition AsyncFibers.h:93
Definition AsyncFibers.h:80
Definition AsyncFibers.h:85
Definition AsyncFibers.h:50
Definition AsyncFibers.h:44
Definition AsyncFibers.h:39
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
Counter used to suspend fibers until a group of operations completes.
Definition Fibers.h:1264
Cooperative fiber scheduler with explicit workers and caller-owned storage.
Definition Fibers.h:1545
[UniqueHandleDeclaration2Snippet]
Definition File.h:130
Low-level OS socket handle.
Definition Socket.h:167
Native representation of an IP Address.
Definition Socket.h:114