Nagram/TMessagesProj/jni/voip/tgcalls/StaticThreads.cpp

152 lines
3.9 KiB
C++
Raw Normal View History

2021-03-19 10:25:58 +00:00
#include "StaticThreads.h"
#include "rtc_base/thread.h"
2021-04-09 13:17:32 +00:00
#include "call/call.h"
2021-03-19 10:25:58 +00:00
#include <mutex>
#include <algorithm>
namespace tgcalls {
template <class ValueT, class CreatorT>
class Pool : public std::enable_shared_from_this<Pool<ValueT, CreatorT>> {
struct Entry {
std::unique_ptr<ValueT> value;
size_t refcnt;
bool operator < (const Entry &other) const {
return refcnt < other.refcnt;
}
};
public:
explicit Pool(CreatorT creator) : creator_(std::move(creator)) {
}
std::shared_ptr<ValueT> get() {
std::unique_lock<std::mutex> lock(mutex_);
set_pool_size_locked(1);
auto i = std::min_element(entries_.begin(), entries_.end()) - entries_.begin();
return std::shared_ptr<ValueT>(entries_[i].value.get(),
[i, self = this->shared_from_this()](auto *ptr) {
self->dec_ref(i);
});
}
void set_pool_size(size_t size) {
std::unique_lock<std::mutex> lock(mutex_);
set_pool_size_locked(size);
}
void dec_ref(size_t i) {
std::unique_lock<std::mutex> lock(mutex_);
entries_.at(i).refcnt--;
}
private:
std::mutex mutex_;
std::vector<Entry> entries_;
CreatorT creator_;
void set_pool_size_locked(size_t size) {
for (size_t i = entries_.size(); i < size; i++) {
entries_.emplace_back(Entry{creator_(i + 1), 0});
}
}
};
class ThreadsImpl : public Threads {
using Thread = std::unique_ptr<rtc::Thread>;
public:
explicit ThreadsImpl(size_t i) {
auto suffix = i == 0 ? "" : "#" + std::to_string(i);
media_ = create("tgc-media" + suffix);
2022-03-11 16:49:54 +00:00
//worker_ = create("tgc-work" + suffix);
worker_ = create_network("tgc-work" + suffix);
//network_ = create_network("tgc-net" + suffix);
//network_->DisallowAllInvokes();
//worker_->DisallowAllInvokes();
//worker_->AllowInvokesToThread(network_.get());
2021-03-19 10:25:58 +00:00
}
rtc::Thread *getNetworkThread() override {
2022-03-11 16:49:54 +00:00
return worker_.get();
2021-03-19 10:25:58 +00:00
}
rtc::Thread *getMediaThread() override {
return media_.get();
}
rtc::Thread *getWorkerThread() override {
return worker_.get();
}
2021-04-09 13:17:32 +00:00
rtc::scoped_refptr<webrtc::SharedModuleThread> getSharedModuleThread() override {
2021-06-25 00:43:10 +00:00
// This function must be called from a single thread because of SharedModuleThread implementation
// So we don't care about making it thread safe
if (!shared_module_thread_) {
shared_module_thread_ = webrtc::SharedModuleThread::Create(
webrtc::ProcessThread::Create("tgc-module"),
[=] { shared_module_thread_ = nullptr; });
}
2021-04-09 13:17:32 +00:00
return shared_module_thread_;
}
2021-03-19 10:25:58 +00:00
private:
2022-03-11 16:49:54 +00:00
//Thread network_;
2021-03-19 10:25:58 +00:00
Thread media_;
Thread worker_;
2021-04-09 13:17:32 +00:00
rtc::scoped_refptr<webrtc::SharedModuleThread> shared_module_thread_;
2021-03-19 10:25:58 +00:00
static Thread create(const std::string &name) {
return init(std::unique_ptr<rtc::Thread>(rtc::Thread::Create()), name);
}
static Thread create_network(const std::string &name) {
return init(std::unique_ptr<rtc::Thread>(rtc::Thread::CreateWithSocketServer()), name);
}
static Thread init(Thread value, const std::string &name) {
value->SetName(name, nullptr);
value->Start();
return value;
}
};
class ThreadsCreator {
public:
std::unique_ptr<Threads> operator()(size_t i) {
return std::make_unique<ThreadsImpl>(i);
}
};
Pool<Threads, ThreadsCreator> &get_pool() {
static auto pool = std::make_shared<Pool<Threads, ThreadsCreator>>(ThreadsCreator());
return *pool;
}
void Threads::setPoolSize(size_t size){
get_pool().set_pool_size(size);
}
std::shared_ptr<Threads> Threads::getThreads(){
return get_pool().get();
}
namespace StaticThreads {
rtc::Thread *getNetworkThread() {
return getThreads()->getNetworkThread();
}
rtc::Thread *getMediaThread() {
return getThreads()->getMediaThread();
}
rtc::Thread *getWorkerThread() {
return getThreads()->getWorkerThread();
}
std::shared_ptr<Threads> &getThreads() {
static std::shared_ptr<Threads> threads = std::make_shared<ThreadsImpl>(0);
return threads;
}
};
}