uvco 0.1
Loading...
Searching...
No Matches
loop.h
Go to the documentation of this file.
1// uvco (c) 2024 Lewin Bormann. See LICENSE for specific terms.
2
3#pragma once
4
5#include <uv.h>
6
7#include "uvco/exception.h"
9
10#include <coroutine>
11
12namespace uvco {
13
16
26class Loop {
27public:
28 // Don't use this constructor. Use `runMain()` instead.
29 Loop();
30 Loop(const Loop &) = delete;
31 Loop(Loop &&) = delete;
32 Loop &operator=(const Loop &) = delete;
33 Loop &operator=(Loop &&) = delete;
34 ~Loop();
35
37 [[nodiscard]] uv_loop_t *uvloop() const;
38
39 // Enqueue a suspended coroutine_handle for later resumption.
40 static void enqueue(std::coroutine_handle<> handle);
41 // Remove a handle from the event loop. Note: if the same handle is posted
42 // again later, it may still be resumed.
43 static void cancel(std::coroutine_handle<> handle);
44
45 static std::coroutine_handle<> getNext();
46
47private:
48 // The default loop is the only loop that can be created. It is set/unset by
49 // the constructor/destructor.
52
53 friend void runLoop(Loop &);
56 void run();
57 // Run a single turn of the loop.
58 void runOne();
59
60 // Loop and scheduler should be kept at the same
61 // place in memory.
62 mutable uv_loop_t loop_;
64 bool stopped_ = false;
65};
66
68
69} // namespace uvco
void runOne()
Definition loop.cc:66
static Scheduler & currentScheduler()
Definition loop.cc:87
Loop & operator=(const Loop &)=delete
Loop(Loop &&)=delete
uv_loop_t * uvloop() const
Get a non-owned pointer to the loop.
Definition loop.cc:83
Loop()
Definition loop.cc:24
~Loop()
Definition loop.cc:44
Loop(const Loop &)=delete
static void cancel(std::coroutine_handle<> handle)
Definition loop.cc:104
Loop & operator=(Loop &&)=delete
friend void runLoop(Loop &)
Definition run.cc:7
bool stopped_
Definition loop.h:64
static std::coroutine_handle getNext()
Definition loop.cc:108
static Loop * defaultLoop
Definition loop.h:50
Scheduler scheduler_
Definition loop.h:63
static void enqueue(std::coroutine_handle<> handle)
Definition loop.cc:94
uv_loop_t loop_
Definition loop.h:62
void run()
Definition loop.cc:68
Definition scheduler.h:45
Definition async_work.cc:18