Skip to content

Commit 9edeb77

Browse files
committed
fix: update get_next_session_id function to use atomic increment for thread safety
Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
1 parent 886003b commit 9edeb77

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tcpmux.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,20 @@ void reset_session_id() { g_session_id = 1; }
226226
*
227227
* @return The next session ID.
228228
*/
229+
/**
230+
* @brief Generates the next unique session ID.
231+
*
232+
* This function generates a monotonically increasing session ID by incrementing
233+
* the global session ID counter by 2. This ensures each new session gets a unique
234+
* odd-numbered ID, while even numbers are reserved for other purposes.
235+
*
236+
* @return The newly generated session ID
237+
*
238+
* @note The function increments by 2 to maintain odd-numbered IDs
239+
*/
229240
uint32_t get_next_session_id() {
230-
uint32_t id = g_session_id;
231-
g_session_id += 2;
232-
return id;
241+
uint32_t current_id = __atomic_fetch_add(&g_session_id, 2, __ATOMIC_SEQ_CST);
242+
return current_id;
233243
}
234244

235245
/**

0 commit comments

Comments
 (0)