Fixed bug 2618 - incomplete pthread-based lock support should be removed
authorSam Lantinga <slouken@libsdl.org>
Mon, 07 Jul 2014 21:27:42 -0700
changeset 899113b0df1793e8
parent 8990 7e3f54f2b16f
child 8992 6d5b8a8752ce
Fixed bug 2618 - incomplete pthread-based lock support should be removed

binarycrusader

Since changeset 358696c354a8, SDL 2.0 has been broken on Solaris when compiling with the Solaris Studio compiler (which uses the pthread implementation of SDL_AtomicLock).

Notably, it gets stuck at the MemoryBarrierRelease in SDL_GetErrBuf:

6585 # 218
6586 if (!tls_errbuf && !tls_being_created) {
6587 SDL_AtomicLock_REAL ( & tls_lock );
6588 if (!tls_errbuf) {
6589 SDL_TLSID slot;
6590 tls_being_created = SDL_TRUE;
6591 slot = SDL_TLSCreate_REAL ( );
6592 tls_being_created = SDL_FALSE;
6593 { SDL_SpinLock _tmp = 0 ; SDL_AtomicLock_REAL ( & _tmp ) ; SDL_AtomicUnlock_REAL ( & _tmp ) ; };
^^^ loops forever above
6594 tls_errbuf = slot;
6595 }
6596 SDL_AtomicUnlock_REAL ( & tls_lock );
6597 }


Running: testthread
(process id 28926)
^Cdbx: warning: Interrupt ignored but forwarded to child.
signal INT (Interrupt) in __nanosleep at 0xfe52a875
0xfe52a875: __nanosleep+0x0015: jae __nanosleep+0x23 [ 0xfe52a883, .+0xe ]
Current function is SDL_Delay_REAL
204 was_error = nanosleep(&tv, &elapsed);
(dbx) where
[1] __nanosleep(0xfeffe848, 0xfeffe850, 0xfe75a5ac, 0xfe5169d8), at 0xfe52a875
[2] nanosleep(0xfeffe848, 0xfeffe850), at 0xfe516a3b
=>[3] SDL_Delay_REAL(ms = 0), line 204 in "SDL_systimer.c"
[4] SDL_AtomicLock_REAL(lock = 0xfeffe88c), line 104 in "SDL_spinlock.c"
[5] SDL_GetErrBuf(), line 225 in "SDL_thread.c"
[6] SDL_ClearError_REAL(), line 216 in "SDL_error.c"
[7] SDL_InitSubSystem_REAL(flags = 0), line 116 in "SDL.c"
[8] SDL_Init_REAL(flags = 0), line 244 in "SDL.c"
[9] SDL_Init(a = 0), line 89 in "SDL_dynapi_procs.h"
[10] main(argc = 1, argv = 0xfeffe948), line 65 in "testthread.c"

As far as I can tell, this is because pthread_spin_trylock() always returns EBUSY for this particular lock; since it works in other places, I'm suspicious.

Different Solaris Studio compiler versions seem to make no difference.

I've verified this is broken on Linux as well if SDL_spinlock.c is modified to use the pthread implementation.

This appears to be because pthread_spin_init() and pthread_spin_destroy() are not used with the locks as required.
cmake/sdlchecks.cmake
configure.in
include/SDL_config.h.cmake
include/SDL_config.h.in
premake/Linux/SDL_config_premake.h
premake/config/SDL_config_cygwin.template.h
premake/config/SDL_config_linux.template.h
src/atomic/SDL_spinlock.c
     1.1 --- a/cmake/sdlchecks.cmake	Mon Jul 07 21:21:05 2014 -0700
     1.2 +++ b/cmake/sdlchecks.cmake	Mon Jul 07 21:27:42 2014 -0700
     1.3 @@ -755,13 +755,6 @@
     1.4  
     1.5        check_c_source_compiles("
     1.6            #include <pthread.h>
     1.7 -          int main(int argc, char** argv) {
     1.8 -              pthread_spin_trylock(NULL);
     1.9 -              return 0;
    1.10 -          }" HAVE_PTHREAD_SPINLOCK)
    1.11 -
    1.12 -      check_c_source_compiles("
    1.13 -          #include <pthread.h>
    1.14            #include <pthread_np.h>
    1.15            int main(int argc, char** argv) { return 0; }" HAVE_PTHREAD_NP_H)
    1.16        check_function_exists(pthread_setname_np HAVE_PTHREAD_setNAME_NP)
     2.1 --- a/configure.in	Mon Jul 07 21:21:05 2014 -0700
     2.2 +++ b/configure.in	Mon Jul 07 21:27:42 2014 -0700
     2.3 @@ -2384,15 +2384,6 @@
     2.4                  AC_MSG_RESULT($have_sem_timedwait)
     2.5              fi
     2.6  
     2.7 -            AC_MSG_CHECKING(for pthread_spin_trylock)
     2.8 -            AC_TRY_LINK_FUNC(pthread_spin_trylock, [
     2.9 -              has_pthread_spin_trylock=yes
    2.10 -              AC_DEFINE(HAVE_PTHREAD_SPINLOCK, 1, [ ])
    2.11 -            ],[
    2.12 -              has_pthread_spin_trylock=no
    2.13 -            ])
    2.14 -            AC_MSG_RESULT($has_pthread_spin_trylock)
    2.15 -
    2.16              AC_CHECK_HEADER(pthread_np.h, have_pthread_np_h=yes, have_pthread_np_h=no, [ #include <pthread.h> ])
    2.17              if test x$have_pthread_np_h = xyes; then
    2.18                  AC_DEFINE(HAVE_PTHREAD_NP_H, 1, [ ])
     3.1 --- a/include/SDL_config.h.cmake	Mon Jul 07 21:21:05 2014 -0700
     3.2 +++ b/include/SDL_config.h.cmake	Mon Jul 07 21:27:42 2014 -0700
     3.3 @@ -46,7 +46,6 @@
     3.4  
     3.5  #cmakedefine HAVE_GCC_ATOMICS @HAVE_GCC_ATOMICS@
     3.6  #cmakedefine HAVE_GCC_SYNC_LOCK_TEST_AND_SET @HAVE_GCC_SYNC_LOCK_TEST_AND_SET@
     3.7 -#cmakedefine HAVE_PTHREAD_SPINLOCK @HAVE_PTHREAD_SPINLOCK@
     3.8  
     3.9  #cmakedefine HAVE_DXGI_H @HAVE_DXGI_H@
    3.10  
     4.1 --- a/include/SDL_config.h.in	Mon Jul 07 21:21:05 2014 -0700
     4.2 +++ b/include/SDL_config.h.in	Mon Jul 07 21:27:42 2014 -0700
     4.3 @@ -49,7 +49,6 @@
     4.4  #endif
     4.5  #undef HAVE_GCC_ATOMICS
     4.6  #undef HAVE_GCC_SYNC_LOCK_TEST_AND_SET
     4.7 -#undef HAVE_PTHREAD_SPINLOCK
     4.8  
     4.9  #undef HAVE_DDRAW_H
    4.10  #undef HAVE_DINPUT_H
     5.1 --- a/premake/Linux/SDL_config_premake.h	Mon Jul 07 21:21:05 2014 -0700
     5.2 +++ b/premake/Linux/SDL_config_premake.h	Mon Jul 07 21:27:42 2014 -0700
     5.3 @@ -50,7 +50,6 @@
     5.4  #endif
     5.5  #define HAVE_GCC_ATOMICS 1
     5.6  /* #undef HAVE_GCC_SYNC_LOCK_TEST_AND_SET */
     5.7 -#define HAVE_PTHREAD_SPINLOCK 1
     5.8  
     5.9  /* Comment this if you want to build without any C library requirements */
    5.10  #define HAVE_LIBC 1
     6.1 --- a/premake/config/SDL_config_cygwin.template.h	Mon Jul 07 21:21:05 2014 -0700
     6.2 +++ b/premake/config/SDL_config_cygwin.template.h	Mon Jul 07 21:27:42 2014 -0700
     6.3 @@ -46,7 +46,6 @@
     6.4  #define SIZEOF_VOIDP 4
     6.5  #define HAVE_GCC_ATOMICS 1
     6.6  /* #undef HAVE_GCC_SYNC_LOCK_TEST_AND_SET */
     6.7 -#define HAVE_PTHREAD_SPINLOCK 1
     6.8  
     6.9  /* Comment this if you want to build without any C library requirements */
    6.10  #define HAVE_LIBC 0
     7.1 --- a/premake/config/SDL_config_linux.template.h	Mon Jul 07 21:21:05 2014 -0700
     7.2 +++ b/premake/config/SDL_config_linux.template.h	Mon Jul 07 21:27:42 2014 -0700
     7.3 @@ -50,7 +50,6 @@
     7.4  #endif
     7.5  #define HAVE_GCC_ATOMICS 1
     7.6  /* #undef HAVE_GCC_SYNC_LOCK_TEST_AND_SET */
     7.7 -#define HAVE_PTHREAD_SPINLOCK 1
     7.8  
     7.9  /* Comment this if you want to build without any C library requirements */
    7.10  #define HAVE_LIBC 1
     8.1 --- a/src/atomic/SDL_spinlock.c	Mon Jul 07 21:21:05 2014 -0700
     8.2 +++ b/src/atomic/SDL_spinlock.c	Mon Jul 07 21:27:42 2014 -0700
     8.3 @@ -89,10 +89,6 @@
     8.4      /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
     8.5      return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
     8.6  
     8.7 -#elif HAVE_PTHREAD_SPINLOCK
     8.8 -    /* pthread instructions */
     8.9 -    return (pthread_spin_trylock(lock) == 0);
    8.10 -
    8.11  #elif defined(__SOLARIS__) && defined(_LP64)
    8.12      /* Used for Solaris with non-gcc compilers. */
    8.13      return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
    8.14 @@ -126,9 +122,6 @@
    8.15  #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
    8.16      __sync_lock_release(lock);
    8.17  
    8.18 -#elif HAVE_PTHREAD_SPINLOCK
    8.19 -    pthread_spin_unlock(lock);
    8.20 -
    8.21  #elif defined(__SOLARIS__)
    8.22      /* Used for Solaris when not using gcc. */
    8.23      *lock = 0;