[vlc-commits] commit: Add atomic swap and compare-and-swap ( Rémi Denis-Courmont )
git at videolan.org
git at videolan.org
Wed Oct 13 18:23:38 CEST 2010
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Oct 13 18:57:58 2010 +0300| [59fb10d033cd01b6d5c2379a20982cc0216f9b22] | committer: Rémi Denis-Courmont
Add atomic swap and compare-and-swap
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=59fb10d033cd01b6d5c2379a20982cc0216f9b22
---
include/vlc_atomic.h | 3 +++
src/libvlccore.sym | 2 ++
src/misc/atomic.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
src/win32/atomic.c | 19 +++++++++++++++++++
4 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/include/vlc_atomic.h b/include/vlc_atomic.h
index 51acccc..770e47a 100644
--- a/include/vlc_atomic.h
+++ b/include/vlc_atomic.h
@@ -47,4 +47,7 @@ static inline uintptr_t vlc_atomic_dec (vlc_atomic_t *atom)
return vlc_atomic_sub (atom, 1);
}
+VLC_EXPORT(uintptr_t, vlc_atomic_swap, (vlc_atomic_t *, uintptr_t));
+VLC_EXPORT(uintptr_t, vlc_atomic_compare_swap, (vlc_atomic_t *, uintptr_t, uintptr_t));
+
#endif
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 413ab79..5632223 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -503,6 +503,8 @@ VLC_Compiler
vlc_atomic_get
vlc_atomic_set
vlc_atomic_add
+vlc_atomic_swap
+vlc_atomic_compare_swap
vlc_cond_broadcast
vlc_cond_destroy
vlc_cond_init
diff --git a/src/misc/atomic.c b/src/misc/atomic.c
index 1f97f59..e0319f5 100644
--- a/src/misc/atomic.c
+++ b/src/misc/atomic.c
@@ -46,6 +46,24 @@ uintptr_t vlc_atomic_add (vlc_atomic_t *atom, uintptr_t v)
return __sync_add_and_fetch (&atom->u, v);
}
+uintptr_t vlc_atomic_swap (vlc_atomic_t *atom, uintptr_t v)
+{
+ /* grmbl, gcc does not provide an intrinsic for this! */
+ uintptr_t u;
+
+ do
+ u = vlc_atomic_get (atom);
+ while (vlc_atomic_compare_swap (atom, u, v) != u);
+
+ return u;
+}
+
+uintptr_t vlc_atomic_compare_swap (vlc_atomic_t *atom,
+ uintptr_t oldval, uintptr_t newval)
+{
+ return __sync_val_compare_and_swap (&atom->u, oldval, newval);
+}
+
#else
/* Worst-case fallback implementation with a mutex */
@@ -78,4 +96,30 @@ uintptr_t vlc_atomic_add (vlc_atomic_t *atom, uintptr_t v)
return v;
}
+uintptr_t vlc_atomic_swap (vlc_atomic_t *atom, uintptr_t v)
+{
+ uintptr_t u;
+
+ vlc_mutex_lock (&lock);
+ u = atom->u;
+ atom->u = v;
+ vlc_mutex_unlock (&lock);
+
+ return u;
+}
+
+uintptr_t vlc_atomic_compare_and_swap (vlc_atomic_t *atom,
+ uintptr_t oldval, uintptr_t newval)
+{
+ uintptr_t u;
+
+ vlc_mutex_lock (&lock);
+ u = atom->u;
+ if (u == oldval)
+ atom->u = newval;
+ vlc_mutex_unlock (&lock);
+
+ return u;
+}
+
#endif
diff --git a/src/win32/atomic.c b/src/win32/atomic.c
index abadf89..14687e6 100644
--- a/src/win32/atomic.c
+++ b/src/win32/atomic.c
@@ -50,3 +50,22 @@ uintptr_t vlc_atomic_add (vlc_atomic_t *atom, uintptr_t v)
return InterlockedExchangeAdd (&atom->s, v) + v;
#endif
}
+
+uintptr_t vlc_atomic_swap (vlc_atomic_t *atom, uintptr_t v)
+{
+#if defined (WIN64)
+ return InterlockedExchange64 (&atom->s, v);
+#else
+ return InterlockedExchange (&atom->s, v);
+#endif
+}
+
+uintptr_t vlc_atomic_compare_swap (vlc_atomic_t *atom,
+ uintptr_t oldval, uintptr_t newval)
+{
+#if defined (WIN64)
+ return InterlockedCompareExchange64 (&atom->s, newval, oldval);
+#else
+ return InterlockedCompareExchange (&atom->s, newval, oldval);
+#endif
+}
More information about the vlc-commits
mailing list