From: Julian Seward Date: Sun, 12 Oct 2003 08:33:30 +0000 (+0000) Subject: Implement pthread_spinlock_* (Tim Potter, tpot@samba.org) X-Git-Tag: svn/VALGRIND_2_1_0~146 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b3e4bdb2a5176fe5e7e83a9d0bbaeca17aed6ff5;p=thirdparty%2Fvalgrind.git Implement pthread_spinlock_* (Tim Potter, tpot@samba.org) MERGE TO STABLE git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1902 --- diff --git a/coregrind/arch/x86-linux/vg_libpthread.c b/coregrind/arch/x86-linux/vg_libpthread.c index 0591fc0f2b..0d8abca887 100644 --- a/coregrind/arch/x86-linux/vg_libpthread.c +++ b/coregrind/arch/x86-linux/vg_libpthread.c @@ -3145,6 +3145,61 @@ weak_alias(_IO_funlockfile, funlockfile); 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 bac177892f..92bed8cd4a 100644 --- a/coregrind/arch/x86-linux/vg_libpthread_unimp.c +++ b/coregrind/arch/x86-linux/vg_libpthread_unimp.c @@ -255,16 +255,16 @@ __attribute__((weak)) void pthread_mutexattr_setkind_np ( void ) // { 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 0591fc0f2b..0d8abca887 100644 --- a/coregrind/vg_libpthread.c +++ b/coregrind/vg_libpthread.c @@ -3145,6 +3145,61 @@ weak_alias(_IO_funlockfile, funlockfile); 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 bac177892f..92bed8cd4a 100644 --- a/coregrind/vg_libpthread_unimp.c +++ b/coregrind/vg_libpthread_unimp.c @@ -255,16 +255,16 @@ __attribute__((weak)) void pthread_mutexattr_setkind_np ( void ) // { 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"); } /*--------------------------------------------------------------------*/