Implementation:Google deepmind Mujoco Engine Global Table
| Knowledge Sources | |
|---|---|
| Domains | Physics Simulation, Plugin System, Data Structures |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
Defines a thread-safe global table data structure for storing extension objects such as plugins, implemented as a linked list of cache-line-aligned array blocks in C++.
Description
This header provides the GlobalTable<T> template class used as global storage for MuJoCo extension objects (primarily plugins). The table is implemented as a linked list of TableBlock arrays (each holding 15 objects), which provides good memory locality for typical use cases while never invalidating pointers when growing. The table uses a ReentrantWriteLock for thread-safe registration with atomic reference counting, and std::atomic_int for the global count with acquire/release memory ordering so that reads do not require locking. Key operations include AppendIfUnique (thread-safe registration with duplicate detection using case-insensitive key comparison), GetAtSlot and GetByKey (lock-free lookup by index or name), and LockExclusively for batch operations. The table blocks are aligned to 256-byte cache line boundaries to minimize false sharing.
Usage
Used internally by the MuJoCo plugin system to maintain a global registry of loaded plugins, resource providers, and other extension objects that persist across model loads.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: src/engine/engine_global_table.h
- Lines: 1-282
Key Functions
// Note: This is a C++ header using templates
// Helper function
static inline bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2);
// TableBlock: cache-line-aligned storage block
template<typename T>
struct alignas(kCacheLineBytes) TableBlock {
static constexpr int kBlockSize = 15;
T objects[kBlockSize];
TableBlock<T>* next;
};
// ReentrantWriteLock: recursive mutex wrapper
class ReentrantWriteLock { ... };
// GlobalTable: thread-safe global registry
template<typename T>
class GlobalTable {
static GlobalTable<T>& GetSingleton();
int count();
int AppendIfUnique(const T& obj);
const T* GetAtSlot(int slot);
const T* GetByKey(std::string_view key, int* slot);
ReentrantWriteLock LockExclusively();
};
Import
#include "engine/engine_global_table.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| obj | const T& | Yes | Extension object to register (must implement ObjectKey, ObjectEqual, CopyObject) |
| slot | int | Yes | Index for slot-based lookup |
| key | std::string_view | Yes | Case-insensitive name for key-based lookup |
Outputs
| Name | Type | Description |
|---|---|---|
| return value (AppendIfUnique) | int | Slot index of registered object, or calls mju_error on failure |
| return value (GetAtSlot/GetByKey) | const T* | Pointer to object, or nullptr if not found |
| count() | int | Current number of registered objects |