Sleep with await
This commit is contained in:
parent
c8f6aa877c
commit
e3574fac17
@ -26,11 +26,7 @@ using namespace boost::asio;
|
||||
namespace marcelb {
|
||||
namespace asynco {
|
||||
|
||||
|
||||
struct SleepHandle {
|
||||
std::future<void> future;
|
||||
std::shared_ptr<Timer> timer;
|
||||
};
|
||||
class Sleep;
|
||||
|
||||
/**
|
||||
* Asynco runtime
|
||||
@ -106,10 +102,14 @@ public:
|
||||
*/
|
||||
template<typename T>
|
||||
T await(future<T>& r, uint16_t time_us = 10) {
|
||||
while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) {
|
||||
io_ctx.poll_one();
|
||||
while (true) {
|
||||
if (r.wait_for(std::chrono::microseconds(0)) == std::future_status::ready) {
|
||||
return r.get();
|
||||
}
|
||||
if (io_ctx.poll_one() == 0) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(time_us));
|
||||
}
|
||||
}
|
||||
return r.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,10 +117,14 @@ public:
|
||||
*/
|
||||
template<typename T>
|
||||
T await(future<T>&& r, uint16_t time_us = 10) {
|
||||
while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) {
|
||||
io_ctx.poll_one();
|
||||
while (true) {
|
||||
if (r.wait_for(std::chrono::microseconds(0)) == std::future_status::ready) {
|
||||
return std::move(r).get();
|
||||
}
|
||||
if (io_ctx.poll_one() == 0) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(time_us));
|
||||
}
|
||||
}
|
||||
return move(r).get();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -178,14 +182,7 @@ public:
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
/**
|
||||
* Nonblock time sleep function
|
||||
*/
|
||||
|
||||
void sleep(int _time);
|
||||
|
||||
SleepHandle sleep2(int _time);
|
||||
|
||||
Sleep sleep(uint64_t time);
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
@ -200,6 +197,26 @@ public:
|
||||
};
|
||||
|
||||
|
||||
class Sleep {
|
||||
Asynco &runtime;
|
||||
std::promise<void> promise;
|
||||
std::future<void> future;
|
||||
std::shared_ptr<Timer> timer;
|
||||
|
||||
public:
|
||||
|
||||
Sleep(uint64_t _time, Asynco &runtime);
|
||||
|
||||
|
||||
// #ifdef _ASYNCO_DEFAULT_
|
||||
// Sleep(uint64_t _time);
|
||||
// #endif
|
||||
|
||||
|
||||
void await();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -97,12 +97,7 @@ Timer delayed(function<void()> callback, uint64_t time);
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
/**
|
||||
* Nonblock time sleep function
|
||||
*/
|
||||
|
||||
void sleep(int _time);
|
||||
SleepHandle sleep2(int _time);
|
||||
Sleep sleep(uint64_t time);
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
|
||||
@ -37,23 +37,39 @@ Timer Asynco::periodic(function<void()> callback, uint64_t time) {
|
||||
return Timer(io_ctx, callback, time, TimerType::Periodic);
|
||||
}
|
||||
|
||||
void Asynco::sleep(int _time) {
|
||||
promise<void> _promise;
|
||||
Timer t = delayed( [&]() {
|
||||
_promise.set_value();
|
||||
}, _time);
|
||||
|
||||
return await(_promise.get_future());
|
||||
Sleep Asynco::sleep(uint64_t time) {
|
||||
return Sleep(time, *this);
|
||||
}
|
||||
|
||||
SleepHandle Asynco::sleep2(int _time) {
|
||||
auto _promise = std::make_shared<std::promise<void>>();
|
||||
// Sleep
|
||||
|
||||
auto _timer = std::make_shared<Timer>(io_ctx, [_promise]() {
|
||||
_promise->set_value();
|
||||
Sleep::Sleep(uint64_t _time, Asynco &runtime): runtime(runtime) {
|
||||
future = promise.get_future();
|
||||
|
||||
timer = std::make_shared<Timer>(runtime.io_ctx, [this](){
|
||||
this->promise.set_value();
|
||||
}, _time, TimerType::Delayed);
|
||||
}
|
||||
|
||||
return { _promise->get_future(), _timer };
|
||||
|
||||
// #ifdef _ASYNCO_DEFAULT_
|
||||
// Sleep::Sleep(uint64_t _time): runtime(asynco_default_runtime()) {
|
||||
// future = promise.get_future();
|
||||
|
||||
// timer = std::make_shared<Timer>(runtime.io_ctx, [this](){
|
||||
// this->promise.set_value();
|
||||
// }, _time, TimerType::Delayed);
|
||||
// }
|
||||
// #endif
|
||||
|
||||
|
||||
void Sleep::await() {
|
||||
#ifdef _ASYNCO_DEFAULT_
|
||||
await_(future);
|
||||
#else
|
||||
runtime.await(future);
|
||||
#endif
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@ -13,15 +13,10 @@ Timer periodic(function<void()> callback, uint64_t time) {
|
||||
return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic);
|
||||
}
|
||||
|
||||
void sleep(int _time) {
|
||||
return Asynco_Default_Runtime.sleep(_time);
|
||||
Sleep sleep(uint64_t time) {
|
||||
return Sleep(time, Asynco_Default_Runtime);
|
||||
}
|
||||
|
||||
SleepHandle sleep2(int _time) {
|
||||
return Asynco_Default_Runtime.sleep2(_time);
|
||||
}
|
||||
|
||||
|
||||
Asynco& asynco_default_runtime() {
|
||||
return Asynco_Default_Runtime;
|
||||
}
|
||||
|
||||
@ -9,6 +9,16 @@ int main() {
|
||||
Asynco asynco;
|
||||
asynco.run(4);
|
||||
|
||||
asynco.async ([&](){
|
||||
loop {
|
||||
cout << "Loop 1" << endl;
|
||||
// auto timer = Sleep3(1000, asynco);
|
||||
// timer.await();
|
||||
// Sleep(1000, asynco).await();
|
||||
asynco.sleep(1000).await();
|
||||
}
|
||||
});
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 1" << endl;
|
||||
|
||||
@ -4,106 +4,105 @@ using namespace marcelb::asynco;
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main() {
|
||||
asynco_default_run();
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(1000).await();
|
||||
cout << "Loop 1" << endl;
|
||||
await_(sleep2(1000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(2000).await();
|
||||
cout << "Loop 2" << endl;
|
||||
await_(sleep2(2000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(2500).await();
|
||||
cout << "Loop 25" << endl;
|
||||
await_(sleep2(2500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(3000).await();
|
||||
cout << "Loop 3" << endl;
|
||||
await_(sleep2(3000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(3500).await();
|
||||
cout << "Loop 35" << endl;
|
||||
await_(sleep2(3500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(4000).await();
|
||||
cout << "Loop 4" << endl;
|
||||
await_(sleep2(4000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(4500).await();
|
||||
cout << "Loop 45" << endl;
|
||||
await_(sleep2(4500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(5000).await();
|
||||
cout << "Loop 5" << endl;
|
||||
await_(sleep2(5000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(5500).await();
|
||||
cout << "Loop 55" << endl;
|
||||
await_(sleep2(5500).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(6000).await();
|
||||
cout << "Loop 6" << endl;
|
||||
await_(sleep2(6000).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(6500).await();
|
||||
cout << "Loop 65" << endl;
|
||||
await_(sleep2(6500).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(7000).await();
|
||||
cout << "Loop 7" << endl;
|
||||
await_(sleep2(7000).future);
|
||||
}
|
||||
});
|
||||
|
||||
loop { // blokira trenutnu
|
||||
marcelb::asynco::sleep(1500).await();
|
||||
cout << "Loop 15" << endl;
|
||||
sleep2(1500).future.get();
|
||||
}
|
||||
|
||||
asynco_default_join();
|
||||
|
||||
@ -42,76 +42,76 @@ int main() {
|
||||
* initialization of typed events
|
||||
*/
|
||||
|
||||
Trigger<int, int> ev2int = trigger<int, int>();
|
||||
Trigger<int, string> evintString = trigger<int, string>();
|
||||
Trigger<> evoid = trigger<>();
|
||||
// Trigger<int, int> ev2int = trigger<int, int>();
|
||||
// Trigger<int, string> evintString = trigger<int, string>();
|
||||
// Trigger<> evoid = trigger<>();
|
||||
|
||||
ev2int.on("sum", [](int a, int b) {
|
||||
cout << "Sum " << a+b << endl;
|
||||
});
|
||||
// ev2int.on("sum", [](int a, int b) {
|
||||
// cout << "Sum " << a+b << endl;
|
||||
// });
|
||||
|
||||
evintString.on("substract", [](int a, string b) {
|
||||
cout << "Substract " << a-stoi(b) << endl;
|
||||
});
|
||||
// evintString.on("substract", [](int a, string b) {
|
||||
// cout << "Substract " << a-stoi(b) << endl;
|
||||
// });
|
||||
|
||||
evoid.on("void", []() {
|
||||
cout << "Void emited" << endl;
|
||||
});
|
||||
// evoid.on("void", []() {
|
||||
// cout << "Void emited" << endl;
|
||||
// });
|
||||
|
||||
// multiple listeners
|
||||
// // multiple listeners
|
||||
|
||||
string emited2 = "2";
|
||||
// string emited2 = "2";
|
||||
|
||||
evoid.on("void", [&]() {
|
||||
cout << "Void emited " << emited2 << endl;
|
||||
});
|
||||
// evoid.on("void", [&]() {
|
||||
// cout << "Void emited " << emited2 << endl;
|
||||
// });
|
||||
|
||||
sleep(1);
|
||||
// sleep(1);
|
||||
|
||||
/**
|
||||
* Emit
|
||||
*/
|
||||
// /**
|
||||
// * Emit
|
||||
// */
|
||||
|
||||
ev2int.tick("sum", 5, 8);
|
||||
// ev2int.tick("sum", 5, 8);
|
||||
|
||||
sleep(1);
|
||||
evintString.tick("substract", 3, to_string(2));
|
||||
// sleep(1);
|
||||
// evintString.tick("substract", 3, to_string(2));
|
||||
|
||||
sleep(1);
|
||||
evoid.tick("void");
|
||||
// sleep(1);
|
||||
// evoid.tick("void");
|
||||
|
||||
// Turn off the event listener
|
||||
|
||||
evoid.off("void");
|
||||
evoid.tick("void"); // nothing is happening
|
||||
// evoid.off("void");
|
||||
// evoid.tick("void"); // nothing is happening
|
||||
|
||||
class myOwnClass : public Trigger<int> {
|
||||
public:
|
||||
myOwnClass() : Trigger(asynco_default_runtime()) {};
|
||||
};
|
||||
// class myOwnClass : public Trigger<int> {
|
||||
// public:
|
||||
// myOwnClass() : Trigger(asynco_default_runtime()) {};
|
||||
// };
|
||||
|
||||
myOwnClass myclass;
|
||||
// myOwnClass myclass;
|
||||
|
||||
Timer t = delayed( [&] {
|
||||
myclass.tick("constructed", 1);
|
||||
}, 200);
|
||||
// Timer t = delayed( [&] {
|
||||
// myclass.tick("constructed", 1);
|
||||
// }, 200);
|
||||
|
||||
myclass.on("constructed", [] (int i) {
|
||||
cout << "Constructed " << i << endl;
|
||||
});
|
||||
// myclass.on("constructed", [] (int i) {
|
||||
// cout << "Constructed " << i << endl;
|
||||
// });
|
||||
|
||||
ClassWithTriggers mt;
|
||||
// ClassWithTriggers mt;
|
||||
|
||||
mt.on<int>("int", function<void(int)>([&](int i) {
|
||||
cout << "Emit int " << i << endl;
|
||||
}));
|
||||
// mt.on<int>("int", function<void(int)>([&](int i) {
|
||||
// cout << "Emit int " << i << endl;
|
||||
// }));
|
||||
|
||||
mt.on<string>("string", function<void(string)>([&](string s) {
|
||||
cout << "Emit string " << s << endl;
|
||||
}));
|
||||
// mt.on<string>("string", function<void(string)>([&](string s) {
|
||||
// cout << "Emit string " << s << endl;
|
||||
// }));
|
||||
|
||||
mt.tick("int", 5);
|
||||
mt.tick("string", string("Hello world"));
|
||||
// mt.tick("int", 5);
|
||||
// mt.tick("string", string("Hello world"));
|
||||
|
||||
asynco_default_join();
|
||||
return 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user