Skip to content

Commit

Permalink
Merge remote-tracking branch 'td/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cavallium committed Nov 23, 2020
2 parents 2588102 + 6571a99 commit 3c59d91
Show file tree
Hide file tree
Showing 30 changed files with 99 additions and 59 deletions.
1 change: 1 addition & 0 deletions benchmark/bench_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "td/utils/benchmark.h"

#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/actor/PromiseFuture.h"

#include "td/utils/common.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/db/binlog/Binlog.h"
#include "td/db/binlog/ConcurrentBinlog.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/net/HttpOutboundConnection.h"
#include "td/net/HttpQuery.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/net/HttpHeaderCreator.h"
#include "td/net/HttpInboundConnection.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench_http_server_cheat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/net/HttpHeaderCreator.h"
#include "td/net/HttpInboundConnection.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench_http_server_fast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/net/HttpHeaderCreator.h"
#include "td/net/HttpQuery.h"
Expand Down
2 changes: 1 addition & 1 deletion benchmark/bench_tddb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/UserId.h"

#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/actor/PromiseFuture.h"

#include "td/db/SqliteConnectionSafe.h"
Expand Down
1 change: 1 addition & 0 deletions benchmark/wget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/actor/PromiseFuture.h"

#include "td/net/HttpQuery.h"
Expand Down
23 changes: 11 additions & 12 deletions build.html
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,17 @@
options.push('-A Win32');
}
}
if (target === 'JNI') {
if (os_linux && linux_distro === 'Alpine') {
options.push('-DJAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/');
}
if (os_freebsd) {
options.push('-DJAVA_HOME=/usr/local/openjdk7/');
}
if (os_mac) {
options.push('-DJAVA_HOME=/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home/');
}
}
return options;
}

Expand Down Expand Up @@ -780,12 +791,6 @@
}
if (target === 'JNI') {
cmake_init_options.push('-DTD_ENABLE_JNI=ON');
if (linux_distro === 'Alpine') {
cmake_init_options.push('-DJAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/');
}
if (os_freebsd) {
cmake_init_options.push('-DJAVA_HOME=/usr/local/openjdk7/');
}
}
if (target === 'C++/CX' || target === 'C++/CLI') {
cmake_init_options.push('-DTD_ENABLE_DOTNET=ON');
Expand Down Expand Up @@ -858,12 +863,6 @@
cmake_init_options.push('-DCMAKE_TOOLCHAIN_FILE:FILEPATH=../../../vcpkg/scripts/buildsystems/vcpkg.cmake');
}
var is_alpine = os_linux && linux_distro === 'Alpine';
if (is_alpine) {
cmake_init_options.push('-DJAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/');
}
if (os_freebsd) {
cmake_init_options.push('-DJAVA_HOME=/usr/local/openjdk7/');
}
var resolve_path = use_powershell ? 'Resolve-Path' : (os_mac ? 'greadlink -e' : (is_alpine || os_freebsd || os_openbsd || os_netbsd ? 'readlink -f' : 'readlink -e'));
var resolved_path = resolve_path + ' ../td/lib/cmake/Td';
if (use_csh) {
Expand Down
15 changes: 8 additions & 7 deletions td/telegram/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "td/telegram/Log.h"

#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/utils/common.h"
#include "td/utils/crypto.h"
Expand Down Expand Up @@ -186,9 +187,7 @@ class ClientManager::Impl final {
while (!tds_.empty() && !ExitGuard::is_exited()) {
receive(0.1);
}
if (!ExitGuard::is_exited()) { // prevent closing of schedulers from already killed by OS threads
concurrent_scheduler_->finish();
}
concurrent_scheduler_->finish();
}

private:
Expand Down Expand Up @@ -395,10 +394,12 @@ class MultiImpl {
multi_td_.reset();
Scheduler::instance()->finish();
}
scheduler_thread_.join();
if (!ExitGuard::is_exited()) { // prevent closing of schedulers from already killed by OS threads
concurrent_scheduler_->finish();
if (!ExitGuard::is_exited()) {
scheduler_thread_.join();
} else {
scheduler_thread_.detach();
}
concurrent_scheduler_->finish();
}

private:
Expand All @@ -418,7 +419,7 @@ class MultiImplPool {
if (impls_.empty()) {
init_openssl_threads();

impls_.resize(clamp(thread::hardware_concurrency(), 8u, 1000u) * 5 / 4);
impls_.resize(clamp(thread::hardware_concurrency(), 8u, 24u) * 5 / 4);

net_query_stats_ = std::make_shared<NetQueryStats>();
}
Expand Down
4 changes: 4 additions & 0 deletions td/telegram/LanguagePackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "td/db/SqliteDb.h"
#include "td/db/SqliteKeyValue.h"

#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Status.h"
Expand Down Expand Up @@ -229,6 +230,9 @@ void LanguagePackManager::start_up() {
}

void LanguagePackManager::tear_down() {
if (ExitGuard::is_exited()) {
return;
}
std::lock_guard<std::mutex> lock(language_database_mutex_);
manager_count_--;
if (manager_count_ == 0) {
Expand Down
1 change: 1 addition & 0 deletions td/telegram/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "memprof/memprof.h"

Expand Down
2 changes: 1 addition & 1 deletion td/telegram/td_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/telegram/td_json_client.h"
#include "td/telegram/td_log.h"
Expand Down
3 changes: 2 additions & 1 deletion td/tl/tl_jni_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "td/tl/tl_jni_object.h"

#include "td/utils/common.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"
Expand Down Expand Up @@ -79,7 +80,7 @@ void register_native_method(JNIEnv *env, jclass clazz, std::string name, std::st

std::unique_ptr<JNIEnv, JvmThreadDetacher> get_jni_env(JavaVM *java_vm, jint jni_version) {
JNIEnv *env = nullptr;
if (java_vm->GetEnv(reinterpret_cast<void **>(&env), jni_version) == JNI_EDETACHED) {
if (!ExitGuard::is_exited() && java_vm->GetEnv(reinterpret_cast<void **>(&env), jni_version) == JNI_EDETACHED) {
#ifdef JDK1_2 // if not Android JNI
auto p_env = reinterpret_cast<void **>(&env);
#else
Expand Down
6 changes: 3 additions & 3 deletions tdactor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ endif()

#SOURCE SETS
set(TDACTOR_SOURCE
td/actor/impl/ConcurrentScheduler.cpp
td/actor/ConcurrentScheduler.cpp
td/actor/impl/Scheduler.cpp
td/actor/MultiPromise.cpp
td/actor/Timeout.cpp

td/actor/actor.h
td/actor/ConcurrentScheduler.h
td/actor/impl/Actor-decl.h
td/actor/impl/Actor.h
td/actor/impl/ActorId-decl.h
Expand All @@ -19,7 +21,6 @@ set(TDACTOR_SOURCE
td/actor/impl/ActorInfo.h
td/actor/impl/EventFull-decl.h
td/actor/impl/EventFull.h
td/actor/impl/ConcurrentScheduler.h
td/actor/impl/Event.h
td/actor/impl/Scheduler-decl.h
td/actor/impl/Scheduler.h
Expand All @@ -29,7 +30,6 @@ set(TDACTOR_SOURCE
td/actor/SignalSlot.h
td/actor/SleepActor.h
td/actor/Timeout.h
td/actor/actor.h
)

set(TDACTOR_TEST_SOURCE
Expand Down
1 change: 1 addition & 0 deletions tdactor/example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/utils/logging.h"
#include "td/utils/Time.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/actor/impl/ConcurrentScheduler.h"

#include "td/actor/impl/Actor.h"
#include "td/actor/impl/ActorId.h"
#include "td/actor/impl/ActorInfo.h"
#include "td/actor/impl/Scheduler.h"
#include "td/actor/ConcurrentScheduler.h"

#include "td/utils/ExitGuard.h"
#include "td/utils/MpscPollableQueue.h"
#include "td/utils/port/thread_local.h"

Expand Down Expand Up @@ -140,6 +136,19 @@ void ConcurrentScheduler::finish() {
detail::Iocp::Guard iocp_guard(iocp_.get());
#endif

if (ExitGuard::is_exited()) {
// prevent closing of schedulers from already killed by OS threads
for (auto &thread : threads_) {
thread.detach();
}

#if TD_PORT_WINDOWS
iocp_->interrupt_loop();
iocp_thread_.detach();
#endif
return;
}

#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED
for (auto &thread : threads_) {
thread.join();
Expand All @@ -161,4 +170,16 @@ void ConcurrentScheduler::finish() {
state_ = State::Start;
}

void ConcurrentScheduler::on_finish() {
is_finished_.store(true, std::memory_order_relaxed);
for (auto &it : schedulers_) {
it->wakeup();
}
}

void ConcurrentScheduler::register_at_finish(std::function<void()> f) {
std::lock_guard<std::mutex> lock(at_finish_mutex_);
at_finish_.push_back(std::move(f));
}

} // namespace td
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
#pragma once

#include "td/actor/impl/Scheduler-decl.h"
#include "td/actor/actor.h"

#include "td/utils/common.h"
#include "td/utils/port/thread.h"
Expand Down Expand Up @@ -44,7 +44,7 @@ class ConcurrentScheduler : private Scheduler::Callback {

void test_one_thread_run();

bool is_finished() {
bool is_finished() const {
return is_finished_.load(std::memory_order_relaxed);
}

Expand Down Expand Up @@ -84,30 +84,22 @@ class ConcurrentScheduler : private Scheduler::Callback {
private:
enum class State { Start, Run };
State state_ = State::Start;
std::vector<unique_ptr<Scheduler>> schedulers_;
std::atomic<bool> is_finished_{false};
std::mutex at_finish_mutex_;
std::vector<std::function<void()>> at_finish_;
vector<std::function<void()>> at_finish_; // can be used during destruction by Scheduler destructors
vector<unique_ptr<Scheduler>> schedulers_;
std::atomic<bool> is_finished_{false};
#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED
std::vector<thread> threads_;
vector<td::thread> threads_;
#endif
#if TD_PORT_WINDOWS
unique_ptr<detail::Iocp> iocp_;
td::thread iocp_thread_;
#endif
int32 extra_scheduler_;

void on_finish() override {
is_finished_.store(true, std::memory_order_relaxed);
for (auto &it : schedulers_) {
it->wakeup();
}
}
void on_finish() override;

void register_at_finish(std::function<void()> f) override {
std::lock_guard<std::mutex> lock(at_finish_mutex_);
at_finish_.push_back(std::move(f));
}
void register_at_finish(std::function<void()> f) override;
};

} // namespace td
1 change: 0 additions & 1 deletion tdactor/td/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
#include "td/actor/impl/Actor.h"
#include "td/actor/impl/ActorId.h"
#include "td/actor/impl/ActorInfo.h"
#include "td/actor/impl/ConcurrentScheduler.h"
#include "td/actor/impl/EventFull.h"
#include "td/actor/impl/Scheduler.h"
5 changes: 2 additions & 3 deletions tdactor/td/actor/impl/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "td/actor/impl/EventFull.h"

#include "td/utils/common.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/format.h"
#include "td/utils/List.h"
#include "td/utils/logging.h"
Expand Down Expand Up @@ -241,11 +242,9 @@ void Scheduler::clear() {
auto actor_info = ActorInfo::from_list_node(ready_actors_list_.get());
do_stop_actor(actor_info);
}
LOG_IF(FATAL, !ready_actors_list_.empty()) << ActorInfo::from_list_node(ready_actors_list_.next)->get_name();
CHECK(ready_actors_list_.empty());
poll_.clear();

if (callback_) {
if (callback_ && !ExitGuard::is_exited()) {
// can't move lambda with unique_ptr inside into std::function
auto ptr = actor_info_pool_.release();
callback_->register_at_finish([ptr] { delete ptr; });
Expand Down
1 change: 1 addition & 0 deletions tdactor/test/actors_bugs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "td/utils/tests.h"

#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/actor/Timeout.h"

using namespace td;
Expand Down
1 change: 1 addition & 0 deletions tdactor/test/actors_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "td/utils/tests.h"

#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/actor/PromiseFuture.h"

#include "td/utils/common.h"
Expand Down

0 comments on commit 3c59d91

Please sign in to comment.