<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5, Linux] Why does this code hang?</title>
		<link>http://www.allegro.cc/forums/view/617179</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 20 Dec 2017 18:15:57 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s been a long time since I posted here but I&#39;m just trying to get back into Allegro and I just hit this. Why does the following code hang?:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;allegro5/allegro.h&gt;</span>

<a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span>display <span class="k3">=</span> NULL<span class="k2">;</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_install_keyboard"><span class="a">al_install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_install_mouse"><span class="a">al_install_mouse</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_set_new_display_flags"><span class="a">al_set_new_display_flags</span></a><span class="k2">(</span>ALLEGRO_WINDOWED<span class="k2">)</span><span class="k2">;</span>
    display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">800</span>, <span class="n">600</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">;</span>
    <span class="c">// al_flip_display();</span>
<span class="k2">}</span>
</pre></div></div><p>

I know this code is pretty much useless but I find it still weird that it hangs. I&#39;m using Debian Stretch and Allegro 5.2.2 from the official repository. Uncommenting the last line makes the code work as expected so it&#39;s not really that much of a big deal but I was puzzled by hitting this, mainly because I thought I had an error in my initialization code (not shown here).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kenmasters1976)</author>
		<pubDate>Tue, 19 Dec 2017 04:39:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p> - I would make sure you&#39;ve got a proper compiled version of Allegro. No &quot;multiple versions on my system&quot; that&#39;ll blow up if the installed vs compiled are different.</p><p> - Does removing windowed mode affect it?</p><p>Run gdb ./my_program, then as it halts, in the gdb terminal hit control-C. That&#39;ll stop execution. You can then type bt for backtrace and it will show you the function stack where everything is frozen.</p><p>When you say, &quot;halt&quot; you mean it freezes right? Not &quot;it crashes to desktop/terminates&quot;. Though, even if it does that, gdb will tell you a hint of why.</p><p> - One more clarification: You gave us sample code. Does that sample code (exactly as written) do what you describe?</p><p>[edit] What&#39;s your hardware look like? Do you have multiple graphics adapters?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
ALLEGRO_WINDOWED<br />Prefer a windowed mode.</p><p>Under multi-head X (not XRandR/TwinView), the use of more than one adapter is impossible due to bugs in X and glX. al_create_display will fail if more than one adapter is attempted to be used.
</p></div></div><p>

[edit]</p><p>Yep. I compiled it with my distribution (took awhile since I always use D now!). It runs fine. It doesn&#39;t freeze or &quot;halt.&quot; It does exit to desktop successfully.</p><p><b>Definitely recheck your Allegro installation.</b> Make sure there is only one version, or, that you are linking and compiling with exactly one version.</p><p>So at this point it seems like it&#39;s reduced to either:</p><p> - Allegro lib linking error (as mentioned above). </p><p> - Some hardware or driver specific bug.</p><p> - An Allegro bug that fails only with your specific OS, hardware, etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 19 Dec 2017 04:45:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>[EDIT:] You edited you post as I was writing mine. I guess I&#39;m gonna build Allegro myself and make some tests. I&#39;ve built it before but this time I was being lazy and decided to use the version from the Debian repository. I will report back the results. Thanks.</p><p>[Original post follows]</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617179/1033961#target">Chris Katko</a> said:</div><div class="quote"><p> - I would make sure you&#39;ve got a proper compiled version of Allegro. No &quot;multiple versions on my system&quot; that&#39;ll blow up if the installed vs compiled are different.
</p></div></div><p>I have only the version from the official Debian repository installed.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> - Does removing windowed mode affect it?
</p></div></div><p>Same result. Actually, the following code has the same issue. I guess it can&#39;t be smaller than that:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;allegro5/allegro.h&gt;</span>

<a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span>display <span class="k3">=</span> NULL<span class="k2">;</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
    display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">800</span>, <span class="n">600</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">;</span>
    <span class="c">// al_flip_display();</span>
<span class="k2">}</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> When you say, &quot;halt&quot; you mean it freezes right? Not &quot;it crashes to desktop/terminates&quot;. 
</p></div></div><p>That&#39;s right, the Allegro window freezes. I&#39;d expect the Allegro window to appear as I call <span class="source-code"><a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="k2">)</span></span> and then to close immediately; instead, the Allegro window remains onscreen and I have to force the program termination.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> - One more clarification: You gave us sample code. Does that sample code (exactly as written) do what you describe?
</p></div></div><p>Yes, as does the code posted here.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> What&#39;s your hardware look like? Do you have multiple graphics adapters?
</p></div></div><p>Just a single AMD graphics card:
</p><pre class="terminal">OpenGL renderer string: Gallium 0.4 on AMD CEDAR (DRM 2.49.0 / 4.9.0-4-amd64, LLVM 3.9.1)</pre><p>

I&#39;m gonna try running it with <i>gdb</i> now.</p><p>[EDIT 2:] Built Allegro 5.2.3 and got the same result. Weird. This is <i>gbd</i>&#39;s backtrace:</p><pre class="terminal scroll">(gdb) run
Starting program: /home/ken/code/allegro/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.
[New Thread 0x7ffff3048700 (LWP 11910)]
Allegro version: 5.2.2
[New Thread 0x7fffed11e700 (LWP 11911)]
[Thread 0x7fffed11e700 (LWP 11911) exited]
[Thread 0x7ffff3048700 (LWP 11910) exited]
^C  &lt;-- Forced program termination here
Thread 1 &quot;a.out&quot; received signal SIGINT, Interrupt.
pthread_cond_wait@@GLIBC_2.3.2 ()
    at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
185	../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S: No such file or directory.
(gdb) bt
#0  pthread_cond_wait@@GLIBC_2.3.2 ()
    at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
#1  0x00007ffff1e9893b in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#2  0x00007ffff21cde2f in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#3  0x00007ffff21edef6 in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#4  0x00007ffff21ecd7e in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#5  0x00007ffff1caaaf4 in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#6  0x00007ffff1b5c448 in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#7  0x00007ffff1ceb5ab in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#8  0x00007ffff1dfe04f in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#9  0x00007ffff1dfdafa in ?? () from /usr/lib/x86_64-linux-gnu/dri/r600_dri.so
#10 0x00007ffff62374f5 in glXMakeCurrentReadSGI ()
   from /usr/lib/x86_64-linux-gnu/libGL.so.1
#11 0x00007ffff7b2eb44 in al_set_target_bitmap ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#12 0x00007ffff7b1a54f in al_destroy_display ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#13 0x00007ffff7b76df2 in ?? ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#14 0x00007ffff7b2bf35 in ?? ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#15 0x00007ffff7b1feed in _al_run_exit_funcs ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
#16 0x00007ffff7b2c015 in al_uninstall_system ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#17 0x00007ffff754b910 in __run_exit_handlers (status=0, 
    listp=0x7ffff78af5d8 &lt;__exit_funcs&gt;, 
    run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true)
    at exit.c:83
#18 0x00007ffff754b96a in __GI_exit (status=&lt;optimized out&gt;) at exit.c:105
#19 0x00007ffff75362b8 in __libc_start_main (main=0x555555554a30 &lt;main&gt;, 
    argc=1, argv=0x7fffffffe258, init=&lt;optimized out&gt;, fini=&lt;optimized out&gt;, 
    rtld_fini=&lt;optimized out&gt;, stack_end=0x7fffffffe248)
    at ../csu/libc-start.c:325
#20 0x000055555555492a in _start ()
(gdb) q
A debugging session is active.

	Inferior 1 [process 11903] will be killed.

Quit anyway? (y or n) y</pre><p>

It&#39;s probably a problem with my Linux distro?. Anyway, it&#39;s not a big deal since a typical Allegro 5 program execution wouldn&#39;t hit this issue, I guess, as calling <span class="source-code"><a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span></span> seems to solve the problem. Posting it here just in case it&#39;s of some use.</p><p>Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kenmasters1976)</author>
		<pubDate>Wed, 20 Dec 2017 03:59:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Have you tried updating your graphics drivers? Any other games cause this problem? try &quot;glxgears&quot;.</p><p>This guy has the same problem with Payday 2 in Linux.</p><p><a href="https://steamcommunity.com/app/218620/discussions/22/364039531223905157/">https://steamcommunity.com/app/218620/discussions/22/364039531223905157/</a></p><p>Here&#39;s more listings regarding that driver and pthread_wait</p><p><a href="https://bugs.freedesktop.org/show_bug.cgi?id=99138">https://bugs.freedesktop.org/show_bug.cgi?id=99138</a></p><p><a href="https://github.com/chjj/compton/issues/170">https://github.com/chjj/compton/issues/170</a></p><p>It could be the distro files, or, driver related files.</p><p>This one mentions after updating from Mesa 11 to Mesa 12 (plus other updates) the hang is gone:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I just tested with Mesa 12.0.1, libdrm 2.45.0, kernel 4.7, libdrm 2.4.70 and the hang seems to be gone. I am marking this bug as fixed.</p><p>Thank you for your work!
</p></div></div><p>

<a href="https://bugs.freedesktop.org/show_bug.cgi?id=93516">https://bugs.freedesktop.org/show_bug.cgi?id=93516</a></p><p>(Complete wild guess) I almost wonder if there&#39;s some sort of pthread related wait condition tied to VSYNC and for some strange reason VSYNC never fires off. I wonder if forcing VSYNC on, or off, changes it. You can test that with:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">export</span> vblank_mode<span class="k3">=</span><span class="n">0</span> <span class="c">//one of these then your program</span>
<span class="k1">export</span> vblank_mode<span class="k3">=</span><span class="n">1</span>
<span class="k1">export</span> vblank_mode<span class="k3">=</span><span class="n">2</span>
<span class="k1">export</span> vblank_mode<span class="k3">=</span><span class="n">3</span>
<span class="c">//or,</span>
vblank_mode<span class="k3">=</span><span class="n">0</span> .<span class="k3">/</span>my_program
<span class="c">//etc</span>
</pre></div></div><p>

(I can&#39;t remember which is &quot;force on&quot; and which is &quot;force off&quot;, try all four. )</p><p>[edit]</p><p>FINALLY found a source that mentions what they do instead of saying &#39;just use this&#39; with no documentation:
</p><div class="source-code snippet"><div class="inner"><pre>vblank_mode<span class="k3">=</span><span class="n">0</span> mean disabled by <span class="k1">default</span> 
vblank_mode<span class="k3">=</span><span class="n">1</span> mean application choice, but enabled by <span class="k1">default</span> 
vblank_mode<span class="k3">=</span><span class="n">2</span> mean application choice, but disabled by <span class="k1">default</span> 
vblank_mode<span class="k3">=</span><span class="n">3</span> mean enabled by <span class="k1">default</span>
</pre></div></div><p>
<a href="https://forums.gentoo.org/viewtopic-t-983466-start-0.html">https://forums.gentoo.org/viewtopic-t-983466-start-0.html</a></p><p>So try 0 and 3.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 20 Dec 2017 07:16:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the thorough answer. It does indeed seem like a driver issue as described in the links you posted, and not Allegro related as I originally thought.</p><p>There are two Radeon drivers on Debian (free and non-free) so I might as well try with the non-free driver.</p><p>Setting <i>vblank_mode</i> didn&#39;t have any effect, the result is the same. Also, Debian Stretch comes with <i>Mesa</i> 13.0.6 and <i>libdrm</i> 2.4.74, so it seems there&#39;s still a bug in the free Radeon driver.</p><p>Not a big deal, for Allegro at least, since a proper game loop should work fine. May this post help other users who might hit this.</p><p>Thanks again.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kenmasters1976)</author>
		<pubDate>Wed, 20 Dec 2017 10:08:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Instead of &#39;bt&#39;, try &#39;thread apply all bt&#39;. You will get to see if there are any other threads hanging around. You can also do &#39;info threads&#39; to see which threads are running.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 20 Dec 2017 17:02:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Neat! I learned something about gdb today.</p><p>Another thing you can do is use strace which lists all system/kernel calls coming from your application. (It can also do a summary mode which lists time spent longest in what calls.)</p><p>strace ./my_program</p><div class="source-code snippet"><div class="inner"><pre><span class="k2">[</span>...<span class="k2">]</span>
ioctl<span class="k2">(</span><span class="n">6</span>, DRM_IOCTL_I915_GEM_MADVISE, <span class="n">0x7ffde80b13d0</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span>
ioctl<span class="k2">(</span><span class="n">6</span>, DRM_IOCTL_I915_GEM_MADVISE, <span class="n">0x7ffde80b13b0</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span>
ioctl<span class="k2">(</span><span class="n">6</span>, DRM_IOCTL_I915_GEM_BUSY, <span class="n">0x7ffde80b1370</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span>
ioctl<span class="k2">(</span><span class="n">6</span>, DRM_IOCTL_I915_GEM_MADVISE, <span class="n">0x7ffde80b1360</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span>
<span class="k2">[</span>...<span class="k2">]</span>
</pre></div></div><p>
My graphics driver is i915. If it halts, those messages would likely stop spamming the console on a specific &quot;wait for event&quot;-like message.</p><p>strace -c ./my_program </p><p>Outputs the summary table / c=&quot;counts&quot; of calls and how much time they took individually and total. Usually that&#39;s not a problem for us userland programmers but you can tell when you&#39;re spamming way too many calls, or, theoretically, if &quot;one call&quot; were to magically halt and freeze, strace would still be calculating the time it took. So after a few seconds of being halted, the faulty function would then be at the top of the total execution time. So when you then force an exit, you would (theoretically) see a list of the calls where it froze. </p><div class="source-code snippet"><div class="inner"><pre>% <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a>     seconds  usecs<span class="k3">/</span>call     calls    errors syscall
<span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span>
 <span class="n">87</span>.<span class="n">76</span>    <span class="n">0</span>.<span class="n">040996</span>           <span class="n">1</span>     <span class="n">75539</span>         <span class="n">6</span> ioctl
  <span class="n">8</span>.<span class="n">56</span>    <span class="n">0</span>.<span class="n">004000</span>          <span class="n">95</span>        <span class="n">42</span>           munmap
  <span class="n">3</span>.<span class="n">29</span>    <span class="n">0</span>.<span class="n">001538</span>           <span class="n">0</span>      <span class="n">6420</span>           poll
  <span class="n">0</span>.<span class="n">20</span>    <span class="n">0</span>.<span class="n">000094</span>           <span class="n">0</span>      <span class="n">3756</span>           writev
</pre></div></div><p>


Of course, it&#39;s likely a driver problem. But gdb and strace can help you find whether an issue is your code, the library code (like Allegro), a higher level bug like OpenGL, or even driver problems.</p><p>[edit]</p><p>I also just realized you can do &quot;bt full&quot; which will show all local variables for all frames. That&#39;s pretty handy.</p><p>I&#39;m actually planning on trying gdbgui, which is a front-end for GDB and is inspired by Google&#39;s Chrome debugger:</p><p><a href="https://github.com/cs01/gdbgui/">https://github.com/cs01/gdbgui/</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 20 Dec 2017 18:15:57 +0000</pubDate>
	</item>
</rss>
