Re: [AD] X11 unresponsiveness

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Tue, 2004-07-13 at 00:04 +0200, Evert Glebbeek wrote:
> > Yeah, of course, I understand now. So the signals version inherently has
> > that problem.
> 
> That's what I was afraid of...
> 
> > I think, 2 would be the cleanest method. 3 is quite similiar. 1 has the
> > problem that all drawing operations would get slower. I'm strongly
> > against 4. Any maybe someone here comes up with an even better idea than
> > my 3.
> 
> I agree that 2 would be the best method. To be clear on this, for the 
> multi-threaded version of the library, we can get away with simply 
> removing the offending line, right?
> In that case, we could fix the multi-threaded version first and retain the 
> current behavior for the sigalrm version until the improved method can be 
> implemented...?
> 

Ok, the attached patch should completely resolve the lag problem for the
threaded version, and improve it a lot with the signals version. With
the threaded version, it is like we said - the thread blocks in XLOCK,
then input is read. Maximum responsiveness is sure.

With the signals version, I'm timing how long it was since the last
event was process. If it is longer than 50ms, all function calling XLOCK
start blocking, until the event signal is called again. (We can't just
process events, or block inside _xwin_event_handler, since it is a
signal handler and has to return soon.) Of course, this is not optimal,
since how all that is ensured is, that at least 5 events are processed
every 50ms. But it as a lot better than before.

-- 
Elias Pschernig
Index: src/x/xwin.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/x/xwin.c,v
retrieving revision 1.55
diff -u -p -r1.55 xwin.c
--- src/x/xwin.c	29 Oct 2003 12:37:57 -0000	1.55
+++ src/x/xwin.c	12 Jul 2004 23:30:11 -0000
@@ -46,6 +46,10 @@
 #include <X11/extensions/xf86dga.h>
 #endif
 
+#ifndef HAVE_LIBPTHREAD
+#include <sys/time.h>
+#endif
+
 
 #define XWIN_DEFAULT_WINDOW_TITLE "Allegro application"
 #define XWIN_DEFAULT_APPLICATION_NAME "allegro"
@@ -64,6 +68,7 @@ struct _xwin_type _xwin =
    0,           /* ximage */
    None,        /* cursor */
    XC_heart,    /* cursor_shape */
+   0,           /* input_priority */
 
    0,           /* bank_switch */
    0,           /* screen_to_buffer */
@@ -143,6 +148,12 @@ static int _xdga_installed_colormap; 
 static Colormap _xdga_colormap[2];
 #endif
 
+#ifndef HAVE_LIBPTHREAD
+static struct timeval last_input_time = {0, 0};
+/* how many us before input events get priority */
+#define MAX_INPUT_LAG_US 50000
+#endif
+
 #define MOUSE_WARP_DELAY   200
 
 static char _xwin_driver_desc[256] = EMPTY_STRING;
@@ -2252,7 +2263,25 @@ static void _xwin_private_handle_input(v
 
 void _xwin_handle_input(void)
 {
-   if (_xwin.lock_count) return;
+#ifndef HAVE_LIBPTHREAD
+   /* If there was no input processing for too long, prioritize the
+    * input handler. With the threaded version, XLOCK simply blocks,
+    * so this is not needed there.
+    */
+   struct timeval current;
+   gettimeofday(&current, NULL);
+
+   if (_xwin.lock_count)
+   {
+      if ((current.tv_sec - last_input_time.tv_sec) * 1000000 +
+         (current.tv_usec - last_input_time.tv_usec) > MAX_INPUT_LAG_US)
+         _xwin.input_priority = 1;
+      return;
+   }
+
+   _xwin.input_priority = 0;
+   last_input_time = current;
+#endif
 
    XLOCK();
 
Index: include/xalleg.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/xalleg.h,v
retrieving revision 1.16
diff -u -p -r1.16 xalleg.h
--- include/xalleg.h	15 Nov 2002 11:07:48 -0000	1.16
+++ include/xalleg.h	12 Jul 2004 23:30:11 -0000
@@ -68,6 +68,12 @@ extern struct _xwin_type
    Cursor cursor;
    int cursor_shape;
 
+   /* With the signals version, this gives the input signals
+	* priority over other functions requiring X locks, to always
+	* ensure responsiveness. It is unused in the threaded version.
+	*/
+   int input_priority;
+
    void (*bank_switch)(int line);
    void (*screen_to_buffer)(int sx, int sy, int sw, int sh);
    void (*set_colors)(AL_CONST PALETTE p, int from, int to);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/