-
Notifications
You must be signed in to change notification settings - Fork 470
UCS: Introduce lightweight rwlock #10355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
#include <ucs/sys/compiler_def.h> | ||
#include <stddef.h> | ||
#include <sched.h> | ||
|
||
BEGIN_C_DECLS | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2025. ALL RIGHTS RESERVED. | ||
* | ||
* See file LICENSE for terms. | ||
*/ | ||
|
||
#ifndef UCS_RWLOCK_H | ||
#define UCS_RWLOCK_H | ||
|
||
#include <ucs/arch/cpu.h> | ||
#include <ucs/debug/assert.h> | ||
#include <ucs/sys/compiler_def.h> | ||
|
||
/** | ||
* The ucs_rw_spinlock_t type. | ||
* | ||
* Readers increment the counter by UCS_RWLOCK_READ (4) | ||
* Writers set the UCS_RWLOCK_WRITE bit when lock is held | ||
* and set the UCS_RWLOCK_WAIT bit while waiting. | ||
tvegas1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* UCS_RWLOCK_WAIT bit is meant for all subsequent reader | ||
* to let any writer go first to avoid write starvation. | ||
* | ||
* 31 2 1 0 | ||
* +-------------------+-+-+ | ||
* | readers | | | | ||
* +-------------------+-+-+ | ||
* ^ ^ | ||
* | | | ||
* WRITE: lock held ----/ | | ||
* WAIT: writer pending --/ | ||
*/ | ||
|
||
#define UCS_RWLOCK_WAIT UCS_BIT(0) /* Writer is waiting */ | ||
#define UCS_RWLOCK_WRITE UCS_BIT(1) /* Writer has the lock */ | ||
#define UCS_RWLOCK_MASK (UCS_RWLOCK_WAIT | UCS_RWLOCK_WRITE) | ||
#define UCS_RWLOCK_READ UCS_BIT(2) /* Reader increment */ | ||
|
||
#define UCS_RWLOCK_STATIC_INITIALIZER {0} | ||
|
||
|
||
#define ucs_rw_spinlock_assert(_lock, _cond, _desc) \ | ||
ucs_assertv((_lock)->state _cond, "lock=%p " _desc " state=0x%x%s%s", \ | ||
(_lock), (_lock)->state, \ | ||
(_lock)->state & UCS_RWLOCK_WAIT ? " WAIT" : "", \ | ||
(_lock)->state & UCS_RWLOCK_WRITE ? " WRITE" : "") | ||
|
||
|
||
/** | ||
* Reader-writer spin lock. | ||
*/ | ||
typedef struct { | ||
uint32_t state; | ||
yosefe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} ucs_rw_spinlock_t; | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void | ||
ucs_rw_spinlock_read_lock(ucs_rw_spinlock_t *lock) | ||
{ | ||
uint32_t x; | ||
|
||
for (;;) { | ||
while (__atomic_load_n(&lock->state, __ATOMIC_RELAXED) & | ||
UCS_RWLOCK_MASK) { | ||
ucs_cpu_relax(); | ||
} | ||
|
||
x = __atomic_fetch_add(&lock->state, UCS_RWLOCK_READ, __ATOMIC_ACQUIRE); | ||
if (!(x & UCS_RWLOCK_MASK)) { | ||
return; | ||
} | ||
|
||
__atomic_fetch_sub(&lock->state, UCS_RWLOCK_READ, __ATOMIC_RELAXED); | ||
} | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void | ||
ucs_rw_spinlock_read_unlock(ucs_rw_spinlock_t *lock) | ||
{ | ||
ucs_rw_spinlock_assert(lock, >= UCS_RWLOCK_READ, "read underrun"); | ||
__atomic_fetch_sub(&lock->state, UCS_RWLOCK_READ, __ATOMIC_RELEASE); | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void | ||
ucs_rw_spinlock_write_lock(ucs_rw_spinlock_t *lock) | ||
{ | ||
uint32_t x; | ||
|
||
x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED); | ||
if (ucs_unlikely(x > UCS_RWLOCK_WAIT)) { | ||
goto wait; | ||
} | ||
|
||
for (;;) { | ||
if (__atomic_compare_exchange_n(&lock->state, &x, UCS_RWLOCK_WRITE, 0, | ||
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { | ||
return; | ||
} | ||
|
||
wait: | ||
yosefe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ucs_likely(!(x & UCS_RWLOCK_WAIT))) { | ||
__atomic_fetch_or(&lock->state, UCS_RWLOCK_WAIT, __ATOMIC_RELAXED); | ||
} | ||
|
||
while ((x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED)) > | ||
UCS_RWLOCK_WAIT) { | ||
ucs_cpu_relax(); | ||
tvegas1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE int | ||
ucs_rw_spinlock_write_trylock(ucs_rw_spinlock_t *lock) | ||
{ | ||
uint32_t x; | ||
|
||
x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED); | ||
if ((x < UCS_RWLOCK_WRITE) && | ||
(__atomic_compare_exchange_n(&lock->state, &x, x | UCS_RWLOCK_WRITE, 1, | ||
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED))) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void | ||
ucs_rw_spinlock_write_unlock(ucs_rw_spinlock_t *lock) | ||
{ | ||
ucs_rw_spinlock_assert(lock, >= UCS_RWLOCK_WRITE, "write underrun"); | ||
__atomic_fetch_sub(&lock->state, UCS_RWLOCK_WRITE, __ATOMIC_RELEASE); | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void ucs_rw_spinlock_init(ucs_rw_spinlock_t *lock) | ||
{ | ||
lock->state = 0; | ||
} | ||
|
||
|
||
static UCS_F_ALWAYS_INLINE void ucs_rw_spinlock_cleanup(ucs_rw_spinlock_t *lock) | ||
{ | ||
ucs_rw_spinlock_assert(lock, == 0, "not released"); | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: do something else if SSE2 not defined, like "":::"memory" ?