# PATCH: fix CMake tests for libpthread feature detection under Linux

**URL:** <https://discourse.libsdl.org/t/patch-fix-cmake-tests-for-libpthread-feature-detection-under-linux/19325>\
**Category:** SDL Development\
**Created:** [January 18, 2013, 3:54am UTC](https://discourse.libsdl.org/t/patch-fix-cmake-tests-for-libpthread-feature-detection-under-linux/19325 "2013-01-18T03:54:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Percival](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/scott_percival/32/2386_2.png) [@Scott\_Percival](https://discourse.libsdl.org/u/Scott_Percival)\
**Post date:** [January 18, 2013, 3:54am UTC](https://discourse.libsdl.org/t/patch-fix-cmake-tests-for-libpthread-feature-detection-under-linux/19325/1 "2013-01-18T03:54:12Z")

</div>

No idea how widespread this is, but under Ubuntu 12.04 x86\_64 a  
default SDL build with CMake would suffer deadlocks in  
SDL\_DestroySemaphore, while a build with autotools ran without a  
problem. About 95% of the time it?d happen in SDL\_Init, but failing  
that it?d occur during SDL\_Quit. A typical backtrace from a deadlocked  
start from the latest hg:

#0 \_\_lll\_lock\_wait () at  
…/nptl/sysdeps/unix/sysv/linux/x86\_64/lowlevellock.S:132  
#1 0x00007ffff690c065 in \_L\_lock\_858 () from  
/lib/x86\_64-linux-gnu/libpthread.so.0  
#2 0x00007ffff690beba in \_\_pthread\_mutex\_lock (mutex=0x6f6950) at  
pthread\_mutex\_lock.c:61  
#3 0x00007ffff66cc3a4 in SDL\_mutexP (mutex=0x6f6950) at  
/home/scott/Development/fourier/SDL/src/thread/pthread/SDL\_sysmutex.c:103  
#4 0x00007ffff66cc84f in SDL\_DestroySemaphore (sem=0x6f8860) at  
/home/scott/Development/fourier/SDL/src/thread/generic/SDL\_syssem.c:126  
#5 0x00007ffff6651c8f in SDL\_CreateThread (fn=0x7ffff6651ded  
\<SDL\_TimerThread\>, name=0x7ffff66d1ba4 “SDLTimer”,  
data=0x7ffff68fe4a0)  
at /home/scott/Development/fourier/SDL/src/thread/SDL\_thread.c:286  
#6 0x00007ffff66520da in SDL\_TimerInit () at  
/home/scott/Development/fourier/SDL/src/timer/SDL\_timer.c:230  
#7 0x00007ffff65d35de in SDL\_InitSubSystem (flags=33) at  
/home/scott/Development/fourier/SDL/src/SDL.c:76  
#8 0x00007ffff65d39cf in SDL\_Init (flags=33) at  
/home/scott/Development/fourier/SDL/src/SDL.c:193

A bit of digging revealed that the CMake litmus test for PTHREADS\_SEM  
didn?t have a definition for main(), which made gcc explosively give  
up when linking.

Also the tests for HAVE\_RECURSIVE\_MUTEXES, HAVE\_SEM\_TIMEDWAIT and  
HAVE\_PTHREAD\_SPINLOCK failed because Linux was set to link in  
-lpthread; using the GCCism -pthread works, and seems to be necessary  
for the linker to find methods like pthread\_mutexattr\_settype. I have  
no idea _what_ it does differently, given that a scan with ldd doesn?t  
show any extra libraries linked in other than libpthread, but I?m  
willing to leave that mystery to the GCC high-priests.

So yes, detecting PTHREADS\_SEM fixes the problem, but since the point  
of these tests is to disable unavailable features or cutover to a  
failsafe, I?m concerned that the failsafe for running with PTHREADS  
and without PTHREADS\_SEM doesn?t work that well. Unfortunately I have  
zero insight as to what it might mean; everything I know about  
libpthread fits comfortably onto a piece of fortune cookie paper. But  
this behaviour was very reproducible on two x86\_64 Ubuntu machines,  
and also quite annoying.  
-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: fixcmakepthread.patch  
Type: application/octet-stream  
Size: 888 bytes  
Desc: not available  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130118/a01f72ed/attachment.obj](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130118/a01f72ed/attachment.obj)
