Compare commits

..

No commits in common. '5b0e363cd56f6253fafdee78c0582eddd12912f1' and '1ccac9dbf805bf92b2245ef039d1c28edc6cdc0f' have entirely different histories.

  1. 3
      .gitignore
  2. 4
      .vscode/settings.json
  3. 22
      README.md
  4. 69
      lib/asynco.hpp
  5. 71
      lib/engine.hpp
  6. 14
      lib/timers.hpp
  7. 6
      lib/trigger.hpp
  8. 7
      src/engine.cpp
  9. 36
      src/timers.cpp
  10. 1
      test/compile.sh
  11. 67
      test/test.cpp

3
.gitignore vendored

@ -1,3 +1,2 @@
test/test test/test
test/*.txt test/*.txt
example

@ -69,8 +69,6 @@
"cinttypes": "cpp", "cinttypes": "cpp",
"typeindex": "cpp", "typeindex": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp", "variant": "cpp"
"coroutine": "cpp",
"source_location": "cpp"
} }
} }

@ -77,28 +77,6 @@ int t = time1.expired();
// is it stopped // is it stopped
bool stoped = time1.stoped(); bool stoped = time1.stoped();
// If you don't want to save in a variable, but you want to start a timer, use these functions
// And you can also save them, they are only of the shared pointer type
auto d = Delayed( [](){
cout << "Delayed" << endl;
}, 2000);
auto p = Periodic( [](){
cout << "Periodic" << endl;
}, 700);
Periodic( [&] (){
cout << "Delayed expire " << d->expired() << endl;
cout << "Periodic ticks " << p->ticks() << endl;
cout << "Delayed stoped " << d->stoped() << endl;
cout << "Periodic stoped " << p->stoped() << endl;
}, 1000);
Delayed( [&](){
p->stop();
}, 10000);
``` ```
Make functions asynchronous Make functions asynchronous

@ -1,7 +1,7 @@
#ifndef _ASYNCO_ #ifndef _ASYNCO_
#define _ASYNCO_ #define _ASYNCO_
#include "engine.hpp" #include <boost/asio.hpp>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
@ -9,6 +9,51 @@ using namespace std;
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4
/**
* Internal anonymous class for initializing the ASIO context and thread pool
* !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
*/
class {
public:
boost::asio::io_context io_context;
void run() {
for (auto& runner : runners) {
runner.join();
}
}
private:
unique_ptr<boost::asio::io_service::work> work { [&] () {
return new boost::asio::io_service::work(io_context);
} ()};
vector<thread> runners { [&] () {
vector<thread> _runs;
unsigned int num_of_runners;
#ifdef NUM_OF_RUNNERS
num_of_runners = NUM_OF_RUNNERS;
#else
num_of_runners = thread::hardware_concurrency();
if (num_of_runners < HW_CONCURRENCY_MINIMAL) {
num_of_runners = HW_CONCURRENCY_MINIMAL;
}
#endif
for (int i=0; i<num_of_runners; i++) {
_runs.push_back(thread ( [this] () {
io_context.run();
}));
}
return _runs;
} ()};
} _asynco_engine;
/** /**
* Run the function asynchronously * Run the function asynchronously
*/ */
@ -35,28 +80,6 @@ T await_(future<T>&& r) {
return move(r).get(); return move(r).get();
} }
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return r.get();
}
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>&& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return move(r).get();
}
} }
} }

@ -1,71 +0,0 @@
#ifndef _ASYNCO_ENGINE_
#define _ASYNCO_ENGINE_
#include <vector>
#include <memory>
using namespace std;
#include <boost/asio.hpp>
namespace marcelb {
namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4
/**
* Internal anonymous class for initializing the ASIO context and thread pool
* !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
*/
class Engine {
public:
boost::asio::io_context io_context;
void run() {
for (auto& runner : runners) {
runner.join();
}
}
private:
unique_ptr<boost::asio::io_service::work> work { [&] () {
return new boost::asio::io_service::work(io_context);
} ()};
vector<thread> runners { [&] () {
vector<thread> _runs;
unsigned int num_of_runners;
#ifdef NUM_OF_RUNNERS
num_of_runners = NUM_OF_RUNNERS;
#else
num_of_runners = thread::hardware_concurrency();
if (num_of_runners < HW_CONCURRENCY_MINIMAL) {
num_of_runners = HW_CONCURRENCY_MINIMAL;
}
#endif
for (int i=0; i<num_of_runners; i++) {
_runs.push_back(thread ( [this] () {
io_context.run();
}));
}
return _runs;
} ()};
};
extern Engine _asynco_engine;
}
}
#endif

@ -1,10 +1,12 @@
#ifndef _ASYNCO_TIMERS_ #ifndef _TIMERS_
#define _ASYNCO_TIMERS_ #define _TIMERS_
#include "asynco.hpp"
#include <chrono> #include <chrono>
using namespace std;
#include "asynco.hpp" using namespace std;
using namespace marcelb;
using namespace asynco;
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
@ -151,10 +153,6 @@ class delayed {
}; };
shared_ptr<periodic> Periodic(function<void()> callback, uint64_t time);
shared_ptr<delayed> Delayed(function<void()> callback, uint64_t time);
} }
} }

@ -1,5 +1,5 @@
#ifndef _ASYNCO_TRIGGER_ #ifndef _TRIGGER_
#define _ASYNCO_TRIGGER_ #define _TRIGGER_
#include <map> #include <map>
#include <vector> #include <vector>
@ -8,7 +8,7 @@
using namespace std; using namespace std;
#include "engine.hpp" #include "asynco.hpp"
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
namespace triggers { namespace triggers {

@ -1,7 +0,0 @@
#include "../lib/engine.hpp"
namespace marcelb::asynco {
Engine _asynco_engine;
};

@ -106,40 +106,4 @@ delayed::~delayed() {
stop(); stop();
} }
mutex p_io, d_io;
vector<shared_ptr<periodic>> periodic_calls_container;
vector<shared_ptr<delayed>> delayed_calls_container;
shared_ptr<periodic> Periodic(function<void()> callback, uint64_t time) {
shared_ptr<periodic> periodic_ptr(make_shared<periodic>(callback, time));
async_ ( [&, periodic_ptr](){
lock_guard<mutex> lock(p_io);
periodic_calls_container.push_back(periodic_ptr);
for (uint32_t i=0; i<periodic_calls_container.size(); i++) {
if (periodic_calls_container[i]->stoped()) {
periodic_calls_container.erase(periodic_calls_container.begin()+i);
i--;
}
}
});
return periodic_ptr;
}
shared_ptr<delayed> Delayed(function<void()> callback, uint64_t time) {
shared_ptr<delayed> delayed_ptr(make_shared<delayed>(callback, time));
async_ ( [&, delayed_ptr](){
lock_guard<mutex> lock(p_io);
delayed_calls_container.push_back(delayed_ptr);
for (uint32_t i=0; i<delayed_calls_container.size(); i++) {
if (delayed_calls_container[i]->stoped() || delayed_calls_container[i]->expired()) {
delayed_calls_container.erase(delayed_calls_container.begin()+i);
i--;
}
}
});
return delayed_ptr;
}
}; };

@ -1 +0,0 @@
g++ test.cpp ../src/* -o test

@ -69,9 +69,9 @@ int main () {
// --------------- TIME ASYNCHRONOUS FUNCTIONS -------------- // --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
/** // /**
* Init periodic and delayed; clear periodic and delayed // * Init periodic and delayed; clear periodic and delayed
*/ // */
// periodic inter1 ([&]() { // periodic inter1 ([&]() {
// cout << "periodic prvi " << rtime_ms() - start << endl; // cout << "periodic prvi " << rtime_ms() - start << endl;
@ -129,30 +129,11 @@ int main () {
// cout << "nije isteko " << endl; // cout << "nije isteko " << endl;
// } // }
// auto d = Delayed( [](){ // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// cout << "Delayed" << endl;
// }, 2000);
// auto p = Periodic( [](){
// cout << "Periodic" << endl;
// }, 700);
// Periodic( [&] (){ // /**
// cout << "Delayed expire " << d->expired() << endl; // * Run an function asyncronic
// cout << "Periodic ticks " << p->ticks() << endl; // */
// cout << "Delayed stoped " << d->stoped() << endl;
// cout << "Periodic stoped " << p->stoped() << endl;
// }, 1000);
// Delayed( [&](){
// p->stop();
// }, 10000);
// // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// // /**
// // * Run an function asyncronic
// // */
// async_ ( []() { // async_ ( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
@ -174,13 +155,13 @@ int main () {
// ); // );
// // async(launch::async, [] () { // async(launch::async, [] () {
// // cout << "Another thread in async style!" << endl; // cout << "Another thread in async style!" << endl;
// // }); // });
// // /** // /**
// // * Call class method // * Call class method
// // */ // */
// clm classes; // clm classes;
// async_ ( [&classes] () { // async_ ( [&classes] () {
@ -189,17 +170,17 @@ int main () {
// sleep(5); // sleep(5);
// // /** // /**
// // * await_ after runned as async // * await_ after runned as async
// // */ // */
// auto aa = async_ ( []() { // auto a = async_ ( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
// cout << "async_ 2" << endl; // cout << "async_ 2" << endl;
// return 5; // return 5;
// }); // });
// cout << await_(aa) << endl; // cout << await_(a) << endl;
// cout << "print after async_ 2" << endl; // cout << "print after async_ 2" << endl;
// /** // /**
@ -290,19 +271,17 @@ int main () {
// ); // );
// } // }
// auto await_all2 = [&] () { // auto await_all = [&] () {
// for (int i=0; i<fut_vec.size(); i++) { // for (int i=0; i<fut_vec.size(); i++) {
// await_ (fut_vec[i]); // await_ (fut_vec[i]);
// } // }
// }; // };
// await_all2(); // --------------- EVENTS -------------------
// // --------------- EVENTS ------------------- /**
* initialization of typed events
// /** */
// * initialization of typed events
// */
// trigger<int, int> ev2int; // trigger<int, int> ev2int;
// trigger<int, string> evintString; // trigger<int, string> evintString;

Loading…
Cancel
Save