Implementation:Google deepmind Mujoco Thread Pool Header
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Threading, Memory Management, Task Scheduling |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
Header declaring the MuJoCo thread pool C API and the per-thread stack shard memory layout structure used for multi-threaded simulation.
Description
This header defines the public C API for MuJoCo's thread pool system and the mjStackInfo structure that describes per-thread stack memory shards. The stack sharding layout splits the mjData arena into per-thread regions so each worker can allocate temporary memory without contention. The header documents the memory layout with a detailed ASCII diagram showing how the arena is divided: used arena, free arena, and per-shard regions each containing stack info metadata, free stack space, and used stack growing downward.
Usage
Included by any code that needs to create, configure, or interact with MuJoCo's thread pool. The typical workflow is: create a pool with mju_threadPoolCreate, bind it to simulation data with mju_bindThreadPool, enqueue tasks with mju_threadPoolEnqueue, and destroy with mju_threadPoolDestroy.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: src/thread/thread_pool.h
- Lines: 1-85
Key Functions
// Per-thread stack memory shard descriptor
typedef struct {
uintptr_t bottom; // First memory address available to the stack
uintptr_t top; // Current memory address used by the stack
uintptr_t limit; // Top limit of the stack (stack grows down)
uintptr_t stack_base; // Current stack base for mark and free stack
} mjStackInfo;
// Create a thread pool with specified number of threads
MJAPI mjThreadPool* mju_threadPoolCreate(size_t number_of_threads);
// Get stack info for a specific thread's shard
mjStackInfo* mju_getStackInfoForThread(mjData* d, size_t thread_id);
// Bind thread pool to mjData for multi-threaded use
MJAPI void mju_bindThreadPool(mjData* d, void* thread_pool);
// Query thread pool state
MJAPI size_t mju_threadPoolNumberOfThreads(mjThreadPool* thread_pool);
MJAPI size_t mju_threadPoolCurrentWorkerId(mjThreadPool* thread_pool);
// Task management
MJAPI void mju_threadPoolEnqueue(mjThreadPool* thread_pool, mjTask* task);
// Arena allocation mutex
MJAPI void mju_threadPoolLockAllocMutex(mjThreadPool* thread_pool);
MJAPI void mju_threadPoolUnlockAllocMutex(mjThreadPool* thread_pool);
// Lifecycle
MJAPI void mju_threadPoolDestroy(mjThreadPool* thread_pool);
// Architecture info
MJAPI size_t mju_getDestructiveInterferenceSize(void);
Import
#include "thread/thread_pool.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| number_of_threads | size_t | Yes | Number of worker threads to create |
| d | mjData* | Yes (for bind/getStackInfo) | Simulation data to configure for multi-threading |
| thread_pool | mjThreadPool* / void* | Yes | Thread pool handle |
| task | mjTask* | Yes (for enqueue) | Task to execute on a worker thread |
| thread_id | size_t | Yes (for getStackInfo) | Worker thread ID to query |
Outputs
| Name | Type | Description |
|---|---|---|
| mjThreadPool* | pointer | Created thread pool handle |
| mjStackInfo* | pointer | Per-thread stack shard descriptor |
| size_t | integer | Thread count, worker ID, or cache line size |