Skip to content

Commit

Permalink
Async: Hide KernelQueue and KernelEvents inside Internal
Browse files Browse the repository at this point in the history
This allows to expose less details to the users.

This commit contains also a refactoring with the following renames:
- KernelQueue -> KernelEvents
- Internal    -> KernelQueue
- Private     -> Internal
  • Loading branch information
Pagghiu committed May 19, 2024
1 parent 35f5fc5 commit af1e0f0
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 372 deletions.
261 changes: 130 additions & 131 deletions Libraries/Async/Async.cpp

Large diffs are not rendered by default.

52 changes: 8 additions & 44 deletions Libraries/Async/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,53 +920,16 @@ struct SC::AsyncEventLoop
/// @return true if liburing has been loaded, false otherwise (and on any non-Linux os)
[[nodiscard]] static bool tryLoadingLiburing();

private:
struct Private;

struct PrivateDefinition
{
static constexpr int Windows = 336;
static constexpr int Apple = 360;
static constexpr int Default = 344;

static constexpr size_t Alignment = 8;

using Object = Private;
};

public:
using PrivateOpaque = OpaqueObject<PrivateDefinition>;
struct Internal;

private:
PrivateOpaque privateOpaque;
Private& privateSelf;

#if SC_PLATFORM_LINUX
struct InternalPosix;
struct KernelQueuePosix;
struct InternalIoURing;
struct KernelQueueIoURing;
struct Internal;
struct KernelQueue;
#elif SC_PLATFORM_APPLE
struct InternalPosix;
struct KernelQueuePosix;
using Internal = InternalPosix;
using KernelQueue = KernelQueuePosix;
#elif SC_PLATFORM_WINDOWS
struct Internal;
struct KernelQueue;
#else
struct Internal;
struct KernelQueue;
#endif
struct InternalDefinition
{
static constexpr int Windows = 184;
static constexpr int Apple = 104;
static constexpr int Default = 336;
static constexpr int Windows = 520;
static constexpr int Apple = 464;
static constexpr int Default = 680;

static constexpr size_t Alignment = alignof(void*);
static constexpr size_t Alignment = 8;

using Object = Internal;
};
Expand All @@ -975,8 +938,9 @@ struct SC::AsyncEventLoop
using InternalOpaque = OpaqueObject<InternalDefinition>;

private:
InternalOpaque internal;
Internal& internalSelf;
InternalOpaque internalOpaque;
Internal& internal;

friend struct AsyncRequest;
friend struct AsyncFileWrite;
friend struct AsyncFileRead;
Expand Down
12 changes: 6 additions & 6 deletions Libraries/Async/Internal/AsyncEmscripten.inl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Stefano Cristiano
// SPDX-License-Identifier: MIT
#include "../Async.h"
#include "AsyncPrivate.h"
#include "AsyncInternal.h"

struct SC::AsyncEventLoop::Internal
struct SC::AsyncEventLoop::KernelQueue
{
~Internal() { SC_TRUST_RESULT(close()); }
~KernelQueue() { SC_TRUST_RESULT(close()); }
[[nodiscard]] Result close() { return Result(true); }
[[nodiscard]] Result createEventLoop(AsyncEventLoop::Options) { return Result(true); }
[[nodiscard]] Result createSharedWatchers(AsyncEventLoop&) { return Result(true); }
Expand All @@ -15,12 +15,12 @@ struct SC::AsyncEventLoop::Internal
[[nodiscard]] Result makesSenseToRunInThreadPool(AsyncRequest&) { return Result(true); }
};

struct SC::AsyncEventLoop::KernelQueue
struct SC::AsyncEventLoop::KernelEvents
{
KernelQueue(Internal&) {}
KernelEvents(KernelQueue&) {}
uint32_t getNumEvents() const { return 0; }

[[nodiscard]] Result syncWithKernel(AsyncEventLoop&, Private::SyncMode) { return Result(true); }
[[nodiscard]] Result syncWithKernel(AsyncEventLoop&, Internal::SyncMode) { return Result(true); }
[[nodiscard]] Result validateEvent(uint32_t, bool&) { return Result(true); }

[[nodiscard]] AsyncRequest* getAsyncRequest(uint32_t) const { return nullptr; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,45 @@
#include "../../Containers/IntrusiveDoubleLinkedList.h"
#include "ThreadSafeLinkedList.h"

struct SC::AsyncEventLoop::Private
struct SC::AsyncEventLoop::Internal
{
AsyncEventLoop* eventLoop = nullptr;
#if SC_PLATFORM_LINUX
struct KernelQueuePosix;
struct KernelEventsPosix;
struct KernelQueueIoURing;
struct KernelEventsIoURing;
struct KernelQueue;
struct KernelEvents;
#elif SC_PLATFORM_APPLE
struct KernelQueuePosix;
struct KernelEventsPosix;
using KernelQueue = KernelQueuePosix;
using KernelEvents = KernelEventsPosix;
#elif SC_PLATFORM_WINDOWS
struct KernelQueue;
struct KernelEvents;
#else
struct KernelQueue;
struct KernelEvents;
#endif

struct KernelQueueDefinition
{
static constexpr int Windows = 184;
static constexpr int Apple = 104;
static constexpr int Default = 336;

static constexpr size_t Alignment = alignof(void*);

using Object = KernelQueue;
};

using KernelQueueOpaque = OpaqueObject<KernelQueueDefinition>;

// Using opaque to allow defining KernelQueue class later
KernelQueueOpaque kernelQueue;

AsyncEventLoop* loop = nullptr;

Atomic<bool> wakeUpPending = false;

Expand Down Expand Up @@ -57,7 +93,7 @@ struct SC::AsyncEventLoop::Private

void invokeExpiredTimers();
void updateTime();
void executeTimers(KernelQueue& queue, const Time::HighResolutionCounter& nextTimer);
void executeTimers(KernelEvents& kernelEvents, const Time::HighResolutionCounter& nextTimer);

[[nodiscard]] Result cancelAsync(AsyncRequest& async);

Expand All @@ -68,21 +104,23 @@ struct SC::AsyncEventLoop::Private
[[nodiscard]] Result queueSubmission(AsyncRequest& async, AsyncTask* task);

// Phases
[[nodiscard]] Result stageSubmission(KernelQueue& queue, AsyncRequest& async);
[[nodiscard]] Result setupAsync(KernelQueue& queue, AsyncRequest& async);
[[nodiscard]] Result teardownAsync(KernelQueue& queue, AsyncRequest& async);
[[nodiscard]] Result activateAsync(KernelQueue& queue, AsyncRequest& async);
[[nodiscard]] Result cancelAsync(KernelQueue& queue, AsyncRequest& async);
[[nodiscard]] Result completeAsync(KernelQueue& queue, AsyncRequest& async, Result&& returnCode, bool& reactivate);
[[nodiscard]] Result stageSubmission(KernelEvents& kernelEvents, AsyncRequest& async);
[[nodiscard]] Result setupAsync(KernelEvents& kernelEvents, AsyncRequest& async);
[[nodiscard]] Result teardownAsync(KernelEvents& kernelEvents, AsyncRequest& async);
[[nodiscard]] Result activateAsync(KernelEvents& kernelEvents, AsyncRequest& async);
[[nodiscard]] Result cancelAsync(KernelEvents& kernelEvents, AsyncRequest& async);
[[nodiscard]] Result completeAsync(KernelEvents& kernelEvents, AsyncRequest& async, Result&& returnCode,
bool& reactivate);

struct SetupAsyncPhase;
struct TeardownAsyncPhase;
struct ActivateAsyncPhase;
struct CancelAsyncPhase;
struct CompleteAsyncPhase;
[[nodiscard]] Result completeAndEventuallyReactivate(KernelQueue& queue, AsyncRequest& async, Result&& returnCode);
[[nodiscard]] Result completeAndEventuallyReactivate(KernelEvents& kernelEvents, AsyncRequest& async,
Result&& returnCode);

void reportError(KernelQueue& queue, AsyncRequest& async, Result&& returnCode);
void reportError(KernelEvents& kernelEvents, AsyncRequest& async, Result&& returnCode);

enum class SyncMode
{
Expand All @@ -92,9 +130,9 @@ struct SC::AsyncEventLoop::Private

[[nodiscard]] Result runStep(SyncMode syncMode);

void runStepExecuteCompletions(KernelQueue& queue);
void runStepExecuteManualCompletions(KernelQueue& queue);
void runStepExecuteManualThreadPoolCompletions(KernelQueue& queue);
void runStepExecuteCompletions(KernelEvents& kernelEvents);
void runStepExecuteManualCompletions(KernelEvents& kernelEvents);
void runStepExecuteManualThreadPoolCompletions(KernelEvents& kernelEvents);

friend struct AsyncRequest;

Expand Down
Loading

0 comments on commit af1e0f0

Please sign in to comment.