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#include "AsyncFibersError.h"
16
19
22namespace SC
23{
24SC_DECLARE_ASSERT_PROVIDER(AsyncFibersAssert, SC_ASYNC_FIBERS_EXPORT);
25
26#define SC_ASYNC_FIBERS_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::AsyncFibersAssert, e)
27#define SC_ASYNC_FIBERS_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::AsyncFibersAssert, e)
28#define SC_ASYNC_FIBERS_TRUST_RESULT(expression) SC_ASYNC_FIBERS_ASSERT_RELEASE(expression)
29
30struct AsyncFiberIO;
31
33{
34 using Procedure = Function<Result()>;
35
36 Procedure execute;
37};
38
40{
41 size_t numBytes = 0;
42};
43
45{
46 Span<char> data;
47 bool disconnected = false;
48};
49
51{
52 Span<char> data;
53 SocketIPAddress sourceAddress;
54};
55
57{
58 Span<char> data;
59 bool endOfFile = false;
60};
61
63{
64 size_t numBytes = 0;
65};
66
68{
69 int64_t offset = 0;
70 size_t length = 0;
71 size_t pipeSize = 0;
72};
73
75{
76 size_t bytesTransferred = 0;
77 bool usedZeroCopy = false;
78};
79
81{
82 int exitStatus = -1;
83};
84
86{
87 int signalNumber = 0;
88 uint32_t deliveryCount = 0;
89};
90
93struct SC_ASYNC_FIBERS_EXPORT AsyncFiberIO
94{
95 AsyncFiberIO(FiberScheduler& fiberScheduler, AsyncEventLoop& asyncEventLoop,
96 Span<AsyncFiberCommand> commandStorage = {});
98
99 AsyncFiberIO(const AsyncFiberIO&) = delete;
100 AsyncFiberIO& operator=(const AsyncFiberIO&) = delete;
101 AsyncFiberIO(AsyncFiberIO&&) = delete;
102 AsyncFiberIO& operator=(AsyncFiberIO&&) = delete;
103
104 [[nodiscard]] FiberScheduler& fiberScheduler();
105 [[nodiscard]] const FiberScheduler& fiberScheduler() const;
106 [[nodiscard]] AsyncEventLoop& asyncEventLoop();
107 [[nodiscard]] const AsyncEventLoop& asyncEventLoop() const;
108 [[nodiscard]] bool isOwnerThread() const;
109
110 Result run();
111 Result runOnce();
112 Result runNoWait();
113 Result runUntilComplete();
114 Result runUntilIdle();
115 Result runOwner();
116 Result runOwnerOnce();
117 Result runOwnerNoWait();
118 Result runOwnerUntilComplete();
119 Result runOwnerUntilIdle();
120
121 Result cancelAll();
122
123 Result sleep(TimeMs duration);
124 Result accept(const SocketDescriptor& serverSocket, SocketDescriptor& outClient);
125 Result connect(const SocketDescriptor& socket, SocketIPAddress address);
126 Result send(const SocketDescriptor& socket, Span<const char> data, AsyncFiberSocketSendResult* outResult = nullptr);
127 Result receive(const SocketDescriptor& socket, Span<char> buffer, AsyncFiberSocketReceiveResult& outResult);
128 Result sendAll(const SocketDescriptor& socket, Span<const char> data,
129 AsyncFiberSocketSendResult* outResult = nullptr);
130 Result sendTo(const SocketDescriptor& socket, SocketIPAddress address, Span<const char> data,
131 AsyncFiberSocketSendResult* outResult = nullptr);
132 Result receiveFrom(const SocketDescriptor& socket, Span<char> buffer, AsyncFiberSocketReceiveFromResult& outResult);
133 Result fileRead(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult);
134 Result fileReadAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
135 AsyncFiberFileReadResult& outResult);
136 Result fileReadExact(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult);
137 Result fileReadExactAt(const FileDescriptor& file, uint64_t offset, Span<char> buffer,
138 AsyncFiberFileReadResult& outResult);
139 Result filePoll(const FileDescriptor& file);
140 Result fileWrite(const FileDescriptor& file, Span<const char> data, AsyncFiberFileWriteResult* outResult = nullptr);
141 Result fileWriteAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
142 AsyncFiberFileWriteResult* outResult = nullptr);
143 Result fileWriteAll(const FileDescriptor& file, Span<const char> data,
144 AsyncFiberFileWriteResult* outResult = nullptr);
145 Result fileWriteAllAt(const FileDescriptor& file, uint64_t offset, Span<const char> data,
146 AsyncFiberFileWriteResult* outResult = nullptr);
147 Result fileSend(const FileDescriptor& file, const SocketDescriptor& socket, AsyncFiberFileSendOptions options = {},
148 AsyncFiberFileSendResult* outResult = nullptr);
149 Result processExit(FileDescriptor::Handle process, AsyncFiberProcessExitResult& outResult);
150 Result signal(int signalNumber, AsyncFiberSignalResult& outResult);
151
152 private:
153 FiberScheduler& scheduler;
154 AsyncEventLoop& eventLoop;
155
156 Span<AsyncFiberCommand> commands;
157 size_t commandHead = 0;
158 size_t commandCount = 0;
159 mutable Atomic<int32_t> commandLock = 0;
160
161 Atomic<int32_t> pendingOperations = 0;
162 uint64_t ownerThreadID = 0;
163
164 Result checkOwnerThread() const;
165 Result checkFiberContext() const;
166 void operationStarted();
167 void operationFinished();
168 void lockCommands() const;
169 void unlockCommands() const;
170 Result enqueueCommand(AsyncFiberCommand& command);
171 Result drainCommandQueue();
172 bool hasPendingCommands() const;
173 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
174 uint64_t offset, bool useOffset);
175 Result fileReadImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
176 uint64_t offset, bool useOffset, AsyncFileRead& request);
177 Result fileReadExactImpl(const FileDescriptor& file, Span<char> buffer, AsyncFiberFileReadResult& outResult,
178 uint64_t offset, bool useOffset);
179 Result fileWriteImpl(const FileDescriptor& file, Span<const char> data, AsyncFiberFileWriteResult* outResult,
180 uint64_t offset, bool useOffset);
181 Result startOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
182 Function<Result(AsyncEventLoop&)>& startProcedure);
183 Result executeStartCommand(void* startState);
184 Result waitForOperation(FiberCounter& counter, AsyncRequest& request, Result& operationResult,
185 void* startState = nullptr);
186 Result stopOperation(FiberCounter& operationCounter, AsyncRequest& request);
187 Result executeStopCommand(void* stopState);
188};
189} // namespace SC
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1511
Definition AsyncFibers.h:33
Definition AsyncFibers.h:57
Definition AsyncFibers.h:68
Definition AsyncFibers.h:75
Definition AsyncFibers.h:63
Synchronous-looking fiber I/O wrapper around an externally owned AsyncEventLoop.
Definition AsyncFibers.h:94
Definition AsyncFibers.h:81
Definition AsyncFibers.h:86
Definition AsyncFibers.h:51
Definition AsyncFibers.h:45
Definition AsyncFibers.h:40
Starts a file read operation, reading bytes from a file (or pipe).
Definition Async.h:844
Base class for all async requests, holding state and type.
Definition Async.h:139
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:1368
Cooperative fiber scheduler with explicit workers and caller-owned storage.
Definition Fibers.h:1649
[UniqueHandleDeclaration2Snippet]
Definition File.h:130
Low-level OS socket handle.
Definition Socket.h:207
Native representation of an IP Address.
Definition Socket.h:117