Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::AsyncLoopWakeUp Struct Reference

Starts a wake-up operation, allowing threads to execute callbacks on loop thread. More...

#include <Async.h>

Inheritance diagram for SC::AsyncLoopWakeUp:
SC::AsyncRequest

Public Types

using CompletionData = AsyncCompletionData
 Completion data for AsyncLoopWakeUp. More...
 
using Result = AsyncResultOf< AsyncLoopWakeUp, CompletionData >
 Callback result for AsyncLoopWakeUp. More...
 
- Public Types inherited from SC::AsyncRequest
enum class  Type : uint8_t {
  LoopTimeout ,
  LoopWakeUp ,
  LoopWork ,
  ProcessExit ,
  SocketAccept ,
  SocketConnect ,
  SocketSend ,
  SocketReceive ,
  SocketClose ,
  FileRead ,
  FileWrite ,
  FileClose ,
  FilePoll
}
 Type of async request. More...
 

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, EventObject *eventObject=nullptr)
 Starts a wake up request, that will be fulfilled when an external thread calls AsyncLoopWakeUp::wakeUp. More...
 
SC::Result wakeUp ()
 Wakes up event loop, scheduling AsyncLoopWakeUp::callback on next AsyncEventLoop::run (or its variations) More...
 
- Public Member Functions inherited from SC::AsyncRequest
void setDebugName (const char *newDebugName)
 
AsyncEventLoopgetEventLoop () const
 Get the event loop associated with this AsyncRequest. More...
 
 AsyncRequest (Type type)
 Constructs a free async request of given type. More...
 
Result stop ()
 Stops the async operation. More...
 

Public Attributes

Function< void(Result &)> callback
 Callback called by SC::AsyncEventLoop::run after SC::AsyncLoopWakeUp::wakeUp. More...
 
- Public Attributes inherited from SC::AsyncRequest
AsyncRequestnext = nullptr
 
AsyncRequestprev = nullptr
 

Friends

struct AsyncEventLoop
 

Additional Inherited Members

- Protected Member Functions inherited from SC::AsyncRequest
Result validateAsync ()
 
Result queueSubmission (AsyncEventLoop &eventLoop)
 
Result queueSubmission (AsyncEventLoop &eventLoop, ThreadPool &threadPool, AsyncTask &task)
 
- Static Protected Member Functions inherited from SC::AsyncRequest
static void updateTime (AsyncEventLoop &loop)
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncEventLoopeventLoop = nullptr
 
AsyncTaskasyncTask = nullptr
 

Detailed Description

Starts a wake-up operation, allowing threads to execute callbacks on loop thread.


SC::AsyncLoopWakeUp::callback will be invoked on the thread running SC::AsyncEventLoop::run (or its variations) after SC::AsyncLoopWakeUp::wakeUp has been called.

Note
There is no guarantee that after calling AsyncLoopWakeUp::start the callback has actually finished execution. An optional SC::EventObject passed to SC::AsyncLoopWakeUp::start can be used for synchronization
// Assuming an already created (and running) AsyncEventLoop named eventLoop
// ...
// This code runs on some different thread from the one calling SC::AsyncEventLoop::run.
// The callback is invoked from the thread calling SC::AsyncEventLoop::run
AsyncLoopWakeUp wakeUp; // Memory lifetime must be valid until callback is called
{
console.print("My wakeUp has been called!");
};
SC_TRY(wakeUp.start(eventLoop));
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:47
SC::Result wakeUp()
Wakes up event loop, scheduling AsyncLoopWakeUp::callback on next AsyncEventLoop::run (or its variati...
AsyncResultOf< AsyncLoopWakeUp, CompletionData > Result
Callback result for AsyncLoopWakeUp.
Definition: Async.h:333

An EventObject can be wait-ed to synchronize further actions from the thread invoking the wake up request, ensuring that the callback has finished its execution.

// Assuming an already created (and running) AsyncEventLoop named eventLoop
// ...
// This code runs on some different thread from the one calling SC::AsyncEventLoop::run.
// The callback is invoked from the thread calling SC::AsyncEventLoop::run
AsyncLoopWakeUp wakeUpWaiting; // Memory lifetime must be valid until callback is called
wakeUpWaiting.callback = [&](AsyncLoopWakeUp::Result&)
{
console.print("My wakeUp has been called!");
};
EventObject eventObject;
SC_TRY(wakeUpWaiting.start(eventLoop, &eventObject));
eventObject.wait(); // Wait until callback has been fully run inside event loop thread
// From here on we know for sure that callback has been called

Member Typedef Documentation

◆ CompletionData

◆ Result

Member Function Documentation

◆ start()

SC::Result SC::AsyncLoopWakeUp::start ( AsyncEventLoop eventLoop,
EventObject eventObject = nullptr 
)

Starts a wake up request, that will be fulfilled when an external thread calls AsyncLoopWakeUp::wakeUp.

Parameters
eventLoopThe event loop where queuing this async request
eventObjectOptional EventObject to synchronize external threads waiting until the callback is finished.
Returns
Valid Result if the request has been successfully queued

◆ wakeUp()

SC::Result SC::AsyncLoopWakeUp::wakeUp ( )

Wakes up event loop, scheduling AsyncLoopWakeUp::callback on next AsyncEventLoop::run (or its variations)

Member Data Documentation

◆ callback

Function<void(Result&)> SC::AsyncLoopWakeUp::callback

The documentation for this struct was generated from the following file: