From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [182.2.68.216]) by gnuweeb.org (Postfix) with ESMTPSA id B906080B0D; Mon, 29 Aug 2022 01:12:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661735525; bh=j4/9nZqkEE8YF7CGXY9N27p5lZGiPqRBksbf70L1YyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JjK615sXozweplijMtghPjBFY22TS69/8vmMiUU/xrxkEUe5Ah1ADVHrB5EeAOdvp 7YUOFBrfowkyf0QeocB5LZatGce6jKC8Erggia/C8g6IKStmIdJxltL+aMm0+Tp2kj 5o+np5qe9b+9TH5kYi4QHGhuuYw2zTOmzpOZLCEK+0U5XtD90B/czAAIgf3XjCId7F Cvq55wGCJul2Y6SxC9RzgsGjkU2ByQbR0D1BGe0kJVs5ggM1+LTrmYSUL18hydrLU3 oelmcXNTSDvwyu/pkiL48qx/tHDDFKI/gte12xCK+jZSU4tX7Ql3aZxwyGgTwGb7Cx GikxcJ/K53FKA== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [RFC PATCH v1 1/2] chnet: Prepare global struct ch_thpool array Date: Mon, 29 Aug 2022 08:11:26 +0700 Message-Id: <20220829011127.3150320-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829011127.3150320-1-ammarfaizi2@gnuweeb.org> References: <20220829011127.3150320-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This is a preparation patch to add "fixed number of chromium workers". Initialize array of pointers `struct ch_thpool` with size `std::thread::hardware_concurrency() - 1`. Signed-off-by: Ammar Faizi --- chnet/chnet.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/chnet/chnet.cc b/chnet/chnet.cc index 155dba8..8662374 100644 --- a/chnet/chnet.cc +++ b/chnet/chnet.cc @@ -607,6 +607,51 @@ const char *CHNet::read_buf(void) return ch_->read_buf(); } +struct ch_thpool { +public: + base::Thread thread_; + uint32_t ref_count_; + uint32_t idx_; + + ch_thpool(void); +}; + +ch_thpool::ch_thpool(void): + thread_("chromium_thread"), + ref_count_(0) +{ + CHECK((void *)this == (void *)&thread_); + base::Thread::Options options(base::MessagePumpType::IO, 0); + CHECK(thread_.StartWithOptions(std::move(options))); +} + +static uint32_t g_max_ch_thpool; +static std::mutex g_thpool_lock_; +static struct ch_thpool **g_thpool; + +static void init_g_ch_thpool(void) +{ + struct ch_thpool **tmp; + + g_max_ch_thpool = std::thread::hardware_concurrency() - 1; + if (g_max_ch_thpool < 1) + g_max_ch_thpool = 1; + + tmp = (struct ch_thpool **)calloc(g_max_ch_thpool, sizeof(*tmp)); + g_thpool_lock_.lock(); + free(g_thpool); + g_thpool = tmp; + g_thpool_lock_.unlock(); +} + +static void destroy_g_ch_thpool(void) +{ + g_thpool_lock_.lock(); + free(g_thpool); + g_thpool = nullptr; + g_thpool_lock_.unlock(); +} + extern "C" { static base::AtExitManager *at_exit_manager; @@ -641,6 +686,7 @@ __cold void chnet_global_init(void) chnet_fix_chromium_boringssl(); SSL_library_init(); __chnet_global_init(); + init_g_ch_thpool(); has_initialized = true; init_lock.unlock(); } @@ -653,6 +699,7 @@ __cold void chnet_global_stop(void) delete cleanup; at_exit_manager = NULL; cleanup = NULL; + destroy_g_ch_thpool(); has_initialized = false; init_lock.unlock(); } -- Ammar Faizi