Skip to content

Commit 7fbddb2

Browse files
committed
Add: locate_thread_in for C and Rust
It helps determine the local thread rank in a larger distributed system
1 parent 339ec63 commit 7fbddb2

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

c/lib.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ size_t fu_pool_count_threads_in(fu_pool_t *pool, size_t colocation_index) {
345345
return std::visit([=](auto &variant) { return variant.threads_count(colocation_index); }, opaque->variants);
346346
}
347347

348+
size_t fu_pool_locate_thread_in(fu_pool_t *pool, size_t global_thread_index, size_t colocation_index) {
349+
assert(pool != nullptr);
350+
opaque_pool_t *opaque = reinterpret_cast<opaque_pool_t *>(pool);
351+
return std::visit([=](auto &variant) {
352+
return variant.thread_local_index(global_thread_index, colocation_index);
353+
}, opaque->variants);
354+
}
355+
348356
#pragma endregion - Lifetime
349357

350358
#pragma region - Primary API

include/fork_union.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,15 @@ size_t fu_pool_count_threads(fu_pool_t *pool);
481481
*/
482482
size_t fu_pool_count_threads_in(fu_pool_t *pool, size_t colocation_index);
483483

484+
/**
485+
* @brief Converts a global thread index to a local thread index within a colocation.
486+
* @param[in] pool Thread pool handle, must not be NULL.
487+
* @param[in] global_thread_index The global thread index to convert.
488+
* @param[in] colocation_index Index of the colocation, must be < `fu_pool_count_colocations(pool)`.
489+
* @retval Local thread index within the specified colocation.
490+
*/
491+
size_t fu_pool_locate_thread_in(fu_pool_t *pool, size_t global_thread_index, size_t colocation_index);
492+
484493
#pragma endregion - Lifetime
485494

486495
#pragma region - Primary API

rust/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ extern "C" {
353353
fn fu_pool_count_threads(pool: *mut c_void) -> usize;
354354
fn fu_pool_count_colocations(pool: *mut c_void) -> usize;
355355
fn fu_pool_count_threads_in(pool: *mut c_void, colocation_index: usize) -> usize;
356+
fn fu_pool_locate_thread_in(pool: *mut c_void, global_thread_index: usize, colocation_index: usize) -> usize;
356357

357358
#[allow(dead_code)]
358359
fn fu_pool_for_threads(
@@ -683,6 +684,24 @@ impl ThreadPool {
683684
unsafe { fu_pool_count_threads_in(self.inner, colocation_index) }
684685
}
685686

687+
/// Converts a global thread index to a local thread index within a colocation.
688+
///
689+
/// This is useful for distributed thread pools where threads are grouped into
690+
/// colocations (NUMA nodes or QoS levels). The local index can be used for
691+
/// per-colocation data structures or algorithms.
692+
///
693+
/// # Arguments
694+
///
695+
/// * `global_thread_index` - The global thread index to convert
696+
/// * `colocation_index` - The colocation to get the local index for
697+
///
698+
/// # Returns
699+
///
700+
/// The local thread index within the specified colocation.
701+
pub fn locate_thread_in(&self, global_thread_index: usize, colocation_index: usize) -> usize {
702+
unsafe { fu_pool_locate_thread_in(self.inner, global_thread_index, colocation_index) }
703+
}
704+
686705
/// Transitions worker threads to a power-saving sleep state.
687706
///
688707
/// This function places worker threads into a low-power sleep state when no work

0 commit comments

Comments
 (0)