Skip to content

Commit 8463f60

Browse files
author
Grok Compression
committed
core: expose thread pool
1 parent bbe7eec commit 8463f60

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/lib/codec/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include_directories(
1414
${CMAKE_CURRENT_BINARY_DIR}/../../lib/core # grk_config.h and grk_config_private.h
1515
${GROK_SOURCE_DIR}/src/lib/core
1616
${GROK_SOURCE_DIR}/src/lib/core/util
17+
${GROK_SOURCE_DIR}/src/lib/core/scheduling
1718
${GROK_SOURCE_DIR}/src/include
1819
${GROK_SOURCE_DIR}/src/include/CLI11/include
1920
${CMAKE_CURRENT_SOURCE_DIR}/formats

src/lib/core/grok.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ void grk_deinitialize(void)
223223
TFSingleton::destroy();
224224
}
225225

226+
void* grk_thread_pool(void)
227+
{
228+
return &TFSingleton::get();
229+
}
230+
231+
size_t grk_num_workers(void)
232+
{
233+
return TFSingleton::num_threads();
234+
}
235+
236+
uint32_t grk_worker_id(void)
237+
{
238+
return TFSingleton::workerId();
239+
}
240+
226241
static inline bool areStringsEqual(const char* lhs, const char* rhs)
227242
{
228243
if(lhs == nullptr && rhs == nullptr)

src/lib/core/grok.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,43 @@ GRK_API int32_t GRK_CALLCONV grk_plugin_batch_decompress(void);
23252325
*/
23262326
GRK_API void GRK_CALLCONV grk_plugin_stop_batch_decompress(void);
23272327

2328+
/*******************************************************************************
2329+
* Thread-pool access
2330+
*
2331+
* These functions expose the core library's TaskFlow thread pool so that
2332+
* other libraries (e.g. the codec library) can submit work to the same
2333+
* pool, avoiding thread contention.
2334+
*
2335+
* The returned handle is only valid between grk_initialize() and
2336+
* grk_deinitialize(). Internal C++ consumers should prefer the typed
2337+
* wrappers in grk_thread_pool.h.
2338+
******************************************************************************/
2339+
2340+
/**
2341+
* @brief Returns an opaque handle to the core thread-pool executor.
2342+
*
2343+
* The handle is a pointer to the internal tf::Executor. Cast it back
2344+
* with the helpers in grk_thread_pool.h (C++ only) or use
2345+
* grk_num_workers() / grk_worker_id() from C.
2346+
*
2347+
* @return opaque executor handle, or NULL if the library is not initialised
2348+
*/
2349+
GRK_API void* GRK_CALLCONV grk_thread_pool(void);
2350+
2351+
/**
2352+
* @brief Returns the number of worker threads in the core thread pool.
2353+
*
2354+
* @return total number of threads (including the driver thread)
2355+
*/
2356+
GRK_API size_t GRK_CALLCONV grk_num_workers(void);
2357+
2358+
/**
2359+
* @brief Returns the TaskFlow worker id of the calling thread.
2360+
*
2361+
* @return worker id if called from inside a TaskFlow task, 0 otherwise
2362+
*/
2363+
GRK_API uint32_t GRK_CALLCONV grk_worker_id(void);
2364+
23282365
#ifndef SWIG
23292366
#ifdef __cplusplus
23302367
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2016-2026 Grok Image Compression Inc.
3+
*
4+
* This source code is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This source code is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
*/
17+
18+
#pragma once
19+
20+
#include "grok.h"
21+
#include "grk_taskflow.h"
22+
23+
namespace grk
24+
{
25+
26+
/**
27+
* @brief Returns a reference to the core library's tf::Executor.
28+
*
29+
* The executor is created by grk_initialize() and destroyed by
30+
* grk_deinitialize(). Calling this outside that window is undefined.
31+
*/
32+
inline tf::Executor& executor()
33+
{
34+
return *static_cast<tf::Executor*>(grk_thread_pool());
35+
}
36+
37+
/**
38+
* @brief Returns the total number of worker threads in the core pool.
39+
*/
40+
inline size_t num_workers()
41+
{
42+
return grk_num_workers();
43+
}
44+
45+
/**
46+
* @brief Returns the TaskFlow worker id of the calling thread.
47+
*
48+
* @return worker id if called from inside a TaskFlow task, 0 otherwise
49+
*/
50+
inline uint32_t worker_id()
51+
{
52+
return grk_worker_id();
53+
}
54+
55+
} // namespace grk

0 commit comments

Comments
 (0)