diff --git a/Userland/Services/RequestServer/ConnectionFromClient.cpp b/Userland/Services/RequestServer/ConnectionFromClient.cpp index 583854f2d4b..8bb1d22f339 100644 --- a/Userland/Services/RequestServer/ConnectionFromClient.cpp +++ b/Userland/Services/RequestServer/ConnectionFromClient.cpp @@ -21,12 +21,27 @@ namespace RequestServer { +template +struct Looper : public Threading::ThreadPoolLooper { + IterationDecision next(Pool& pool, bool wait); + Core::EventLoop event_loop; +}; + +struct ThreadPoolEntry { + NonnullRefPtr client; + ConnectionFromClient::Work work; +}; +static Threading::ThreadPool s_thread_pool { + [](ThreadPoolEntry entry) { + entry.client->worker_do_work(move(entry.work)); + } +}; + static HashMap> s_connections; static IDAllocator s_client_ids; ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr socket) : IPC::ConnectionFromClient(*this, move(socket), s_client_ids.allocate()) - , m_thread_pool([this](Work work) { worker_do_work(move(work)); }) { s_connections.set(client_id(), *this); } @@ -74,7 +89,7 @@ private: }; template -IterationDecision ConnectionFromClient::Looper::next(Pool& pool, bool wait) +IterationDecision Looper::next(Pool& pool, bool wait) { bool should_exit = false; auto timer = Core::Timer::create_repeating(100, [&] { @@ -187,7 +202,7 @@ Messages::RequestServer::ConnectNewClientResponse ConnectionFromClient::connect_ void ConnectionFromClient::enqueue(Work work) { - m_thread_pool.submit(move(work)); + s_thread_pool.submit({ *this, move(work) }); } Messages::RequestServer::IsSupportedProtocolResponse ConnectionFromClient::is_supported_protocol(ByteString const& protocol) diff --git a/Userland/Services/RequestServer/ConnectionFromClient.h b/Userland/Services/RequestServer/ConnectionFromClient.h index 6303e5ad3b7..f8e5d9f73bf 100644 --- a/Userland/Services/RequestServer/ConnectionFromClient.h +++ b/Userland/Services/RequestServer/ConnectionFromClient.h @@ -32,6 +32,23 @@ public: void did_progress_request(Badge, Request&); void did_request_certificates(Badge, Request&); + struct StartRequest { + i32 request_id; + ByteString method; + URL::URL url; + HTTP::HeaderMap request_headers; + ByteBuffer request_body; + Core::ProxyData proxy_data; + }; + + struct EnsureConnection { + URL::URL url; + CacheLevel cache_level; + }; + + using Work = Variant; + void worker_do_work(Work); + private: explicit ConnectionFromClient(NonnullOwnPtr); @@ -49,36 +66,11 @@ private: virtual void websocket_close(i32, u16, ByteString const&) override; virtual Messages::RequestServer::WebsocketSetCertificateResponse websocket_set_certificate(i32, ByteString const&, ByteString const&) override; - struct StartRequest { - i32 request_id; - ByteString method; - URL::URL url; - HTTP::HeaderMap request_headers; - ByteBuffer request_body; - Core::ProxyData proxy_data; - }; - - struct EnsureConnection { - URL::URL url; - CacheLevel cache_level; - }; - - using Work = Variant; - - void worker_do_work(Work); - Threading::MutexProtected>> m_requests; HashMap> m_websockets; void enqueue(Work); - template - struct Looper : public Threading::ThreadPoolLooper { - IterationDecision next(Pool& pool, bool wait); - Core::EventLoop event_loop; - }; - - Threading::ThreadPool m_thread_pool; Threading::Mutex m_ipc_mutex; };