Skip to content

Commit

Permalink
fix: update get_next_session_id function to use atomic increment for …
Browse files Browse the repository at this point in the history
…thread safety

Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
  • Loading branch information
liudf0716 committed Nov 13, 2024
1 parent 886003b commit 9edeb77
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tcpmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,20 @@ void reset_session_id() { g_session_id = 1; }
*
* @return The next session ID.
*/
/**
* @brief Generates the next unique session ID.
*
* This function generates a monotonically increasing session ID by incrementing
* the global session ID counter by 2. This ensures each new session gets a unique
* odd-numbered ID, while even numbers are reserved for other purposes.
*
* @return The newly generated session ID
*
* @note The function increments by 2 to maintain odd-numbered IDs
*/
uint32_t get_next_session_id() {
uint32_t id = g_session_id;
g_session_id += 2;
return id;
uint32_t current_id = __atomic_fetch_add(&g_session_id, 2, __ATOMIC_SEQ_CST);
return current_id;
}

/**
Expand Down

0 comments on commit 9edeb77

Please sign in to comment.