More on hang in SDL_Init

Hello SDL developers–

This is further information on my email from last Friday regarding
a hang in SDL_Init. Here is a very simple test program that
reliably hangs:

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <SDL/SDL.h>

int main (int argc, char **argv)
{

XInitThreads();

SDL_Init (SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);

printf("SUCCESS!!!!!\n");

exit(0);

}

I am running completely vanilla Red Hat 8.0 with XFree86 4.2.1 and SDL
1.2.5. The program above is saved as the file “sdlhang.c” and compiled as
follows:

gcc -g -O2 -c sdlhang.c

gcc -g -O2 sdlhang.o -o sdlhang -rdynamic
/usr/local/vbrick/lib/libSDL-1.2.so.0 -lpthread -L/usr/X11R6/lib -lXext -lX1
1 -lm -lz

After compiling sdlhang, I run the following shell script:

#!/bin/sh

iter=0

while /bin/true ; do
./sdlhang

iter=`echo $iter + 1 | bc -q`
echo "Completed iteration $iter"

done

After some number of iterations – sometimes on the first iteration,
sometimes up to 2000 iterations – the test program will hang with the
following backtrace:

(gdb) bt
#0 0x42028d69 in sigsuspend () from /lib/i686/libc.so.6
#1 0x400aa108 in __pthread_wait_for_restart_signal ()
from /lib/i686/libpthread.so.0
#2 0x400ac480 in __pthread_alt_lock () from /lib/i686/libpthread.so.0
#3 0x400a8f87 in pthread_mutex_lock () from /lib/i686/libpthread.so.0
#4 0x401289bc in _XLockDisplay () from /usr/X11R6/lib/libX11.so.6
#5 0x4010e0d0 in XMatchVisualInfo () from /usr/X11R6/lib/libX11.so.6
#6 0x400608fd in add_visual () from /usr/lib/libSDL-1.2.so.0
#7 0x40060db9 in X11_GetVideoModes () from /usr/lib/libSDL-1.2.so.0
#8 0x40062a53 in X11_VideoInit () from /usr/lib/libSDL-1.2.so.0
#9 0x400576c3 in SDL_VideoInit () from /usr/lib/libSDL-1.2.so.0
#10 0x40033b85 in SDL_InitSubSystem () from /usr/lib/libSDL-1.2.so.0
#11 0x40033bc7 in SDL_Init () from /usr/lib/libSDL-1.2.so.0
#12 0x08048607 in main (argc=1, argv=0xbffff824) at sdlhang.c:140
#13 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6

I have determined that the problem goes away if I comment out the call
to XInitThreads, but unfortunately I need this elsewhere in my
application.

Any ideas as to where I should look next?

Thanks in advance,
–Howdy=============================
Howdy Pierce
Cardinal Peak, LLC

email: howdy -at- cardinalpeak.com
work: (303) 665-3962

Hello,

I tried your code and it just hangup, didn’t write its message even on
the first run. As far as I understood XInitThreads is used to initialize
the X lib with concurrent thread usage. I do not want to give you advice
on your programming but if possible you may try to use SDL whereever you
need X and try the SDL_Threads.On Thu, 2003-08-21 at 00:39, Howdy Pierce wrote:

Hello SDL developers–

This is further information on my email from last Friday regarding
a hang in SDL_Init. Here is a very simple test program that
reliably hangs:

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <SDL/SDL.h>

int main (int argc, char **argv)
{

XInitThreads();

SDL_Init (SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);

printf(“SUCCESS!!!\n”);

exit(0);
}

I am running completely vanilla Red Hat 8.0 with XFree86 4.2.1 and SDL
1.2.5. The program above is saved as the file “sdlhang.c” and compiled as
follows:

gcc -g -O2 -c sdlhang.c

gcc -g -O2 sdlhang.o -o sdlhang -rdynamic
/usr/local/vbrick/lib/libSDL-1.2.so.0 -lpthread -L/usr/X11R6/lib -lXext -lX1
1 -lm -lz

After compiling sdlhang, I run the following shell script:

#!/bin/sh

iter=0

while /bin/true ; do
./sdlhang

iter=echo $iter + 1 | bc -q
echo "Completed iteration $iter"
done

After some number of iterations – sometimes on the first iteration,
sometimes up to 2000 iterations – the test program will hang with the
following backtrace:

(gdb) bt
#0 0x42028d69 in sigsuspend () from /lib/i686/libc.so.6
#1 0x400aa108 in __pthread_wait_for_restart_signal ()
from /lib/i686/libpthread.so.0
#2 0x400ac480 in __pthread_alt_lock () from /lib/i686/libpthread.so.0
#3 0x400a8f87 in pthread_mutex_lock () from /lib/i686/libpthread.so.0
#4 0x401289bc in _XLockDisplay () from /usr/X11R6/lib/libX11.so.6
#5 0x4010e0d0 in XMatchVisualInfo () from /usr/X11R6/lib/libX11.so.6
#6 0x400608fd in add_visual () from /usr/lib/libSDL-1.2.so.0
#7 0x40060db9 in X11_GetVideoModes () from /usr/lib/libSDL-1.2.so.0
#8 0x40062a53 in X11_VideoInit () from /usr/lib/libSDL-1.2.so.0
#9 0x400576c3 in SDL_VideoInit () from /usr/lib/libSDL-1.2.so.0
#10 0x40033b85 in SDL_InitSubSystem () from /usr/lib/libSDL-1.2.so.0
#11 0x40033bc7 in SDL_Init () from /usr/lib/libSDL-1.2.so.0
#12 0x08048607 in main (argc=1, argv=0xbffff824) at sdlhang.c:140
#13 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6

I have determined that the problem goes away if I comment out the call
to XInitThreads, but unfortunately I need this elsewhere in my
application.

Any ideas as to where I should look next?

Thanks in advance,
–Howdy

=============================
Howdy Pierce
Cardinal Peak, LLC

email: howdy -at- cardinalpeak.com
work: (303) 665-3962


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Perit Bezek :slight_smile:

The problem with that is that I’m not directly using SDL; I am working on an
existing X windows-based media player that embeds MPEG4IP as a way to play
MPEG-4 files. MPEG4IP, in turn, uses SDL. The way the code is architected,
I need the XInitThreads elsewhere, and when it gets called, it initializes
concurrency for all threads in the program.

My question is – why is this hanging? The X windows locking mechanism
(XLockDisplay/XUnlockDisplay) is supposed to be re-entrant within a single
thread. From the XLockDisplay man page:

“The XLockDisplay() function locks out all other threads from using the
specified display. Other threads attempting to use the display will block
until the display is unlocked by this thread. Nested calls to XLockDisplay()
work correctly; the display will not actually be unlocked until
XUnlockDisplay() has been called the same number of times as XLockDisplay().
This function has no effect unless Xlib was successfully initialized for
threads using XInitThreads().”

In the test program I emailed, there’s only one thread ever created, so the
fact that it hangs suggests to me that something is getting corrupted
somewhere. I have even tried to hack up the SDL code to bracket all X calls
with calls to XLockDisplay/XUnlockDisplay, but the hang still occurs.

Any pointers or thoughts on this would be welcome, since one week ago I
didn’t know anything about SDL and I’m coming up to speed only slowly.

Thanks for your help,
–Howdy> -----Original Message-----

From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Perit Bezek
Sent: Thursday, August 21, 2003 3:17 AM
To: sdl at libsdl.org
Subject: Re: [SDL] more on hang in SDL_Init

Hello,

I tried your code and it just hangup, didn’t write its message even on
the first run. As far as I understood XInitThreads is used to initialize
the X lib with concurrent thread usage. I do not want to give you advice
on your programming but if possible you may try to use SDL whereever you
need X and try the SDL_Threads.

On Thu, 2003-08-21 at 00:39, Howdy Pierce wrote:

Hello SDL developers–

This is further information on my email from last Friday regarding
a hang in SDL_Init. Here is a very simple test program that
reliably hangs:

It turns out that I can “fix” this bug by modifying SDL to use the native
XFree86 functions instead of the versions of those functions included under
src/video/XFree86 in the SDL tree. Which begs the question: Why is some of
the XFree86 code included in SDL in the first place?

(The root cause of the problem is that, when using the versions of the
XFree86 functions included in the SDL tree, the Xlib internal
"_XUnlockMutex" would sometimes be called to unlock an already-unlocked
mutex. This leaves the mutex in an undetermined state, and so sometimes on
the next call to “_XLockMutex” the program would hang. Without the initial
call to XInitThreads, none of the Xlib finctions try to call these
locking/unlocking routines to begin with, which is why you don’t see the
problem until using XInitThreads.)

Thanks for your help,
–Howdy> -----Original Message-----

From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Howdy Pierce
Sent: Thursday, August 21, 2003 8:19 AM
To: sdl at libsdl.org
Subject: RE: [SDL] more on hang in SDL_Init

The problem with that is that I’m not directly using SDL; I am
working on an
existing X windows-based media player that embeds MPEG4IP as a way to play
MPEG-4 files. MPEG4IP, in turn, uses SDL. The way the code is
architected,
I need the XInitThreads elsewhere, and when it gets called, it initializes
concurrency for all threads in the program.

My question is – why is this hanging? The X windows locking mechanism
(XLockDisplay/XUnlockDisplay) is supposed to be re-entrant within a single
thread. From the XLockDisplay man page:

“The XLockDisplay() function locks out all other threads from using the
specified display. Other threads attempting to use the display will block
until the display is unlocked by this thread. Nested calls to
XLockDisplay()
work correctly; the display will not actually be unlocked until
XUnlockDisplay() has been called the same number of times as
XLockDisplay().
This function has no effect unless Xlib was successfully initialized for
threads using XInitThreads().”

In the test program I emailed, there’s only one thread ever
created, so the
fact that it hangs suggests to me that something is getting corrupted
somewhere. I have even tried to hack up the SDL code to bracket
all X calls
with calls to XLockDisplay/XUnlockDisplay, but the hang still occurs.

Any pointers or thoughts on this would be welcome, since one week ago I
didn’t know anything about SDL and I’m coming up to speed only slowly.

Thanks for your help,
–Howdy

-----Original Message-----
From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Perit Bezek
Sent: Thursday, August 21, 2003 3:17 AM
To: sdl at libsdl.org
Subject: Re: [SDL] more on hang in SDL_Init

Hello,

I tried your code and it just hangup, didn’t write its message even on
the first run. As far as I understood XInitThreads is used to initialize
the X lib with concurrent thread usage. I do not want to give you advice
on your programming but if possible you may try to use SDL whereever you
need X and try the SDL_Threads.

On Thu, 2003-08-21 at 00:39, Howdy Pierce wrote:

Hello SDL developers–

This is further information on my email from last Friday regarding
a hang in SDL_Init. Here is a very simple test program that
reliably hangs:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

It turns out that I can “fix” this bug by modifying SDL to use the native
XFree86 functions instead of the versions of those functions included under
src/video/XFree86 in the SDL tree. Which begs the question: Why is some of
the XFree86 code included in SDL in the first place?

Because there are / were bugs relating to remote display in the installed
versions of the libraries at the time. Also, it worked around a problem
on debian where the X extension libraries are shared libraries which caused
some other problems. Oh, and I added a patch which never made it into the
official release, which allows DGA without needing root on a properly
configured framebuffer console.

(The root cause of the problem is that, when using the versions of the
XFree86 functions included in the SDL tree, the Xlib internal
"_XUnlockMutex" would sometimes be called to unlock an already-unlocked
mutex. This leaves the mutex in an undetermined state, and so sometimes on
the next call to “_XLockMutex” the program would hang. Without the initial
call to XInitThreads, none of the Xlib finctions try to call these
locking/unlocking routines to begin with, which is why you don’t see the
problem until using XInitThreads.)

Did you track down why it unlocks an unlocked mutex? This is a bug that
needs to be fixed. I’ll merge any changes from the latest XFree86 client
libraries, but I’m betting that it hasn’t been run across before. :slight_smile:

Thanks!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Hello SDL developers–

This is further information on my email from last Friday regarding
a hang in SDL_Init. Here is a very simple test program that
reliably hangs:

Fixed in CVS. I wasn’t building the X extension libraries with -DXTHREADS,
which meant that it wasn’t doing any locking.

Thanks!
-Sam Lantinga, Software Engineer, Blizzard Entertainment