uvco 0.1
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
uvco::Loop Class Reference

#include <loop.h>

Collaboration diagram for uvco::Loop:
Collaboration graph
[legend]

Public Member Functions

 Loop (Scheduler::RunMode mode=Scheduler::RunMode::Deferred)
 
 Loop (const Loop &)=delete
 
 Loop (Loop &&)=delete
 
Loopoperator= (const Loop &)=delete
 
Loopoperator= (Loop &&)=delete
 
 ~Loop ()
 
uv_loop_t * uvloop () const
 Get a non-owned pointer to the loop.
 

Static Public Member Functions

static void enqueue (std::coroutine_handle<> handle)
 

Private Member Functions

void run ()
 
void runOne ()
 

Static Private Member Functions

static SchedulercurrentScheduler ()
 

Private Attributes

std::unique_ptr< uv_loop_t > loop_
 
std::unique_ptr< Schedulerscheduler_
 

Static Private Attributes

static LoopdefaultLoop = nullptr
 

Friends

void runLoop (Loop &)
 

Detailed Description

Not used by user code! Use runMain() for the top-level interface.

A wrapper around a libuv event loop. uvloop() returns a reference to the loop, and run() starts the event loop. enqueue() schedules a coroutine to run on the default loop at a later time, enabling any part of uvco to easily schedule work on the current loop.

Typically this is only used by uvco's internal machinery. User code will pass around a reference to the loop.

Constructor & Destructor Documentation

◆ Loop() [1/3]

uvco::Loop::Loop ( Scheduler::RunMode  mode = Scheduler::RunMode::Deferred)
explicit
18 : loop_{std::make_unique<uv_loop_t>()},
19 scheduler_{std::make_unique<Scheduler>(mode)} {
20
21 if (defaultLoop != nullptr) {
22 throw UvcoException(UV_EBUSY,
23 "Loop::Loop(): only one loop can be created.");
24 }
25
26 uv_loop_init(loop_.get());
27 uv_loop_set_data(loop_.get(), scheduler_.get());
28 scheduler_->setUpLoop(loop_.get());
29 defaultLoop = this;
30}
std::unique_ptr< uv_loop_t > loop_
Definition loop.h:59
std::unique_ptr< Scheduler > scheduler_
Definition loop.h:60
static Loop * defaultLoop
Definition loop.h:47

◆ Loop() [2/3]

uvco::Loop::Loop ( const Loop )
delete

◆ Loop() [3/3]

uvco::Loop::Loop ( Loop &&  )
delete

◆ ~Loop()

uvco::Loop::~Loop ( )
32 {
33 scheduler_->close();
34
35 // Run loop again so that all handles are closed.
36 // A single turn is enough.
37 runOne();
38
39 // Now run all scheduled handles
40 scheduler_->runAll();
41
42 const uv_status status = uv_loop_close(loop_.get());
43 if (0 != status) {
44 fmt::print(stderr,
45 "Loop::~Loop(): uv_loop_close() failed; there were "
46 "still resources on the loop: {}\n",
47 uv_strerror(status));
48 }
49 defaultLoop = nullptr;
50}
void runOne()
Definition loop.cc:52
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22

Member Function Documentation

◆ currentScheduler()

Scheduler & uvco::Loop::currentScheduler ( )
staticprivate
66 {
67 if (defaultLoop == nullptr) {
68 throw UvcoException(UV_EINVAL, "Loop::getDefaultLoop(): no loop created.");
69 }
70 return *defaultLoop->scheduler_;
71}

◆ enqueue()

void uvco::Loop::enqueue ( std::coroutine_handle<>  handle)
static
73 {
74 currentScheduler().enqueue(handle);
75 // If any handles are present, ensure that uv_run returns from waiting for I/O
76 // soon.
77 uv_stop(defaultLoop->uvloop());
78}
static Scheduler & currentScheduler()
Definition loop.cc:66
uv_loop_t * uvloop() const
Get a non-owned pointer to the loop.
Definition loop.cc:62
void enqueue(std::coroutine_handle<> handle)
Schedule a coroutine for resumption.
Definition scheduler.cc:60

◆ operator=() [1/2]

Loop & uvco::Loop::operator= ( const Loop )
delete

◆ operator=() [2/2]

Loop & uvco::Loop::operator= ( Loop &&  )
delete

◆ run()

void uvco::Loop::run ( )
private

Run the event loop. This will serve all promises initialized before calling it.

54 {
55 while (!scheduler_->empty() || uv_loop_alive(loop_.get()) != 0) {
56 runOne();
57 // Run any left-over coroutines, and check if they schedule callbacks.
58 scheduler_->runAll();
59 }
60}

◆ runOne()

void uvco::Loop::runOne ( )
private
52{ uv_run(loop_.get(), UV_RUN_ONCE); }

◆ uvloop()

uv_loop_t * uvco::Loop::uvloop ( ) const

Get a non-owned pointer to the loop.

62{ return loop_.get(); }

Friends And Related Symbol Documentation

◆ runLoop

void runLoop ( Loop loop)
friend
32{ loop.run(); }

Member Data Documentation

◆ defaultLoop

Loop * uvco::Loop::defaultLoop = nullptr
staticprivate

◆ loop_

std::unique_ptr<uv_loop_t> uvco::Loop::loop_
private

◆ scheduler_

std::unique_ptr<Scheduler> uvco::Loop::scheduler_
private

The documentation for this class was generated from the following files: