2424#include "mtl_session_api.h"
2525
2626/* Internal MTL headers */
27- #include "mt_main.h"
28- #include "st2110/st_header.h"
27+ #include "../ mt_main.h"
28+ #include "../ st2110/st_header.h"
2929
3030#include <rte_atomic.h>
3131#include <rte_ring.h>
32- #include <rte_spinlock.h>
3332
3433#if defined(__cplusplus )
3534extern "C" {
@@ -159,12 +158,23 @@ struct mtl_session_impl {
159158 int idx ; /**< Session index (for logging) */
160159 int socket_id ; /**< NUMA socket */
161160
162- /* State */
161+ /*
162+ * Session state — accessed with C11 __atomic builtins.
163+ * No lock needed; state transitions pair with the stopped flag.
164+ */
163165 mtl_session_state_t state ;
164- rte_spinlock_t state_lock ;
165166
166- /* Set by stop(), checked by buffer_get/event_poll to return -EAGAIN */
167- volatile bool stopped ;
167+ /**
168+ * Atomic stopped flag — the primary cross-thread signal.
169+ * Set by stop(), checked by buffer_get/event_poll to return -EAGAIN.
170+ *
171+ * Memory ordering rationale:
172+ * store (__ATOMIC_RELEASE): all prior stores (state, data) are visible
173+ * before stopped is observed by other threads.
174+ * load (__ATOMIC_ACQUIRE): subsequent reads in the checking thread see
175+ * all stores that happened before the set.
176+ */
177+ int stopped ;
168178
169179 /* Configuration (copied from create) */
170180 char name [ST_MAX_NAME_LEN ];
@@ -194,6 +204,13 @@ struct mtl_session_impl {
194204 * Frame buffer management.
195205 * For library-owned mode, we manage mtl_buffer_impl wrappers.
196206 * The actual frame memory is in inner->st20_frames (st_frame_trans array).
207+ *
208+ * Thread safety: completely lock-free.
209+ * - TX: atomic CAS on per-frame state (enum tx_frame_state) provides
210+ * mutual exclusion — only the CAS winner owns a given frame.
211+ * - RX: multi-consumer rte_ring ensures safe concurrent dequeue.
212+ * Buffer wrapper assignment is race-free because each frame_idx maps
213+ * 1:1 to a unique buffer_impl slot (buffer_count >= frame_count).
197214 */
198215 uint32_t buffer_count ;
199216 struct mtl_buffer_impl * buffers ; /**< Buffer wrapper pool */
@@ -202,9 +219,14 @@ struct mtl_session_impl {
202219 struct rte_ring * event_ring ; /**< Pending events */
203220 int event_fd ; /**< For epoll integration */
204221
205- /* Statistics - aggregated view of inner session stats */
222+ /*
223+ * Statistics — aggregated view of inner session stats.
224+ * Thread safety: individual counter fields are accessed with __atomic builtins.
225+ * Increments use __ATOMIC_RELAXED (no ordering needed for counters).
226+ * Reads/resets also use __ATOMIC_RELAXED (approximate snapshot is fine).
227+ * No lock needed — each field is independently atomic.
228+ */
206229 mtl_session_stats_t stats ;
207- rte_spinlock_t stats_lock ;
208230
209231 /* Callbacks (optional, for low-latency notification) */
210232 int (* notify_buffer_ready )(void * priv );
@@ -353,27 +375,31 @@ void mtl_session_put_frame_trans(struct st_frame_trans* ft);
353375 *************************************************************************/
354376
355377/**
356- * Check if session is stopped.
378+ * Check if session is stopped (thread-safe, acquire semantics) .
357379 * Call this at the start of any blocking operation.
358380 */
359381static inline bool mtl_session_check_stopped (struct mtl_session_impl * s ) {
360- return s -> stopped ;
382+ return __atomic_load_n ( & s -> stopped , __ATOMIC_ACQUIRE ) != 0 ;
361383}
362384
363385/**
364- * Set stopped flag. Called by mtl_session_stop().
386+ * Set stopped flag (thread-safe). Called by mtl_session_stop().
387+ * Uses release semantics so all prior stores are visible.
365388 */
366389static inline void mtl_session_set_stopped (struct mtl_session_impl * s ) {
367- s -> stopped = true;
368- s -> state = MTL_SESSION_STATE_STOPPED ;
390+ /* Store state first (relaxed), then stopped with release.
391+ * The release on stopped ensures that the state store is visible
392+ * to any thread that observes stopped == 1 via acquire load. */
393+ __atomic_store_n (& s -> state , MTL_SESSION_STATE_STOPPED , __ATOMIC_RELAXED );
394+ __atomic_store_n (& s -> stopped , 1 , __ATOMIC_RELEASE );
369395}
370396
371397/**
372- * Clear stopped flag. Called by mtl_session_start().
398+ * Clear stopped flag (thread-safe) . Called by mtl_session_start().
373399 */
374400static inline void mtl_session_clear_stopped (struct mtl_session_impl * s ) {
375- s -> stopped = false ;
376- s -> state = MTL_SESSION_STATE_STARTED ;
401+ __atomic_store_n ( & s -> state , MTL_SESSION_STATE_STARTED , __ATOMIC_RELAXED ) ;
402+ __atomic_store_n ( & s -> stopped , 0 , __ATOMIC_RELEASE ) ;
377403}
378404
379405/*************************************************************************
0 commit comments