Skip to content

Commit 8e343da

Browse files
authored
Fix locking issues (#129)
1 parent d961252 commit 8e343da

File tree

12 files changed

+224
-118
lines changed

12 files changed

+224
-118
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
# clangd
1010
/.cache/
11+
12+
# Visual Studio files
13+
/.vs

ecsact/runtime/async.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,18 @@ ECSACT_ASYNC_API_FN(void, ecsact_async_disconnect)(void);
208208
*/
209209
ECSACT_ASYNC_API_FN(ecsact_async_request_id, ecsact_async_create_entity)(void);
210210

211+
/**
212+
* Gets the current tick
213+
*/
214+
ECSACT_ASYNC_API_FN(int32_t, ecsact_async_get_current_tick)(void);
215+
211216
#define FOR_EACH_ECSACT_ASYNC_API_FN(fn, ...) \
212217
fn(ecsact_async_enqueue_execution_options, __VA_ARGS__); \
213218
fn(ecsact_async_flush_events, __VA_ARGS__); \
214219
fn(ecsact_async_connect, __VA_ARGS__); \
215220
fn(ecsact_async_disconnect, __VA_ARGS__); \
216-
fn(ecsact_async_create_entity, __VA_ARGS__);
221+
fn(ecsact_async_create_entity, __VA_ARGS__); \
222+
fn(ecsact_async_get_current_tick, __VA_ARGS__);
217223

218224
#undef ECSACT_ASYNC_API
219225
#undef ECSACT_ASYNC_API_FN

reference/async_reference/async.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ ecsact_async_request_id ecsact_async_connect(const char* connection_string) {
1313
}
1414

1515
void ecsact_async_disconnect() {
16-
async::reference->disconnect();
17-
async::reference.reset();
16+
if(async::reference) {
17+
async::reference->disconnect();
18+
async::reference.reset();
19+
}
1820
}
1921

2022
void ecsact_async_flush_events(
@@ -33,3 +35,7 @@ ecsact_async_request_id ecsact_async_enqueue_execution_options(
3335
) {
3436
return async::reference->enqueue_execution_options(options);
3537
}
38+
39+
int32_t ecsact_async_get_current_tick() {
40+
return async::reference->get_current_tick();
41+
}

reference/async_reference/async_reference.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ ecsact_async_request_id async_reference::create_entity_request() {
217217
return req_id;
218218
}
219219

220+
int32_t async_reference::get_current_tick() {
221+
return tick_manager.get_current_tick();
222+
}
223+
220224
void async_reference::disconnect() {
221225
is_connected = false;
222226
if(execution_thread.joinable()) {

reference/async_reference/async_reference.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <atomic>
99
#include <optional>
1010
#include <variant>
11+
#include <condition_variable>
1112
#include "ecsact/runtime/core.hh"
1213
#include "ecsact/runtime/async.h"
1314

@@ -33,6 +34,9 @@ public:
3334
);
3435

3536
ecsact_async_request_id create_entity_request();
37+
38+
int32_t get_current_tick();
39+
3640
ecsact_async_request_id connect(const char* connection_string);
3741

3842
void disconnect();

reference/async_reference/callbacks/async_callbacks.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ void async_callbacks::add(const types::async_requests type) {
77

88
void async_callbacks::invoke(const ecsact_async_events_collector* async_events
99
) {
10+
if(requests.empty()) {
11+
return;
12+
}
13+
1014
if(async_events == nullptr) {
1115
std::unique_lock lk(async_m);
1216
requests.clear();

reference/async_reference/callbacks/execution_callbacks.cc

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ void execution_callbacks::invoke(
1717
const ecsact_execution_events_collector* execution_events,
1818
ecsact_registry_id registry_id
1919
) {
20+
if(!has_callbacks()) {
21+
return;
22+
}
23+
2024
if(execution_events == nullptr) {
21-
std::unique_lock lk(execution_m);
22-
init_callbacks_info.clear();
23-
update_callbacks_info.clear();
24-
remove_callbacks_info.clear();
25+
if(has_callbacks()) {
26+
std::unique_lock lk(execution_m);
27+
init_callbacks_info.clear();
28+
update_callbacks_info.clear();
29+
remove_callbacks_info.clear();
30+
}
2531
return;
2632
}
2733

@@ -105,6 +111,22 @@ void execution_callbacks::invoke(
105111
}
106112
}
107113

114+
bool execution_callbacks::has_callbacks() {
115+
if(init_callbacks_info.size() > 0) {
116+
return true;
117+
}
118+
119+
if(update_callbacks_info.size() > 0) {
120+
return true;
121+
}
122+
123+
if(remove_callbacks_info.size() > 0) {
124+
return true;
125+
}
126+
127+
return false;
128+
}
129+
108130
void execution_callbacks::init_callback(
109131
ecsact_event event,
110132
ecsact_entity_id entity_id,

reference/async_reference/callbacks/execution_callbacks.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ private:
3333
std::vector<types::callback_info> update_callbacks_info;
3434
std::vector<types::callback_info> remove_callbacks_info;
3535

36+
bool has_callbacks();
37+
3638
static void init_callback(
3739
ecsact_event event,
3840
ecsact_entity_id entity_id,

0 commit comments

Comments
 (0)