From: David Vrabel <david.vrabel@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: David Vrabel <david.vrabel@citrix.com>,
Jan Beulich <jbeulich@suse.com>,
Ian Campbell <ian.campbell@citrix.com>
Subject: [PATCHv1 1/4] atomic: replace atomic_compareandswap() with atomic_cmpxchg()
Date: Fri, 18 Dec 2015 14:09:03 +0000 [thread overview]
Message-ID: <1450447746-9305-2-git-send-email-david.vrabel@citrix.com> (raw)
In-Reply-To: <1450447746-9305-1-git-send-email-david.vrabel@citrix.com>
atomic_compareandswap() used atomic_t as the new, old and returned
values which is less convinient than using just int.
Signed-off-by: David Vrabel <david.vrabel@citrix.com>
---
xen/common/domain.c | 5 +----
xen/include/asm-arm/atomic.h | 8 --------
xen/include/asm-x86/atomic.h | 8 --------
xen/include/xen/atomic.h | 43 +++++++++++++++++++++++++++++++++++++++++++
xen/include/xen/sched.h | 11 +++++------
5 files changed, 49 insertions(+), 26 deletions(-)
create mode 100644 xen/include/xen/atomic.h
diff --git a/xen/common/domain.c b/xen/common/domain.c
index c4661d8..3960f06 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -851,14 +851,11 @@ static void complete_domain_destroy(struct rcu_head *head)
void domain_destroy(struct domain *d)
{
struct domain **pd;
- atomic_t old = ATOMIC_INIT(0);
- atomic_t new = ATOMIC_INIT(DOMAIN_DESTROYED);
BUG_ON(!d->is_dying);
/* May be already destroyed, or get_domain() can race us. */
- old = atomic_compareandswap(old, new, &d->refcnt);
- if ( _atomic_read(old) != 0 )
+ if ( atomic_cmpxchg(&d->refcnt, 0, DOMAIN_DESTROYED) != 0 )
return;
cpupool_rm_domain(d);
diff --git a/xen/include/asm-arm/atomic.h b/xen/include/asm-arm/atomic.h
index 5a38c67..29ab265 100644
--- a/xen/include/asm-arm/atomic.h
+++ b/xen/include/asm-arm/atomic.h
@@ -138,14 +138,6 @@ static inline void _atomic_set(atomic_t *v, int i)
# error "unknown ARM variant"
#endif
-static inline atomic_t atomic_compareandswap(
- atomic_t old, atomic_t new, atomic_t *v)
-{
- atomic_t rc;
- rc.counter = __cmpxchg(&v->counter, old.counter, new.counter, sizeof(int));
- return rc;
-}
-
#endif /* __ARCH_ARM_ATOMIC__ */
/*
* Local variables:
diff --git a/xen/include/asm-x86/atomic.h b/xen/include/asm-x86/atomic.h
index 2b8c877..a40319e 100644
--- a/xen/include/asm-x86/atomic.h
+++ b/xen/include/asm-x86/atomic.h
@@ -272,12 +272,4 @@ static inline int atomic_add_negative(int i, atomic_t *v)
return c;
}
-static inline atomic_t atomic_compareandswap(
- atomic_t old, atomic_t new, atomic_t *v)
-{
- atomic_t rc;
- rc.counter = __cmpxchg(&v->counter, old.counter, new.counter, sizeof(int));
- return rc;
-}
-
#endif /* __ARCH_X86_ATOMIC__ */
diff --git a/xen/include/xen/atomic.h b/xen/include/xen/atomic.h
new file mode 100644
index 0000000..387ac04
--- /dev/null
+++ b/xen/include/xen/atomic.h
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * atomic.h
+ *
+ * Generic atomic variables.
+ *
+ * Copyright (c) 2015 Citrix R&D Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ATOMIC_H__
+#define __ATOMIC_H__
+
+#include <asm/atomic.h>
+
+static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
+{
+ return cmpxchg(&v->counter, old, new);
+}
+
+/**
+ * atomic_add_return - add integer and return
+ * @i: integer value to add
+ * @v: pointer of type atomic_t
+ *
+ * Atomically adds @i to @v and returns @i + @v
+ */
+static inline int atomic_add_return(int i, atomic_t *v)
+{
+ return i + arch_fetch_and_add(&v->counter, i);
+}
+
+#endif /* __ATOMIC_H__ */
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index 3729b0f..cca5547 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -17,7 +17,7 @@
#include <xen/mm.h>
#include <xen/smp.h>
#include <xen/perfc.h>
-#include <asm/atomic.h>
+#include <xen/atomic.h>
#include <xen/wait.h>
#include <public/xen.h>
#include <public/domctl.h>
@@ -481,16 +481,15 @@ extern struct vcpu *idle_vcpu[NR_CPUS];
*/
static always_inline int get_domain(struct domain *d)
{
- atomic_t old, new, seen = d->refcnt;
+ int old, seen = atomic_read(&d->refcnt);
do
{
old = seen;
- if ( unlikely(_atomic_read(old) & DOMAIN_DESTROYED) )
+ if ( unlikely(old & DOMAIN_DESTROYED) )
return 0;
- _atomic_set(&new, _atomic_read(old) + 1);
- seen = atomic_compareandswap(old, new, &d->refcnt);
+ seen = atomic_cmpxchg(&d->refcnt, old, old + 1);
}
- while ( unlikely(_atomic_read(seen) != _atomic_read(old)) );
+ while ( unlikely(seen != old) );
return 1;
}
--
2.1.4
next prev parent reply other threads:[~2015-12-18 14:09 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-18 14:09 [PATCHv1 0/4] spinlock: add qrwlocks David Vrabel
2015-12-18 14:09 ` David Vrabel [this message]
2015-12-18 17:01 ` [PATCHv1 1/4] atomic: replace atomic_compareandswap() with atomic_cmpxchg() Jan Beulich
2016-01-21 15:19 ` Ian Campbell
2015-12-18 14:09 ` [PATCHv1 2/4] x86/domain: Compile with lock_profile=y enabled David Vrabel
2015-12-18 14:09 ` [PATCHv1 3/4] spinlock: shrink struct lock_debug David Vrabel
2015-12-18 16:58 ` Jan Beulich
2016-01-19 12:31 ` Jennifer Herbert
2016-01-19 13:04 ` Jan Beulich
2015-12-18 14:09 ` [PATCHv1 4/4] spinlock: add fair read-write locks David Vrabel
2015-12-22 13:54 ` Jan Beulich
2016-01-18 16:44 ` Jennifer Herbert
2015-12-18 15:47 ` [PATCHv1 0/4] spinlock: add qrwlocks Ian Campbell
2015-12-18 15:49 ` David Vrabel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1450447746-9305-2-git-send-email-david.vrabel@citrix.com \
--to=david.vrabel@citrix.com \
--cc=ian.campbell@citrix.com \
--cc=jbeulich@suse.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.