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