From 06cd3ac67f9e039219df21d4bf63bf53b666355a Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 14 Jul 2024 13:19:10 +0300 Subject: expose max number of concurrent threads to users + Can be used to build fast hashmaps to speed up ipc --- include/kmi/syscalls.h | 9 +++++++++ include/kmi/tcb.h | 5 +++++ src/tcb.c | 19 ++++++++++++------- src/uapi/conf.c | 4 ++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/kmi/syscalls.h b/include/kmi/syscalls.h index 5079d74..4319812 100644 --- a/include/kmi/syscalls.h +++ b/include/kmi/syscalls.h @@ -258,6 +258,15 @@ enum conf_param { * \c R */ CONF_PAGE_SIZE, + + /** + * Maximum number of threads active at the same time. + * Can be used by userspace to build a pretty fast hashmaps of thread + * IDs to whatever, see \ref get_tcb(). + * Guaranteed to be some power of 2. + * \c R + */ + CONF_MAX_THREADS, }; /** Capabilities of process. */ diff --git a/include/kmi/tcb.h b/include/kmi/tcb.h index a6522f8..4214cb0 100644 --- a/include/kmi/tcb.h +++ b/include/kmi/tcb.h @@ -198,6 +198,11 @@ struct tcb { */ void init_tcbs(); +/** + * @return Maximum number of threads active at the same time. + */ +size_t max_tcbs(); + /** * Destroy thread control subsystem. */ diff --git a/src/tcb.c b/src/tcb.c index 506b756..238d8e3 100644 --- a/src/tcb.c +++ b/src/tcb.c @@ -26,8 +26,8 @@ /** Thread ID to start looking from when allocating new ID. */ static id_t start_tid = 0; -/** Total number of possible thread IDs. */ -static id_t num_tids; +/** Maximum number of concurrently active threads. */ +static id_t num_tcbs; /** Pointer to array of \ref tcb structures. Length of the array is \c num_tids.*/ static struct tcb **tcbs; @@ -40,14 +40,19 @@ static struct tcb **tcbs; */ static struct tcb *__cpu_tcb[MAX_CPUS] = { 0 }; +size_t max_tcbs() +{ + return num_tcbs; +} + void init_tcbs() { /* MM_O1 is 2MiB on riscv64, so 262144 different possible thread ids. * Should be enough, if we're really strapped for memory I might try * something smaller but this is fine for now. */ tcbs = (struct tcb **)alloc_page(MM_O1); - num_tids = order_size(MM_O1) / sizeof(struct tcb *); - assert(is_powerof2(num_tids)); + num_tcbs = order_size(MM_O1) / sizeof(struct tcb *); + assert(is_powerof2(num_tcbs)); memset(tcbs, 0, order_size(MM_O1)); } @@ -74,10 +79,10 @@ static id_t __alloc_tid(struct tcb *t) if (i == stop_tid) return ERR_NF; - if (tcbs[i & (num_tids - 1)] || i == 0) + if (tcbs[i & (num_tcbs - 1)] || i == 0) continue; - tcbs[i & (num_tids - 1)] = t; + tcbs[i & (num_tcbs - 1)] = t; start_tid = i + 1; return i; } @@ -369,7 +374,7 @@ struct tcb *get_tcb(id_t tid) if (tid <= 0) return NULL; - struct tcb *t = tcbs[tid & (num_tids - 1)]; + struct tcb *t = tcbs[tid & (num_tcbs - 1)]; if (!t) return NULL; diff --git a/src/uapi/conf.c b/src/uapi/conf.c index 6a801f3..24a4706 100644 --- a/src/uapi/conf.c +++ b/src/uapi/conf.c @@ -76,6 +76,10 @@ SYSCALL_DEFINE2(get_conf)(struct tcb *t, sys_arg_t param, sys_arg_t d0) val = order_size(d0); break; + case CONF_MAX_THREADS: + val = max_tcbs(); + break; + default: return_args1(t, ERR_NF); } -- cgit v1.3