aboutsummaryrefslogtreecommitdiff
path: root/util/lockcnt.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@redhat.com>2020-09-23 11:56:46 +0100
committerStefan Hajnoczi <stefanha@redhat.com>2020-09-23 16:07:44 +0100
commitd73415a315471ac0b127ed3fad45c8ec5d711de1 (patch)
treebae20b3a39968fdfb4340b1a39b533333a8e6fd0 /util/lockcnt.c
parented7db34b5aedba4487fd949b2e545eef954f093e (diff)
qemu/atomic.h: rename atomic_ to qatomic_
clang's C11 atomic_fetch_*() functions only take a C11 atomic type pointer argument. QEMU uses direct types (int, etc) and this causes a compiler error when a QEMU code calls these functions in a source file that also included <stdatomic.h> via a system header file: $ CC=clang CXX=clang++ ./configure ... && make ../util/async.c:79:17: error: address argument to atomic operation must be a pointer to _Atomic type ('unsigned int *' invalid) Avoid using atomic_*() names in QEMU's atomic.h since that namespace is used by <stdatomic.h>. Prefix QEMU's APIs with 'q' so that atomic.h and <stdatomic.h> can co-exist. I checked /usr/include on my machine and searched GitHub for existing "qatomic_" users but there seem to be none. This patch was generated using: $ git grep -h -o '\<atomic\(64\)\?_[a-z0-9_]\+' include/qemu/atomic.h | \ sort -u >/tmp/changed_identifiers $ for identifier in $(</tmp/changed_identifiers); do sed -i "s%\<$identifier\>%q$identifier%g" \ $(git grep -I -l "\<$identifier\>") done I manually fixed line-wrap issues and misaligned rST tables. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Acked-by: Paolo Bonzini <pbonzini@redhat.com> Message-Id: <20200923105646.47864-1-stefanha@redhat.com>
Diffstat (limited to 'util/lockcnt.c')
-rw-r--r--util/lockcnt.c52
1 files changed, 27 insertions, 25 deletions
diff --git a/util/lockcnt.c b/util/lockcnt.c
index 4f88dcf8b8..5da36946b1 100644
--- a/util/lockcnt.c
+++ b/util/lockcnt.c
@@ -61,7 +61,7 @@ static bool qemu_lockcnt_cmpxchg_or_wait(QemuLockCnt *lockcnt, int *val,
int expected = *val;
trace_lockcnt_fast_path_attempt(lockcnt, expected, new_if_free);
- *val = atomic_cmpxchg(&lockcnt->count, expected, new_if_free);
+ *val = qatomic_cmpxchg(&lockcnt->count, expected, new_if_free);
if (*val == expected) {
trace_lockcnt_fast_path_success(lockcnt, expected, new_if_free);
*val = new_if_free;
@@ -81,7 +81,7 @@ static bool qemu_lockcnt_cmpxchg_or_wait(QemuLockCnt *lockcnt, int *val,
int new = expected - QEMU_LOCKCNT_STATE_LOCKED + QEMU_LOCKCNT_STATE_WAITING;
trace_lockcnt_futex_wait_prepare(lockcnt, expected, new);
- *val = atomic_cmpxchg(&lockcnt->count, expected, new);
+ *val = qatomic_cmpxchg(&lockcnt->count, expected, new);
if (*val == expected) {
*val = new;
}
@@ -92,7 +92,7 @@ static bool qemu_lockcnt_cmpxchg_or_wait(QemuLockCnt *lockcnt, int *val,
*waited = true;
trace_lockcnt_futex_wait(lockcnt, *val);
qemu_futex_wait(&lockcnt->count, *val);
- *val = atomic_read(&lockcnt->count);
+ *val = qatomic_read(&lockcnt->count);
trace_lockcnt_futex_wait_resume(lockcnt, *val);
continue;
}
@@ -110,13 +110,14 @@ static void lockcnt_wake(QemuLockCnt *lockcnt)
void qemu_lockcnt_inc(QemuLockCnt *lockcnt)
{
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
bool waited = false;
for (;;) {
if (val >= QEMU_LOCKCNT_COUNT_STEP) {
int expected = val;
- val = atomic_cmpxchg(&lockcnt->count, val, val + QEMU_LOCKCNT_COUNT_STEP);
+ val = qatomic_cmpxchg(&lockcnt->count, val,
+ val + QEMU_LOCKCNT_COUNT_STEP);
if (val == expected) {
break;
}
@@ -142,7 +143,7 @@ void qemu_lockcnt_inc(QemuLockCnt *lockcnt)
void qemu_lockcnt_dec(QemuLockCnt *lockcnt)
{
- atomic_sub(&lockcnt->count, QEMU_LOCKCNT_COUNT_STEP);
+ qatomic_sub(&lockcnt->count, QEMU_LOCKCNT_COUNT_STEP);
}
/* Decrement a counter, and return locked if it is decremented to zero.
@@ -151,14 +152,15 @@ void qemu_lockcnt_dec(QemuLockCnt *lockcnt)
*/
bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt)
{
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
int locked_state = QEMU_LOCKCNT_STATE_LOCKED;
bool waited = false;
for (;;) {
if (val >= 2 * QEMU_LOCKCNT_COUNT_STEP) {
int expected = val;
- val = atomic_cmpxchg(&lockcnt->count, val, val - QEMU_LOCKCNT_COUNT_STEP);
+ val = qatomic_cmpxchg(&lockcnt->count, val,
+ val - QEMU_LOCKCNT_COUNT_STEP);
if (val == expected) {
break;
}
@@ -199,7 +201,7 @@ bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt)
*/
bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt)
{
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
int locked_state = QEMU_LOCKCNT_STATE_LOCKED;
bool waited = false;
@@ -233,7 +235,7 @@ bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt)
void qemu_lockcnt_lock(QemuLockCnt *lockcnt)
{
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
int step = QEMU_LOCKCNT_STATE_LOCKED;
bool waited = false;
@@ -255,12 +257,12 @@ void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt)
{
int expected, new, val;
- val = atomic_read(&lockcnt->count);
+ val = qatomic_read(&lockcnt->count);
do {
expected = val;
new = (val + QEMU_LOCKCNT_COUNT_STEP) & ~QEMU_LOCKCNT_STATE_MASK;
trace_lockcnt_unlock_attempt(lockcnt, val, new);
- val = atomic_cmpxchg(&lockcnt->count, val, new);
+ val = qatomic_cmpxchg(&lockcnt->count, val, new);
} while (val != expected);
trace_lockcnt_unlock_success(lockcnt, val, new);
@@ -273,12 +275,12 @@ void qemu_lockcnt_unlock(QemuLockCnt *lockcnt)
{
int expected, new, val;
- val = atomic_read(&lockcnt->count);
+ val = qatomic_read(&lockcnt->count);
do {
expected = val;
new = val & ~QEMU_LOCKCNT_STATE_MASK;
trace_lockcnt_unlock_attempt(lockcnt, val, new);
- val = atomic_cmpxchg(&lockcnt->count, val, new);
+ val = qatomic_cmpxchg(&lockcnt->count, val, new);
} while (val != expected);
trace_lockcnt_unlock_success(lockcnt, val, new);
@@ -289,7 +291,7 @@ void qemu_lockcnt_unlock(QemuLockCnt *lockcnt)
unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt)
{
- return atomic_read(&lockcnt->count) >> QEMU_LOCKCNT_COUNT_SHIFT;
+ return qatomic_read(&lockcnt->count) >> QEMU_LOCKCNT_COUNT_SHIFT;
}
#else
void qemu_lockcnt_init(QemuLockCnt *lockcnt)
@@ -307,13 +309,13 @@ void qemu_lockcnt_inc(QemuLockCnt *lockcnt)
{
int old;
for (;;) {
- old = atomic_read(&lockcnt->count);
+ old = qatomic_read(&lockcnt->count);
if (old == 0) {
qemu_lockcnt_lock(lockcnt);
qemu_lockcnt_inc_and_unlock(lockcnt);
return;
} else {
- if (atomic_cmpxchg(&lockcnt->count, old, old + 1) == old) {
+ if (qatomic_cmpxchg(&lockcnt->count, old, old + 1) == old) {
return;
}
}
@@ -322,7 +324,7 @@ void qemu_lockcnt_inc(QemuLockCnt *lockcnt)
void qemu_lockcnt_dec(QemuLockCnt *lockcnt)
{
- atomic_dec(&lockcnt->count);
+ qatomic_dec(&lockcnt->count);
}
/* Decrement a counter, and return locked if it is decremented to zero.
@@ -331,9 +333,9 @@ void qemu_lockcnt_dec(QemuLockCnt *lockcnt)
*/
bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt)
{
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
while (val > 1) {
- int old = atomic_cmpxchg(&lockcnt->count, val, val - 1);
+ int old = qatomic_cmpxchg(&lockcnt->count, val, val - 1);
if (old != val) {
val = old;
continue;
@@ -343,7 +345,7 @@ bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt)
}
qemu_lockcnt_lock(lockcnt);
- if (atomic_fetch_dec(&lockcnt->count) == 1) {
+ if (qatomic_fetch_dec(&lockcnt->count) == 1) {
return true;
}
@@ -360,13 +362,13 @@ bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt)
bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt)
{
/* No need for acquire semantics if we return false. */
- int val = atomic_read(&lockcnt->count);
+ int val = qatomic_read(&lockcnt->count);
if (val > 1) {
return false;
}
qemu_lockcnt_lock(lockcnt);
- if (atomic_fetch_dec(&lockcnt->count) == 1) {
+ if (qatomic_fetch_dec(&lockcnt->count) == 1) {
return true;
}
@@ -381,7 +383,7 @@ void qemu_lockcnt_lock(QemuLockCnt *lockcnt)
void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt)
{
- atomic_inc(&lockcnt->count);
+ qatomic_inc(&lockcnt->count);
qemu_mutex_unlock(&lockcnt->mutex);
}
@@ -392,6 +394,6 @@ void qemu_lockcnt_unlock(QemuLockCnt *lockcnt)
unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt)
{
- return atomic_read(&lockcnt->count);
+ return qatomic_read(&lockcnt->count);
}
#endif