Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e3574fac17 | |||
| c8f6aa877c | |||
| 4283f32826 |
@ -27,6 +27,7 @@ add_subdirectory(test)
|
||||
|
||||
add_compile_options(-w)
|
||||
|
||||
# add_definitions(-DASYNCO_THREADS_POOL_SIZE=20)
|
||||
|
||||
|
||||
# Instaliraj biblioteku
|
||||
|
||||
@ -18,12 +18,16 @@ using namespace std;
|
||||
#endif
|
||||
using namespace boost::asio;
|
||||
|
||||
#define loop while(true)
|
||||
|
||||
#include "timers.hpp"
|
||||
#include "trigger.hpp"
|
||||
|
||||
namespace marcelb {
|
||||
namespace asynco {
|
||||
|
||||
class Sleep;
|
||||
|
||||
/**
|
||||
* Asynco runtime
|
||||
* Used for all asynchronous capabilities of this wrapper
|
||||
@ -98,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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -170,6 +182,8 @@ public:
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
Sleep sleep(uint64_t time);
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
*/
|
||||
@ -183,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,6 +97,8 @@ Timer delayed(function<void()> callback, uint64_t time);
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
Sleep sleep(uint64_t time);
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
*/
|
||||
|
||||
@ -38,4 +38,38 @@ Timer Asynco::periodic(function<void()> callback, uint64_t time) {
|
||||
}
|
||||
|
||||
|
||||
Sleep Asynco::sleep(uint64_t time) {
|
||||
return Sleep(time, *this);
|
||||
}
|
||||
|
||||
// Sleep
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// #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,12 +13,20 @@ Timer periodic(function<void()> callback, uint64_t time) {
|
||||
return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic);
|
||||
}
|
||||
|
||||
Sleep sleep(uint64_t time) {
|
||||
return Sleep(time, Asynco_Default_Runtime);
|
||||
}
|
||||
|
||||
Asynco& asynco_default_runtime() {
|
||||
return Asynco_Default_Runtime;
|
||||
}
|
||||
|
||||
void asynco_default_run() {
|
||||
#ifdef ASYNCO_THREADS_POOL_SIZE
|
||||
Asynco_Default_Runtime.run(ASYNCO_THREADS_POOL_SIZE);
|
||||
#else
|
||||
Asynco_Default_Runtime.run();
|
||||
#endif
|
||||
}
|
||||
|
||||
void asynco_default_run_on_this() {
|
||||
|
||||
@ -26,4 +26,10 @@ add_executable(asynco_coroutine_default main_coroutine_default.cpp)
|
||||
target_link_libraries(asynco_coroutine_default asynco Boost::system)
|
||||
|
||||
add_executable(asynco_coroutine main_coroutine.cpp)
|
||||
target_link_libraries(asynco_coroutine asynco Boost::system)
|
||||
target_link_libraries(asynco_coroutine asynco Boost::system)
|
||||
|
||||
add_executable(asynco_infinit_loop_default main_infinit_loop_default.cpp)
|
||||
target_link_libraries(asynco_infinit_loop_default asynco Boost::system)
|
||||
|
||||
add_executable(asynco_infinit_loop main_infinit_loop.cpp)
|
||||
target_link_libraries(asynco_infinit_loop asynco Boost::system)
|
||||
@ -11,8 +11,15 @@ void notLambdaFunction() {
|
||||
|
||||
class clm {
|
||||
public:
|
||||
int i = 7;
|
||||
void classMethode() {
|
||||
cout << "Call class method" << endl;
|
||||
cout << "Call class method" << i << endl;
|
||||
}
|
||||
|
||||
future<void> asyncMethode() {
|
||||
return async_([&]() {
|
||||
cout << "Async class method" << i << endl;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -53,6 +60,8 @@ int main() {
|
||||
classes.classMethode();
|
||||
});
|
||||
|
||||
await_(classes.asyncMethode());
|
||||
|
||||
//------------------AWAIT----------------------
|
||||
|
||||
auto a = async_ ( []() {
|
||||
|
||||
121
test/main_infinit_loop.cpp
Normal file
121
test/main_infinit_loop.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
#include "../lib/asynco.hpp"
|
||||
using namespace marcelb::asynco;
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
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;
|
||||
// asynco.sleep(1000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 2" << endl;
|
||||
// asynco.sleep(2000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 25" << endl;
|
||||
// asynco.sleep(2500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 3" << endl;
|
||||
// asynco.sleep(3000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 35" << endl;
|
||||
// asynco.sleep(3500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 4" << endl;
|
||||
// asynco.sleep(4000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 45" << endl;
|
||||
// asynco.sleep(4500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 5" << endl;
|
||||
// asynco.sleep(5000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 55" << endl;
|
||||
// asynco.sleep(5500);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 6" << endl;
|
||||
// asynco.sleep(6000);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 65" << endl;
|
||||
// asynco.sleep(6500);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 7" << endl;
|
||||
// asynco.sleep(7000);
|
||||
// }
|
||||
// });
|
||||
|
||||
// loop { // blokira trenutnu
|
||||
// cout << "Loop 15" << endl;
|
||||
// asynco.sleep(1500);
|
||||
// }
|
||||
|
||||
asynco.join();
|
||||
return 0;
|
||||
}
|
||||
110
test/main_infinit_loop_default.cpp
Normal file
110
test/main_infinit_loop_default.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
#include "../lib/asynco_default.hpp"
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(2000).await();
|
||||
cout << "Loop 2" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(2500).await();
|
||||
cout << "Loop 25" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(3000).await();
|
||||
cout << "Loop 3" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(3500).await();
|
||||
cout << "Loop 35" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(4000).await();
|
||||
cout << "Loop 4" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(4500).await();
|
||||
cout << "Loop 45" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(5000).await();
|
||||
cout << "Loop 5" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(5500).await();
|
||||
cout << "Loop 55" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(6000).await();
|
||||
cout << "Loop 6" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(6500).await();
|
||||
cout << "Loop 65" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
marcelb::asynco::sleep(7000).await();
|
||||
cout << "Loop 7" << endl;
|
||||
}
|
||||
});
|
||||
|
||||
loop { // blokira trenutnu
|
||||
marcelb::asynco::sleep(1500).await();
|
||||
cout << "Loop 15" << endl;
|
||||
}
|
||||
|
||||
asynco_default_join();
|
||||
return 0;
|
||||
}
|
||||
@ -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