Implement pthread_spinlock_* (Tim Potter, tpot@samba.org)
MERGE TO STABLE
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1902 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/arch/x86-linux/vg_libpthread.c b/coregrind/arch/x86-linux/vg_libpthread.c
index 0591fc0..0d8abca 100644
--- a/coregrind/arch/x86-linux/vg_libpthread.c
+++ b/coregrind/arch/x86-linux/vg_libpthread.c
@@ -3145,6 +3145,61 @@
strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
+/* POSIX spinlocks, taken from glibc linuxthreads/sysdeps/i386 */
+
+typedef volatile int pthread_spinlock_t; /* Huh? Guarded by __USE_XOPEN2K */
+
+int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
+{
+ /* We can ignore the `pshared' parameter. Since we are busy-waiting
+ all processes which can access the memory location `lock' points
+ to can use the spinlock. */
+ *lock = 1;
+ return 0;
+}
+
+int pthread_spin_lock(pthread_spinlock_t *lock)
+{
+ asm volatile
+ ("\n"
+ "1:\n\t"
+ "lock; decl %0\n\t"
+ "js 2f\n\t"
+ ".section .text.spinlock,\"ax\"\n"
+ "2:\n\t"
+ "cmpl $0,%0\n\t"
+ "rep; nop\n\t"
+ "jle 2b\n\t"
+ "jmp 1b\n\t"
+ ".previous"
+ : "=m" (*lock));
+ return 0;
+}
+
+int pthread_spin_unlock(pthread_spinlock_t *lock)
+{
+ asm volatile
+ ("movl $1,%0"
+ : "=m" (*lock));
+ return 0;
+}
+
+int pthread_spin_destroy(pthread_spinlock_t *lock)
+{
+ /* Nothing to do. */
+ return 0;
+}
+
+int pthread_spin_trylock(pthread_spinlock_t *lock)
+{
+ int oldval;
+
+ asm volatile
+ ("xchgl %0,%1"
+ : "=r" (oldval), "=m" (*lock)
+ : "0" (0));
+ return oldval > 0 ? 0 : EBUSY;
+}
/*--------------------------------------------------------------------*/
/*--- end vg_libpthread.c ---*/
diff --git a/coregrind/arch/x86-linux/vg_libpthread_unimp.c b/coregrind/arch/x86-linux/vg_libpthread_unimp.c
index bac1778..92bed8c 100644
--- a/coregrind/arch/x86-linux/vg_libpthread_unimp.c
+++ b/coregrind/arch/x86-linux/vg_libpthread_unimp.c
@@ -255,16 +255,16 @@
// { vgPlain_unimp("pthread_mutexattr_setpshared"); }
//__attribute__((weak)) void pthread_setconcurrency ( void )
// { vgPlain_unimp("pthread_setconcurrency"); }
-__attribute__((weak)) void pthread_spin_destroy ( void )
- { vgPlain_unimp("pthread_spin_destroy"); }
-__attribute__((weak)) void pthread_spin_init ( void )
- { vgPlain_unimp("pthread_spin_init"); }
-__attribute__((weak)) void pthread_spin_lock ( void )
- { vgPlain_unimp("pthread_spin_lock"); }
-__attribute__((weak)) void pthread_spin_trylock ( void )
- { vgPlain_unimp("pthread_spin_trylock"); }
-__attribute__((weak)) void pthread_spin_unlock ( void )
- { vgPlain_unimp("pthread_spin_unlock"); }
+//__attribute__((weak)) void pthread_spin_destroy ( void )
+// { vgPlain_unimp("pthread_spin_destroy"); }
+//__attribute__((weak)) void pthread_spin_init ( void )
+// { vgPlain_unimp("pthread_spin_init"); }
+//__attribute__((weak)) void pthread_spin_lock ( void )
+// { vgPlain_unimp("pthread_spin_lock"); }
+//__attribute__((weak)) void pthread_spin_trylock ( void )
+// { vgPlain_unimp("pthread_spin_trylock"); }
+//__attribute__((weak)) void pthread_spin_unlock ( void )
+// { vgPlain_unimp("pthread_spin_unlock"); }
/*--------------------------------------------------------------------*/
diff --git a/coregrind/vg_libpthread.c b/coregrind/vg_libpthread.c
index 0591fc0..0d8abca 100644
--- a/coregrind/vg_libpthread.c
+++ b/coregrind/vg_libpthread.c
@@ -3145,6 +3145,61 @@
strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
+/* POSIX spinlocks, taken from glibc linuxthreads/sysdeps/i386 */
+
+typedef volatile int pthread_spinlock_t; /* Huh? Guarded by __USE_XOPEN2K */
+
+int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
+{
+ /* We can ignore the `pshared' parameter. Since we are busy-waiting
+ all processes which can access the memory location `lock' points
+ to can use the spinlock. */
+ *lock = 1;
+ return 0;
+}
+
+int pthread_spin_lock(pthread_spinlock_t *lock)
+{
+ asm volatile
+ ("\n"
+ "1:\n\t"
+ "lock; decl %0\n\t"
+ "js 2f\n\t"
+ ".section .text.spinlock,\"ax\"\n"
+ "2:\n\t"
+ "cmpl $0,%0\n\t"
+ "rep; nop\n\t"
+ "jle 2b\n\t"
+ "jmp 1b\n\t"
+ ".previous"
+ : "=m" (*lock));
+ return 0;
+}
+
+int pthread_spin_unlock(pthread_spinlock_t *lock)
+{
+ asm volatile
+ ("movl $1,%0"
+ : "=m" (*lock));
+ return 0;
+}
+
+int pthread_spin_destroy(pthread_spinlock_t *lock)
+{
+ /* Nothing to do. */
+ return 0;
+}
+
+int pthread_spin_trylock(pthread_spinlock_t *lock)
+{
+ int oldval;
+
+ asm volatile
+ ("xchgl %0,%1"
+ : "=r" (oldval), "=m" (*lock)
+ : "0" (0));
+ return oldval > 0 ? 0 : EBUSY;
+}
/*--------------------------------------------------------------------*/
/*--- end vg_libpthread.c ---*/
diff --git a/coregrind/vg_libpthread_unimp.c b/coregrind/vg_libpthread_unimp.c
index bac1778..92bed8c 100644
--- a/coregrind/vg_libpthread_unimp.c
+++ b/coregrind/vg_libpthread_unimp.c
@@ -255,16 +255,16 @@
// { vgPlain_unimp("pthread_mutexattr_setpshared"); }
//__attribute__((weak)) void pthread_setconcurrency ( void )
// { vgPlain_unimp("pthread_setconcurrency"); }
-__attribute__((weak)) void pthread_spin_destroy ( void )
- { vgPlain_unimp("pthread_spin_destroy"); }
-__attribute__((weak)) void pthread_spin_init ( void )
- { vgPlain_unimp("pthread_spin_init"); }
-__attribute__((weak)) void pthread_spin_lock ( void )
- { vgPlain_unimp("pthread_spin_lock"); }
-__attribute__((weak)) void pthread_spin_trylock ( void )
- { vgPlain_unimp("pthread_spin_trylock"); }
-__attribute__((weak)) void pthread_spin_unlock ( void )
- { vgPlain_unimp("pthread_spin_unlock"); }
+//__attribute__((weak)) void pthread_spin_destroy ( void )
+// { vgPlain_unimp("pthread_spin_destroy"); }
+//__attribute__((weak)) void pthread_spin_init ( void )
+// { vgPlain_unimp("pthread_spin_init"); }
+//__attribute__((weak)) void pthread_spin_lock ( void )
+// { vgPlain_unimp("pthread_spin_lock"); }
+//__attribute__((weak)) void pthread_spin_trylock ( void )
+// { vgPlain_unimp("pthread_spin_trylock"); }
+//__attribute__((weak)) void pthread_spin_unlock ( void )
+// { vgPlain_unimp("pthread_spin_unlock"); }
/*--------------------------------------------------------------------*/