2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-03 07:56:03 +00:00

Use limited number of threads in MTP.

This commit is contained in:
John Preston
2019-11-27 18:09:56 +03:00
parent c742d7406c
commit 64535251e8
4 changed files with 74 additions and 32 deletions

View File

@@ -146,29 +146,28 @@ void SessionData::detach() {
Session::Session(
not_null<Instance*> instance,
not_null<QThread*> thread,
ShiftedDcId shiftedDcId,
not_null<Dcenter*> dc)
: _instance(instance)
, _shiftedDcId(shiftedDcId)
, _dc(dc)
, _data(std::make_shared<SessionData>(this))
, _thread(thread)
, _sender([=] { needToResumeAndSend(); }) {
_timeouter.callEach(1000);
refreshOptions();
watchDcKeyChanges();
watchDcOptionsChanges();
start();
}
Session::~Session() {
Expects(!_connection);
Expects(!_thread);
if (_myKeyCreation != CreatingKeyType::None) {
releaseKeyCreationOnFail();
}
for (const auto &thread : _destroyingThreads) {
thread->wait();
}
}
void Session::watchDcKeyChanges() {
@@ -210,27 +209,11 @@ void Session::watchDcOptionsChanges() {
void Session::start() {
killConnection();
_thread = std::make_unique<QThread>();
const auto thread = _thread.get();
connect(thread, &QThread::finished, [=] {
InvokeQueued(this, [=] {
const auto i = ranges::find(
_destroyingThreads,
thread,
&std::unique_ptr<QThread>::get);
if (i != _destroyingThreads.end()) {
_destroyingThreads.erase(i);
}
});
});
_connection = new Connection(
_instance,
thread,
_thread.get(),
_data,
_shiftedDcId);
thread->start();
}
bool Session::rpcErrorOccured(
@@ -578,18 +561,13 @@ void Session::tryToReceive() {
}
void Session::killConnection() {
Expects(!_thread || _connection);
if (!_connection) {
return;
}
base::take(_connection)->deleteLater();
_destroyingThreads.push_back(base::take(_thread));
_destroyingThreads.back()->quit();
Ensures(_connection == nullptr);
Ensures(_thread == nullptr);
}
} // namespace internal