GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Alviro Iskandar Setiawan <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	Muhammad Rizki <[email protected]>,
	Kanna Scarlet <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [RFC PATCH v1 1/2] chnet: Prepare global struct ch_thpool array
Date: Mon, 29 Aug 2022 08:11:26 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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


  reply	other threads:[~2022-08-29  1:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29  1:11 [RFC PATCH v1 0/2] Fixed number of chromium workers Ammar Faizi
2022-08-29  1:11 ` Ammar Faizi [this message]
2022-08-29  4:21   ` [RFC PATCH v1 1/2] chnet: Prepare global struct ch_thpool array Alviro Iskandar Setiawan
2022-08-29  4:47     ` Ammar Nofan Faizi
2022-08-29  1:11 ` [RFC PATCH v1 2/2] chnet: Implement `get_thread()` and `put_thread()` function Ammar Faizi
2022-08-29  4:41   ` Alviro Iskandar Setiawan
2022-08-29  4:54     ` Ammar Nofan Faizi
2022-08-29  5:17       ` Alviro Iskandar Setiawan
2022-08-29  5:24         ` Alviro Iskandar Setiawan
2022-08-29  5:29           ` Ammar Nofan Faizi
2022-08-29  5:38             ` Alviro Iskandar Setiawan
2022-08-29  5:48               ` Ammar Nofan Faizi
2022-08-29  6:01                 ` Alviro Iskandar Setiawan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox