Skip to content

Commit

Permalink
Clean up of the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaudov, Stanislav Tomov committed Jul 29, 2020
1 parent 8151f14 commit 67fcf0b
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/alisp/include/alisp/alisp/alisp_common.hpp
Expand Up @@ -159,7 +159,7 @@ class ALObject : public std::conditional_t<USING_SHARED, std::enable_shared_from
{
set_temp_flag();
}

ALObjectType type() const { return m_type; }
bool is_int() const { return m_type == ALObjectType::INT_VALUE; }
bool is_string() const { return m_type == ALObjectType::STRING_VALUE; }
Expand Down
77 changes: 0 additions & 77 deletions src/alisp/include/alisp/alisp/async/action.hpp

This file was deleted.

11 changes: 6 additions & 5 deletions src/alisp/include/alisp/alisp/async/asyncs.hpp
Expand Up @@ -74,7 +74,7 @@ class AsyncS
public:
static constexpr size_t POOL_SIZE = 3;
using callback_type = detail::CallbackObject;
using event_type = detail::EventObject;
using work_type = detail::WorkObject;
using action_type = detail::ActionObject;

static constexpr std::uint32_t RUNNING_FLAG = 0x0001;
Expand All @@ -86,8 +86,7 @@ class AsyncS
private:
eval::Evaluator *m_eval;

std::queue<event_type> m_event_queue;
mutable std::mutex event_queue_mutex;
std::queue<work_type> m_work_queue;

std::queue<callback_type> m_callback_queue;
mutable std::mutex callback_queue_mutex;
Expand All @@ -110,7 +109,7 @@ class AsyncS
mutable std::condition_variable event_loop_cv;
void event_loop();

void execute_event(event_type call);
void execute_work(work_type call);

void execute_callback(callback_type call);

Expand All @@ -128,7 +127,9 @@ class AsyncS
void spin_loop();


void submit_event(event_type t_event);
void submit_work(work_type t_event);

void submit_action(action_type t_event);

void submit_callback(ALObjectPtr function, ALObjectPtr args = nullptr, al_callback internal = {});

Expand Down
4 changes: 2 additions & 2 deletions src/alisp/include/alisp/alisp/async/dispatch.hpp
Expand Up @@ -40,12 +40,12 @@ template<typename T, typename... Args> auto dispatch(AsyncS &async, Args &&... a
if constexpr (T::has_future)
{
auto fut = event_object.future(&async);
async.submit_event(detail::EventObject{ std::move(event_object) });
async.submit_work(AsyncS::work_type{ std::move(event_object) });
return fut;
}
else
{
async.submit_event(detail::EventObject{ std::move(event_object) });
async.submit_work(AsyncS::work_type{ std::move(event_object) });
return Qt;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/alisp/include/alisp/alisp/async/event.hpp
Expand Up @@ -51,13 +51,13 @@ template<class T> struct WrappingCallback : AbstractCallback
ALObjectPtr call(AsyncS *async) const override { return cb_(async); }
};

struct EventObject
struct WorkObject
{
std::unique_ptr<AbstractCallback> ptr_;

Future *future;

template<class T> EventObject(T t) { ptr_ = std::make_unique<WrappingCallback<T>>(std::move(t)); }
template<class T> WorkObject(T t) { ptr_ = std::make_unique<WrappingCallback<T>>(std::move(t)); }

ALObjectPtr operator()(AsyncS *async) const { return ptr_->call(async); }
};
Expand Down
51 changes: 0 additions & 51 deletions src/alisp/include/alisp/alisp/async/timing.hpp

This file was deleted.

26 changes: 19 additions & 7 deletions src/alisp/src/async/asyncs.cpp
Expand Up @@ -62,7 +62,7 @@ void AsyncS::check_exit_condition()
return;
}

if (!m_event_queue.empty())
if (!m_work_queue.empty())
{
return;
}
Expand Down Expand Up @@ -172,11 +172,11 @@ void AsyncS::event_loop()
return;
}

while (!m_event_queue.empty())
while (!m_work_queue.empty())
{
execute_event(std::move(m_event_queue.front()));
execute_work(std::move(m_work_queue.front()));
++m_asyncs;
m_event_queue.pop();
m_work_queue.pop();
}

handle_actions();
Expand All @@ -199,7 +199,7 @@ void AsyncS::event_loop()
}
}

void AsyncS::execute_event(event_type call)
void AsyncS::execute_work(work_type call)
{

std::thread tr([&, call = std::move(call)] {
Expand Down Expand Up @@ -236,12 +236,24 @@ void AsyncS::spin_loop()
event_loop_cv.notify_one();
}

void AsyncS::submit_event(event_type t_callback)
void AsyncS::submit_work(work_type t_callback)
{

{
std::lock_guard<std::mutex> guard{ event_loop_mutex };
m_event_queue.push(std::move(t_callback));
m_work_queue.push(std::move(t_callback));
m_eval->set_async_flag();
}

init();
spin_loop();
}

void AsyncS::submit_action(action_type t_event)
{
{
std::lock_guard<std::mutex> guard{ event_loop_mutex };
m_actions_queue.push(std::move(t_event));
m_eval->set_async_flag();
}

Expand Down
4 changes: 1 addition & 3 deletions src/alisp/src/definitions/alisp_language.cpp
Expand Up @@ -34,8 +34,6 @@
#include "alisp/utility/macros.hpp"
#include "alisp/utility/hash.hpp"

#include "alisp/alisp/async/timing.hpp"

namespace alisp
{

Expand Down Expand Up @@ -735,7 +733,7 @@ ALObjectPtr Flet(const ALObjectPtr &obj, env::Environment *env, eval::Evaluator
}
}


for (auto [ob, cell] : cells)
{
env->put(ob, cell);
Expand Down
1 change: 0 additions & 1 deletion src/alisp/src/modules/async.cpp
Expand Up @@ -20,7 +20,6 @@
#include "alisp/alisp/alisp_asyncs.hpp"
#include "alisp/alisp/alisp_eval.hpp"

#include "alisp/alisp/async/timing.hpp"
#include "alisp/alisp/async/action.hpp"

namespace alisp
Expand Down
1 change: 0 additions & 1 deletion src/modules/src/async_fileio.cpp
Expand Up @@ -20,7 +20,6 @@
#include "alisp/alisp/alisp_asyncs.hpp"
#include "alisp/alisp/alisp_eval.hpp"

#include "alisp/alisp/async/timing.hpp"
#include "alisp/alisp/async/action.hpp"
#include "alisp/config.hpp"
#include "alisp/utility.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/modules/src/http/route_functions.cpp
Expand Up @@ -27,7 +27,6 @@
#include "http/definitions.hpp"
#include "http/response_handling.hpp"
#include "http/request_handling.hpp"
#include "http/async_actions.hpp"
#include "http/language_space.hpp"


Expand Down
1 change: 0 additions & 1 deletion src/modules/src/http/server_funcitons.cpp
Expand Up @@ -26,7 +26,6 @@
#include "alisp/alisp/alisp_eval.hpp"

#include "http/definitions.hpp"
#include "http/async_actions.hpp"
#include "http/language_space.hpp"
#include "http/response_handling.hpp"
#include "http/request_handling.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/modules/src/http_restbed.cpp
Expand Up @@ -26,7 +26,6 @@
#include "alisp/alisp/alisp_eval.hpp"

#include "http/definitions.hpp"
#include "http/async_actions.hpp"
#include "http/language_space.hpp"


Expand Down

0 comments on commit 67fcf0b

Please sign in to comment.