Skip to content

CCI: optional thread-safe broker and parameter access#345

Open
markfoodyburton wants to merge 2 commits into
accellera-official:mainfrom
markfoodyburton:mburton/thread_safety
Open

CCI: optional thread-safe broker and parameter access#345
markfoodyburton wants to merge 2 commits into
accellera-official:mainfrom
markfoodyburton:mburton/thread_safety

Conversation

@markfoodyburton

Copy link
Copy Markdown
Contributor

Add compile-time option CCI_ENABLE_THREAD_SAFETY (OFF by default) that enables thread-safe access to the CCI broker, parameters, and value pool. When enabled, defines CCI_THREAD_SAFE as a PUBLIC compile definition so all linking code sees it consistently.

Thread-safety changes (all gated by #ifdef CCI_THREAD_SAFE):

Broker (consuming_broker):

  • Add shared_mutex to consuming_broker. Read operations (get_preset, get_param_handle, get_unconsumed_presets, etc.) take shared_lock for concurrent access. Write operations (set_preset, lock_preset, add/remove_param, register/unregister callbacks) take unique_lock.

Parameter handles (cci_param_untyped):

  • Add recursive_mutex on the param handle list (m_param_handles). Recursive because invalidate_all_param_handles() calls invalidate() which calls remove_param_handle() — re-entrant.

Parameter callbacks (cci_param_typed):

  • Change oncall re-entrancy flag to atomic.
  • Change fast_read/fast_write optimization flags to atomic.
  • Update scoped_true helper to work with atomic.

Value pool (cci_value.cpp):

  • Add mutex to impl_pool free list. Cross-thread alloc/dealloc is possible when cci_value objects are stored in broker maps and accessed from different threads.

Build system:

  • New option: cmake -DCCI_ENABLE_THREAD_SAFETY=ON
  • Try CONFIG mode before Find module for SystemC discovery (fixes -DSystemCLanguage_DIR usage)

Test:

  • New ex20_Thread_Safety example demonstrating concurrent broker access from multiple threads using custom originators. Includes performance benchmarks for new-handle and cached-handle workloads. Only built when CCI_ENABLE_THREAD_SAFETY=ON.

Performance (100K ops, -O3, 4 threads vs single-threaded):

  • New handle per op: ~133% overhead (dominated by handle registration)
  • Cached handle: ~300% overhead (dominated by callback flag contention)
  • Realistic usage (handle cached in logger): negligible

@markfoodyburton
markfoodyburton requested a review from aut0 April 22, 2026 14:10
Thread safety (compile-time option -DCCI_ENABLE_THREAD_SAFETY=ON):

Add shared_mutex to consuming_broker for concurrent read access.
Add recursive_mutex to cci_param_untyped handle list. Add mutex to
cci_value impl_pool. Make oncall/fast_read/fast_write atomic. All
changes gated by CCI_THREAD_SAFE define (PUBLIC, propagated to
linking code). Zero overhead when disabled.

Originator thread awareness:

current_originator_object() now returns NULL on non-SystemC threads.
The SystemC thread ID is captured on first originator construction
(during elaboration). On other threads, sc_get_current_object() is
not called — the originator behaves as if outside the SystemC
hierarchy. Named originators work from any thread; unnamed default
originators still require SystemC context.

cmake: try CONFIG mode before Find module for SystemC discovery.

Test: ex20_Thread_Safety — concurrent broker access from multiple
threads with custom originators and performance benchmarks. Only
built when CCI_ENABLE_THREAD_SAFETY=ON. All 24 existing tests pass.

Signed-off-by: Mark Burton <mburton@qti.qualcomm.com>
When the default cci_originator() constructor is called outside the
SystemC hierarchy (including from a non-SystemC thread), silently create
an originator with the default name (CCI_UNKNOWN_ORIGINATOR_STRING_)
instead of reporting a fatal error. This enables cci_get_broker()
and other CCI APIs that construct default originators to work from
any context without requiring an explicit named originator.

Signed-off-by: Mark Burton <mburton@qti.qualcomm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant