Skip to content

Commit 2766e7d

Browse files
committed
emscripten: td_get_timeout
GitOrigin-RevId: 920dac2d11ed999019e7faafe47cadf96d06900f
1 parent 66d5c69 commit 2766e7d

16 files changed

Lines changed: 85 additions & 53 deletions

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ if (EMSCRIPTEN)
739739
set(TD_EMSCRIPTEN_SRC td/telegram/td_emscripten.cpp)
740740
add_executable(${TD_EMSCRIPTEN} ${TD_EMSCRIPTEN_SRC})
741741
target_include_directories(${TD_EMSCRIPTEN} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
742-
target_link_libraries(${TD_EMSCRIPTEN} PRIVATE tdjson_static)
742+
target_link_libraries(${TD_EMSCRIPTEN} PRIVATE tdjson_static tdactor)
743743
endif()
744744

745745
#EXECUTABLES

‎benchmark/bench_actor.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class QueryBench : public td::Benchmark {
247247
void run(int n) override {
248248
// first actor is on main_thread
249249
{
250-
auto guard = scheduler_->get_current_guard();
250+
auto guard = scheduler_->get_main_guard();
251251
send_closure(server_, &ServerActor::run, n);
252252
}
253253
while (scheduler_->run_main(10)) {

‎benchmark/bench_db.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
145145
scheduler_->start();
146146
}
147147
void run(int n) override {
148-
auto guard = scheduler_->get_current_guard();
148+
auto guard = scheduler_->get_main_guard();
149149

150150
for (int i = 0; i < n; i++) {
151151
auto key = td::to_string(i % 10);
@@ -156,7 +156,7 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
156156
void tear_down() override {
157157
scheduler_->run_main(0.1);
158158
{
159-
auto guard = scheduler_->get_current_guard();
159+
auto guard = scheduler_->get_main_guard();
160160
sqlite_kv_async_.reset();
161161
sqlite_kv_safe_.reset();
162162
sql_connection_->close_and_destroy();
@@ -176,7 +176,7 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
176176
scheduler_ = std::make_unique<td::ConcurrentScheduler>();
177177
scheduler_->init(1);
178178

179-
auto guard = scheduler_->get_current_guard();
179+
auto guard = scheduler_->get_main_guard();
180180

181181
td::string sql_db_name = "testdb.sqlite";
182182
td::SqliteDb::destroy(sql_db_name).ignore();

‎benchmark/bench_tddb.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MessagesDbBench : public Benchmark {
4343
scheduler_->start();
4444
}
4545
void run(int n) override {
46-
auto guard = scheduler_->get_current_guard();
46+
auto guard = scheduler_->get_main_guard();
4747
for (int i = 0; i < n; i += 20) {
4848
auto dialog_id = DialogId{UserId{Random::fast(1, 100)}};
4949
auto message_id_raw = Random::fast(1, 100000);
@@ -64,7 +64,7 @@ class MessagesDbBench : public Benchmark {
6464
void tear_down() override {
6565
scheduler_->run_main(0.1);
6666
{
67-
auto guard = scheduler_->get_current_guard();
67+
auto guard = scheduler_->get_main_guard();
6868
sql_connection_.reset();
6969
messages_db_sync_safe_.reset();
7070
messages_db_async_.reset();
@@ -85,7 +85,7 @@ class MessagesDbBench : public Benchmark {
8585
scheduler_ = std::make_unique<ConcurrentScheduler>();
8686
scheduler_->init(1);
8787

88-
auto guard = scheduler_->get_current_guard();
88+
auto guard = scheduler_->get_main_guard();
8989

9090
string sql_db_name = "testdb.sqlite";
9191
sql_connection_ = std::make_shared<SqliteConnectionSafe>(sql_db_name);

‎td/telegram/Client.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Client::Impl final {
6060

6161
Response receive(double timeout) {
6262
if (!requests_.empty()) {
63-
auto guard = concurrent_scheduler_->get_current_guard();
63+
auto guard = concurrent_scheduler_->get_main_guard();
6464
for (auto &request : requests_) {
6565
send_closure_later(td_, &Td::request, request.id, std::move(request.function));
6666
}
@@ -69,6 +69,8 @@ class Client::Impl final {
6969

7070
if (responses_.empty()) {
7171
concurrent_scheduler_->run_main(0);
72+
} else {
73+
ConcurrentScheduler::emscripten_clear_main_timeout();
7274
}
7375
if (!responses_.empty()) {
7476
auto result = std::move(responses_.front());
@@ -84,7 +86,7 @@ class Client::Impl final {
8486
Impl &operator=(Impl &&) = delete;
8587
~Impl() {
8688
{
87-
auto guard = concurrent_scheduler_->get_current_guard();
89+
auto guard = concurrent_scheduler_->get_main_guard();
8890
td_.reset();
8991
}
9092
while (!closed_) {

‎td/telegram/cli.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3623,7 +3623,7 @@ void main(int argc, char **argv) {
36233623
.release();
36243624

36253625
scheduler.start();
3626-
while (scheduler.run_main(100)) {
3626+
while (scheduler.run_main(td::Timestamp::in(100))) {
36273627
}
36283628
scheduler.finish();
36293629
}

‎td/telegram/td_emscripten.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "td/telegram/td_json_client.h"
1111
#include "td/telegram/td_log.h"
12+
#include "td/actor/actor.h"
1213

1314
#include <emscripten.h>
1415

@@ -32,6 +33,9 @@ EMSCRIPTEN_KEEPALIVE void td_destroy(void *client) {
3233
EMSCRIPTEN_KEEPALIVE void td_set_verbosity(int verbosity) {
3334
td_set_log_verbosity_level(verbosity);
3435
}
36+
EMSCRIPTEN_KEEPALIVE double td_get_timeout() {
37+
return td::ConcurrentScheduler::emscripten_get_main_timeout();
38+
}
3539
}
3640

3741
int main(void) {

‎tdactor/example/example.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ int main(void) {
3838
scheduler.init(4 /*threads_count*/);
3939
scheduler.start();
4040
{
41-
auto guard = scheduler.get_current_guard();
41+
auto guard = scheduler.get_main_guard();
4242
td::create_actor_on_scheduler<MainActor>("Main actor", 0).release();
4343
}
4444
while (!scheduler.is_finished()) {
45-
scheduler.run_main(10);
45+
scheduler.run_main(td::Timestamp::in(10));
4646
}
4747
scheduler.finish();
4848
return 0;

‎tdactor/td/actor/impl/ConcurrentScheduler.cpp‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ConcurrentScheduler::init(int32 threads_n) {
6767
void ConcurrentScheduler::test_one_thread_run() {
6868
do {
6969
for (auto &sched : schedulers_) {
70-
sched->run(0);
70+
sched->run(Timestamp::now_cached());
7171
}
7272
} while (!is_finished_.load(std::memory_order_relaxed));
7373
}
@@ -85,7 +85,7 @@ void ConcurrentScheduler::start() {
8585
td::detail::Iocp::Guard iocp_guard(iocp_.get());
8686
#endif
8787
while (!is_finished()) {
88-
sched->run(10);
88+
sched->run(Timestamp::in(10));
8989
}
9090
}));
9191
}
@@ -99,8 +99,9 @@ void ConcurrentScheduler::start() {
9999

100100
state_ = State::Run;
101101
}
102+
static TD_THREAD_LOCAL double emscripten_timeout;
102103

103-
bool ConcurrentScheduler::run_main(double timeout) {
104+
bool ConcurrentScheduler::run_main(Timestamp timeout) {
104105
CHECK(state_ == State::Run);
105106
// run main scheduler in same thread
106107
auto &main_sched = schedulers_[0];
@@ -110,9 +111,25 @@ bool ConcurrentScheduler::run_main(double timeout) {
110111
#endif
111112
main_sched->run(timeout);
112113
}
114+
115+
// hack for emscripten
116+
emscripten_timeout = get_main_timeout().at();
117+
113118
return !is_finished();
114119
}
115120

121+
Timestamp ConcurrentScheduler::get_main_timeout() {
122+
CHECK(state_ == State::Run);
123+
return schedulers_[0]->get_timeout();
124+
}
125+
126+
double ConcurrentScheduler::emscripten_get_main_timeout() {
127+
return Timestamp::at(emscripten_timeout).in();
128+
}
129+
void ConcurrentScheduler::emscripten_clear_main_timeout() {
130+
emscripten_timeout = 0;
131+
}
132+
116133
void ConcurrentScheduler::finish() {
117134
CHECK(state_ == State::Run);
118135
if (!is_finished()) {

‎tdactor/td/actor/impl/ConcurrentScheduler.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ConcurrentScheduler : private Scheduler::Callback {
3434
void wakeup() {
3535
schedulers_[0]->wakeup();
3636
}
37-
SchedulerGuard get_current_guard() {
37+
SchedulerGuard get_main_guard() {
3838
return schedulers_[0]->get_guard();
3939
}
4040

@@ -50,7 +50,14 @@ class ConcurrentScheduler : private Scheduler::Callback {
5050

5151
void start();
5252

53-
bool run_main(double timeout);
53+
bool run_main(double timeout) {
54+
return run_main(Timestamp::in(timeout));
55+
}
56+
bool run_main(Timestamp timeout);
57+
58+
Timestamp get_main_timeout();
59+
static double emscripten_get_main_timeout();
60+
static void emscripten_clear_main_timeout();
5461

5562
void finish();
5663

0 commit comments

Comments
 (0)