[06/10] zdtm/lib: add futex_cmpxchg and futex_cmpxchg_and_wake

Submitted by Pavel Tikhomirov on Feb. 22, 2018, 11:49 a.m.

Details

Message ID 20180222114928.24343-7-ptikhomirov@virtuozzo.com
State Accepted
Series "don't use wrong pagemap (from other task) on pid reuse"
Headers show

Commit Message

Pavel Tikhomirov Feb. 22, 2018, 11:49 a.m.
These are usefull as we want to indicate predump status in the same
futex where we indicate test running/finished state, we don't want
predump state replace finished state so we should check and update the
state in one atomic transaction.

https://jira.sw.ru/browse/PSBM-67502

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 test/zdtm/lib/lock.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Patch hide | download patch | download mbox

diff --git a/test/zdtm/lib/lock.h b/test/zdtm/lib/lock.h
index db9e96d0e..5acaa876c 100644
--- a/test/zdtm/lib/lock.h
+++ b/test/zdtm/lib/lock.h
@@ -41,6 +41,11 @@  static inline void futex_set(futex_t *f, uint32_t v)
 	atomic_set(&f->raw, v);
 }
 
+static inline int futex_cmpxchg(futex_t *f, uint32_t old, uint32_t new)
+{
+	return atomic_cmpxchg(&f->raw, old, new);
+}
+
 /* Set futex @f to @v and wake up all waiters */
 static inline void futex_add_and_wake(futex_t *f, uint32_t v)
 {
@@ -77,6 +82,16 @@  static inline void futex_set_and_wake(futex_t *f, uint32_t v)
 	BUG_ON(sys_futex(&f->raw, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
 }
 
+static inline int futex_cmpxchg_and_wake(futex_t *f, uint32_t old, uint32_t new)
+{
+	uint32_t ret;
+
+	ret = atomic_cmpxchg(&f->raw, old, new);
+	if (ret == old)
+		BUG_ON(sys_futex(&f->raw, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+	return ret;
+}
+
 /* Mark futex @f as wait abort needed and wake up all waiters */
 static inline void futex_abort_and_wake(futex_t *f)
 {