| 1 | #include "StaticThreads.h" |
| 2 | |
| 3 | #include "rtc_base/thread.h" |
| 4 | #include "call/call.h" |
| 5 | |
| 6 | #include <mutex> |
| 7 | #include <algorithm> |
| 8 | |
| 9 | namespace tgcalls { |
| 10 | |
| 11 | template <class ValueT, class CreatorT> |
| 12 | class Pool : public std::enable_shared_from_this<Pool<ValueT, CreatorT>> { |
| 13 | struct Entry { |
| 14 | std::unique_ptr<ValueT> value; |
| 15 | size_t refcnt; |
| 16 | |
| 17 | bool operator < (const Entry &other) const { |
| 18 | return refcnt < other.refcnt; |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | public: |
| 23 | explicit Pool(CreatorT creator) : creator_(std::move(creator)) { |
| 24 | } |
| 25 | std::shared_ptr<ValueT> get() { |
| 26 | std::unique_lock<std::mutex> lock(mutex_); |
| 27 | set_pool_size_locked(1); |
| 28 | auto i = std::min_element(entries_.begin(), entries_.end()) - entries_.begin(); |
| 29 | return std::shared_ptr<ValueT>(entries_[i].value.get(), |
| 30 | [i, self = this->shared_from_this()](auto *ptr) { |
| 31 | self->dec_ref(i); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | void set_pool_size(size_t size) { |
| 36 | std::unique_lock<std::mutex> lock(mutex_); |
| 37 | set_pool_size_locked(size); |
| 38 | } |
| 39 | |
| 40 | void dec_ref(size_t i) { |
| 41 | std::unique_lock<std::mutex> lock(mutex_); |
| 42 | entries_.at(i).refcnt--; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | std::mutex mutex_; |
| 47 | std::vector<Entry> entries_; |
| 48 | |
| 49 | CreatorT creator_; |
| 50 | |
| 51 | void set_pool_size_locked(size_t size) { |
| 52 | for (size_t i = entries_.size(); i < size; i++) { |
| 53 | entries_.emplace_back(Entry{creator_(i + 1), 0}); |
| 54 | } |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | class ThreadsImpl : public Threads { |
| 59 | using Thread = std::unique_ptr<rtc::Thread>; |
| 60 | public: |
| 61 | explicit ThreadsImpl(size_t i) { |
| 62 | auto suffix = i == 0 ? "" : "#" + std::to_string(i); |
| 63 | media_ = create("tgc-media" + suffix); |
| 64 | worker_ = create("tgc-work" + suffix); |
| 65 | network_ = create_network("tgc-net" + suffix); |
| 66 | |
| 67 | media_->AllowInvokesToThread(worker_.get()); |
| 68 | media_->AllowInvokesToThread(network_.get()); |
| 69 | worker_->AllowInvokesToThread(network_.get()); |
| 70 | |
| 71 | //network_->DisallowAllInvokes(); |
| 72 | //worker_->DisallowAllInvokes(); |
| 73 | //worker_->AllowInvokesToThread(network_.get()); |
| 74 | } |
| 75 | |
| 76 | rtc::Thread *getNetworkThread() override { |
| 77 | return network_.get(); |
| 78 | } |
| 79 | rtc::Thread *getMediaThread() override { |
| 80 | return media_.get(); |
| 81 | } |
| 82 | rtc::Thread *getWorkerThread() override { |
| 83 | return worker_.get(); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | Thread network_; |
| 88 | Thread media_; |
| 89 | Thread worker_; |
| 90 | |
| 91 | static Thread create(const std::string &name) { |
| 92 | return init(std::unique_ptr<rtc::Thread>(rtc::Thread::Create()), name); |
| 93 | } |
| 94 | static Thread create_network(const std::string &name) { |
| 95 | return init(std::unique_ptr<rtc::Thread>(rtc::Thread::CreateWithSocketServer()), name); |
| 96 | } |
| 97 | |
| 98 | static Thread init(Thread value, const std::string &name) { |
| 99 | value->SetName(name, nullptr); |
| 100 | value->Start(); |
| 101 | return value; |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | class ThreadsCreator { |
| 106 | public: |
| 107 | std::unique_ptr<Threads> operator()(size_t i) { |
| 108 | return std::make_unique<ThreadsImpl>(i); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | Pool<Threads, ThreadsCreator> &get_pool() { |
| 113 | static auto pool = std::make_shared<Pool<Threads, ThreadsCreator>>(ThreadsCreator()); |
| 114 | return *pool; |
| 115 | } |
| 116 | |
| 117 | void Threads::setPoolSize(size_t size){ |
| 118 | get_pool().set_pool_size(size); |
| 119 | } |
| 120 | std::shared_ptr<Threads> Threads::getThreads(){ |
| 121 | return get_pool().get(); |
| 122 | } |
| 123 | |
| 124 | namespace StaticThreads { |
| 125 | |
| 126 | rtc::Thread *getNetworkThread() { |
| 127 | return getThreads()->getNetworkThread(); |
| 128 | } |
| 129 | |
| 130 | rtc::Thread *getMediaThread() { |
| 131 | return getThreads()->getMediaThread(); |
| 132 | } |
| 133 | |
| 134 | rtc::Thread *getWorkerThread() { |
| 135 | return getThreads()->getWorkerThread(); |
| 136 | } |
| 137 | |
| 138 | std::shared_ptr<Threads> &getThreads() { |
| 139 | static std::shared_ptr<Threads> threads = std::make_shared<ThreadsImpl>(0); |
| 140 | return threads; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | } |
| 145 | |