Skip to content

Fix locking issues #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

# clangd
/.cache/

# Visual Studio files
/.vs
8 changes: 7 additions & 1 deletion ecsact/runtime/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,18 @@ ECSACT_ASYNC_API_FN(void, ecsact_async_disconnect)(void);
*/
ECSACT_ASYNC_API_FN(ecsact_async_request_id, ecsact_async_create_entity)(void);

/**
* Gets the current tick
*/
ECSACT_ASYNC_API_FN(int32_t, ecsact_async_get_current_tick)(void);

#define FOR_EACH_ECSACT_ASYNC_API_FN(fn, ...) \
fn(ecsact_async_enqueue_execution_options, __VA_ARGS__); \
fn(ecsact_async_flush_events, __VA_ARGS__); \
fn(ecsact_async_connect, __VA_ARGS__); \
fn(ecsact_async_disconnect, __VA_ARGS__); \
fn(ecsact_async_create_entity, __VA_ARGS__);
fn(ecsact_async_create_entity, __VA_ARGS__); \
fn(ecsact_async_get_current_tick, __VA_ARGS__);

#undef ECSACT_ASYNC_API
#undef ECSACT_ASYNC_API_FN
Expand Down
10 changes: 8 additions & 2 deletions reference/async_reference/async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ ecsact_async_request_id ecsact_async_connect(const char* connection_string) {
}

void ecsact_async_disconnect() {
async::reference->disconnect();
async::reference.reset();
if(async::reference) {
async::reference->disconnect();
async::reference.reset();
}
}

void ecsact_async_flush_events(
Expand All @@ -33,3 +35,7 @@ ecsact_async_request_id ecsact_async_enqueue_execution_options(
) {
return async::reference->enqueue_execution_options(options);
}

int32_t ecsact_async_get_current_tick() {
return async::reference->get_current_tick();
}
4 changes: 4 additions & 0 deletions reference/async_reference/async_reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ ecsact_async_request_id async_reference::create_entity_request() {
return req_id;
}

int32_t async_reference::get_current_tick() {
return tick_manager.get_current_tick();
}

void async_reference::disconnect() {
is_connected = false;
if(execution_thread.joinable()) {
Expand Down
4 changes: 4 additions & 0 deletions reference/async_reference/async_reference.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <atomic>
#include <optional>
#include <variant>
#include <condition_variable>
#include "ecsact/runtime/core.hh"
#include "ecsact/runtime/async.h"

Expand All @@ -33,6 +34,9 @@ public:
);

ecsact_async_request_id create_entity_request();

int32_t get_current_tick();

ecsact_async_request_id connect(const char* connection_string);

void disconnect();
Expand Down
4 changes: 4 additions & 0 deletions reference/async_reference/callbacks/async_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ void async_callbacks::add(const types::async_requests type) {

void async_callbacks::invoke(const ecsact_async_events_collector* async_events
) {
if(requests.empty()) {
return;
}

if(async_events == nullptr) {
std::unique_lock lk(async_m);
requests.clear();
Expand Down
30 changes: 26 additions & 4 deletions reference/async_reference/callbacks/execution_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ void execution_callbacks::invoke(
const ecsact_execution_events_collector* execution_events,
ecsact_registry_id registry_id
) {
if(!has_callbacks()) {
return;
}

if(execution_events == nullptr) {
std::unique_lock lk(execution_m);
init_callbacks_info.clear();
update_callbacks_info.clear();
remove_callbacks_info.clear();
if(has_callbacks()) {
std::unique_lock lk(execution_m);
init_callbacks_info.clear();
update_callbacks_info.clear();
remove_callbacks_info.clear();
}
return;
}

Expand Down Expand Up @@ -105,6 +111,22 @@ void execution_callbacks::invoke(
}
}

bool execution_callbacks::has_callbacks() {
if(init_callbacks_info.size() > 0) {
return true;
}

if(update_callbacks_info.size() > 0) {
return true;
}

if(remove_callbacks_info.size() > 0) {
return true;
}

return false;
}

void execution_callbacks::init_callback(
ecsact_event event,
ecsact_entity_id entity_id,
Expand Down
2 changes: 2 additions & 0 deletions reference/async_reference/callbacks/execution_callbacks.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private:
std::vector<types::callback_info> update_callbacks_info;
std::vector<types::callback_info> remove_callbacks_info;

bool has_callbacks();

static void init_callback(
ecsact_event event,
ecsact_entity_id entity_id,
Expand Down
Loading