Skip to content

Commit 865684c

Browse files
committed
decoupling request IDs for pre-connect api calls
1 parent 8e343da commit 865684c

20 files changed

+153
-60
lines changed

reference/async_reference/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cc_library(
1818
deps = [
1919
"//:async",
2020
"//:core",
21+
"//reference/async_reference/request_id_factory",
2122
"//reference/async_reference/callbacks:async_callbacks",
2223
"//reference/async_reference/callbacks:execution_callbacks",
2324
"//reference/async_reference/detail/c_execution_options",

reference/async_reference/async.cc

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,70 @@
22

33
#include "ecsact/runtime/async.h"
44
#include "async_reference.hh"
5+
#include "callbacks/async_callbacks.hh"
6+
#include "request_id_factory/request_id_factory.hh"
57

6-
namespace async {
7-
std::optional<async_reference> reference;
8-
}
8+
using namespace ecsact::async_reference;
9+
10+
// NOTE: These are the singletons for managing the reference library state.
11+
// This file should be small. Declare a few variables and call a few
12+
// functions in the C function bodies. Keep the logic minimal.
13+
static auto async_callbacks = detail::async_callbacks{};
14+
static auto request_id_factory = detail::request_id_factory{};
15+
static auto reference = std::optional<detail::async_reference>{};
916

1017
ecsact_async_request_id ecsact_async_connect(const char* connection_string) {
11-
async::reference.emplace();
12-
return async::reference->connect(connection_string);
18+
auto req_id = request_id_factory.next_id();
19+
reference.emplace(request_id_factory, async_callbacks);
20+
reference->connect(req_id, connection_string);
21+
return req_id;
1322
}
1423

1524
void ecsact_async_disconnect() {
16-
if(async::reference) {
17-
async::reference->disconnect();
18-
async::reference.reset();
25+
if(reference) {
26+
reference->disconnect();
27+
reference.reset();
1928
}
2029
}
2130

2231
void ecsact_async_flush_events(
23-
const ecsact_execution_events_collector* execution_events,
24-
const ecsact_async_events_collector* async_events
32+
const ecsact_execution_events_collector* execution_evc,
33+
const ecsact_async_events_collector* async_evc
2534
) {
26-
async::reference->flush_events(execution_events, async_events);
35+
async_callbacks.invoke(async_evc);
36+
if(reference) {
37+
reference->invoke_execution_events(execution_evc);
38+
}
2739
}
2840

2941
ecsact_async_request_id ecsact_async_create_entity() {
30-
return async::reference->create_entity_request();
42+
auto req_id = request_id_factory.next_id();
43+
if(!reference) {
44+
async_callbacks.add(detail::types::async_error{
45+
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
46+
.request_ids = {req_id},
47+
});
48+
return req_id;
49+
}
50+
51+
reference->create_entity_request(req_id);
52+
return req_id;
3153
}
3254

3355
ecsact_async_request_id ecsact_async_enqueue_execution_options(
3456
const ecsact_execution_options options
3557
) {
36-
return async::reference->enqueue_execution_options(options);
58+
auto req_id = request_id_factory.next_id();
59+
if(!reference) {
60+
async_callbacks.add(detail::types::async_error{
61+
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
62+
.request_ids = {req_id},
63+
});
64+
return req_id;
65+
}
66+
67+
reference->enqueue_execution_options(req_id, options);
68+
return req_id;
3769
}
3870

3971
int32_t ecsact_async_get_current_tick() {

reference/async_reference/async_reference.cc

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "async_reference.hh"
99

10+
using namespace ecsact::async_reference::detail;
11+
1012
struct parsed_connection_string {
1113
std::string host;
1214
std::map<std::string, std::string> options;
@@ -54,10 +56,10 @@ static auto parse_connection_string(std::string_view str)
5456
return result;
5557
}
5658

57-
ecsact_async_request_id async_reference::connect(const char* connection_string
59+
void async_reference::connect(
60+
ecsact_async_request_id req_id,
61+
const char* connection_string
5862
) {
59-
auto req_id = next_request_id();
60-
6163
std::string connect_str(connection_string);
6264

6365
auto result = parse_connection_string(connect_str);
@@ -74,7 +76,7 @@ ecsact_async_request_id async_reference::connect(const char* connection_string
7476
if(result.host != "good") {
7577
is_connected = false;
7678
is_connected_notified = false;
77-
return req_id;
79+
return;
7880
}
7981

8082
if(tick_rate.count() == 0) {
@@ -84,21 +86,18 @@ ecsact_async_request_id async_reference::connect(const char* connection_string
8486
};
8587

8688
async_callbacks.add(async_err);
87-
return req_id;
89+
return;
8890
}
8991

9092
registry_id = ecsact_create_registry("async_reference_impl_reg");
9193
is_connected = true;
9294
execute_systems();
93-
94-
return req_id;
9595
}
9696

97-
ecsact_async_request_id async_reference::enqueue_execution_options(
97+
void async_reference::enqueue_execution_options(
98+
ecsact_async_request_id req_id,
9899
const ecsact_execution_options& options
99100
) {
100-
auto req_id = next_request_id();
101-
102101
if(is_connected == false && is_connected_notified == false) {
103102
types::async_error async_err{
104103
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
@@ -107,7 +106,7 @@ ecsact_async_request_id async_reference::enqueue_execution_options(
107106

108107
is_connected_notified = true;
109108
async_callbacks.add(async_err);
110-
return req_id;
109+
return;
111110
}
112111

113112
auto cpp_options = util::c_to_cpp_execution_options(options);
@@ -118,7 +117,7 @@ ecsact_async_request_id async_reference::enqueue_execution_options(
118117
};
119118

120119
tick_manager.add_pending_options(pending_options);
121-
return req_id;
120+
return;
122121
}
123122

124123
void async_reference::execute_systems() {
@@ -189,18 +188,15 @@ void async_reference::execute_systems() {
189188
});
190189
}
191190

192-
void async_reference::flush_events(
193-
const ecsact_execution_events_collector* execution_events,
194-
const ecsact_async_events_collector* async_events
191+
void async_reference::invoke_execution_events(
192+
const ecsact_execution_events_collector* execution_evc
195193
) {
196-
async_callbacks.invoke(async_events);
197194
if(registry_id) {
198-
exec_callbacks.invoke(execution_events, *registry_id);
195+
exec_callbacks.invoke(execution_evc, *registry_id);
199196
}
200197
}
201198

202-
ecsact_async_request_id async_reference::create_entity_request() {
203-
auto req_id = next_request_id();
199+
void async_reference::create_entity_request(ecsact_async_request_id req_id) {
204200
if(is_connected == false && is_connected_notified == false) {
205201
types::async_error async_err{
206202
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
@@ -210,11 +206,10 @@ ecsact_async_request_id async_reference::create_entity_request() {
210206
async_callbacks.add(async_err);
211207
is_connected_notified = true;
212208

213-
return req_id;
209+
return;
214210
}
215211

216212
entity_manager.request_entity(req_id);
217-
return req_id;
218213
}
219214

220215
int32_t async_reference::get_current_tick() {
@@ -227,9 +222,3 @@ void async_reference::disconnect() {
227222
execution_thread.join();
228223
}
229224
}
230-
231-
ecsact_async_request_id async_reference::next_request_id() {
232-
return static_cast<ecsact_async_request_id>(
233-
_last_request_id.fetch_add(1, std::memory_order_relaxed)
234-
);
235-
}

reference/async_reference/async_reference.hh

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,37 @@
1919
#include "reference/async_reference/callbacks/execution_callbacks.hh"
2020
#include "reference/async_reference/callbacks/async_callbacks.hh"
2121
#include "reference/async_reference/entity_manager/entity_manager.hh"
22+
#include "reference/async_reference/request_id_factory/request_id_factory.hh"
23+
#include "request_id_factory/request_id_factory.hh"
2224

25+
namespace ecsact::async_reference::detail {
2326
class async_reference {
2427
public:
25-
ecsact_async_request_id enqueue_execution_options(
28+
inline async_reference(
29+
request_id_factory& request_id_factory,
30+
async_callbacks& async_callbacks
31+
)
32+
: request_id_factory(request_id_factory), async_callbacks(async_callbacks) {
33+
}
34+
35+
inline ~async_reference() {
36+
}
37+
38+
void enqueue_execution_options(
39+
ecsact_async_request_id req_id,
2640
const ecsact_execution_options& options
2741
);
2842

2943
void execute_systems();
3044

31-
void flush_events(
32-
const ecsact_execution_events_collector* execution_events,
33-
const ecsact_async_events_collector* async_events
45+
void invoke_execution_events(
46+
const ecsact_execution_events_collector* execution_evc
3447
);
3548

36-
ecsact_async_request_id create_entity_request();
37-
3849
int32_t get_current_tick();
3950

40-
ecsact_async_request_id connect(const char* connection_string);
51+
void create_entity_request(ecsact_async_request_id req_id);
52+
void connect(ecsact_async_request_id req_id, const char* connection_string);
4153

4254
void disconnect();
4355

@@ -48,16 +60,17 @@ private:
4860

4961
tick_manager tick_manager;
5062
execution_callbacks exec_callbacks;
51-
async_callbacks async_callbacks;
5263
entity_manager entity_manager;
5364

65+
detail::request_id_factory& request_id_factory;
66+
detail::async_callbacks& async_callbacks;
67+
5468
std::thread execution_thread;
5569
std::mutex execution_m;
5670

5771
std::atomic_bool is_connected = false;
5872
std::atomic_bool is_connected_notified = false;
5973

6074
std::chrono::milliseconds tick_rate = {};
61-
62-
ecsact_async_request_id next_request_id();
6375
};
76+
}; // namespace ecsact::async_reference::detail

reference/async_reference/callbacks/async_callbacks.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "async_callbacks.hh"
22

3+
using namespace ecsact::async_reference::detail;
4+
35
void async_callbacks::add(const types::async_requests type) {
46
std::unique_lock lk(async_m);
57
requests.push_back(type);

reference/async_reference/callbacks/async_callbacks.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "reference/async_reference/util/types.hh"
88
#include "reference/async_reference/util/util.hh"
99

10+
namespace ecsact::async_reference::detail {
11+
1012
class async_callbacks {
1113
public:
1214
void invoke(const ecsact_async_events_collector* async_events);
@@ -18,3 +20,5 @@ private:
1820

1921
std::mutex async_m;
2022
};
23+
24+
} // namespace ecsact::async_reference::detail

reference/async_reference/callbacks/execution_callbacks.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "execution_callbacks.hh"
22

3+
using namespace ecsact::async_reference::detail;
4+
35
execution_callbacks::execution_callbacks() {
46
collector.init_callback = &execution_callbacks::init_callback;
57
collector.update_callback = &execution_callbacks::update_callback;
@@ -158,7 +160,7 @@ void execution_callbacks::init_callback(
158160
}
159161
}
160162

161-
auto info = types::callback_info{};
163+
auto info = detail::types::callback_info{};
162164

163165
info.event = event;
164166
info.entity_id = entity_id;
@@ -175,7 +177,7 @@ void execution_callbacks::update_callback(
175177
) {
176178
auto self = static_cast<execution_callbacks*>(callback_user_data);
177179

178-
auto info = types::callback_info{};
180+
auto info = detail::types::callback_info{};
179181

180182
info.event = event;
181183
info.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
@@ -8,6 +8,7 @@
88
#include "ecsact/runtime/serialize.hh"
99
#include "reference/async_reference/util/types.hh"
1010

11+
namespace ecsact::async_reference::detail {
1112
class execution_callbacks {
1213
public:
1314
execution_callbacks();
@@ -59,3 +60,4 @@ private:
5960
void* callback_user_data
6061
);
6162
};
63+
} // namespace ecsact::async_reference::detail

reference/async_reference/detail/c_execution_options/c_execution_options.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "c_execution_options.hh"
22

3-
ecsact_execution_options detail::c_execution_options::c() {
3+
using namespace ecsact::async_reference::detail;
4+
5+
ecsact_execution_options c_execution_options::c() {
46
for(auto& action_info : actions_info) {
57
ecsact_action action;
68
action.action_data = action_info.data.data();

reference/async_reference/detail/c_execution_options/c_execution_options.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "ecsact/runtime/core.h"
77
#include "ecsact/runtime/async.h"
88

9-
namespace detail {
9+
namespace ecsact::async_reference::detail {
1010

1111
template<typename T>
1212
struct data_info {
@@ -37,4 +37,5 @@ private:
3737
std::vector<ecsact_component> adds;
3838
std::vector<ecsact_component> updates;
3939
};
40-
} // namespace detail
40+
41+
} // namespace ecsact::async_reference::detail

reference/async_reference/entity_manager/entity_manager.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "entity_manager.hh"
22

3+
using namespace ecsact::async_reference::detail;
4+
35
void entity_manager::process_entities(
46
async_callbacks& callbacks,
57
const ecsact_registry_id& registry_id

reference/async_reference/entity_manager/entity_manager.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "reference/async_reference/callbacks/async_callbacks.hh"
88
#include "reference/async_reference/util/types.hh"
99

10+
namespace ecsact::async_reference::detail {
1011
class entity_manager {
1112
public:
1213
void process_entities(
@@ -21,3 +22,4 @@ private:
2122

2223
std::vector<ecsact_async_request_id> pending_entity_requests;
2324
};
25+
} // namespace ecsact::async_reference::detail
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load("//bazel:copts.bzl", "copts")
3+
4+
cc_library(
5+
name = "request_id_factory",
6+
hdrs = ["request_id_factory.hh"],
7+
copts = copts,
8+
visibility = ["//visibility:public"],
9+
)

0 commit comments

Comments
 (0)