[AD] CPU yielding patch

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


Hi!

This is an incomplete version of the CPU yielding feature.
It is implemented for the console, X and DOS system drivers.
It will need Jason Wilkins to fill up the BeOS version, and
someone for the Windows version (Jason too, maybe ? ;o))
Both entries at NULL at present, and will do nothing, so the
patch is usable as is (though I couldn't compile these versions
to make sure it is).

I don't know why, but when using the sched_yield one, a program
still takes up nearly 100% time, so there could be a hidden
flaw here.

George: I used 0 sec 1 usec as the timeout for select when
sched_yield is not available, as 0 sec 0 usec would probably
return immediately. Is this OK ? Since I am new to low level
Linux programming, I'm afraid I could make something wrong,
so tell me :)

Shawn: I won't be angry at you if you decide to not include it, as I did
this under your back ;)
If you don't, maybe you could put it on the library extensions page, since
there seems to be a few people interested ?

-- 
Vincent Penquerc'h
Windows NT - New Trial

diff --recursive -U 3 -N allegro.3.9.30.original/include/allegro/alinline.h allegro/include/allegro/alinline.h
--- allegro.3.9.30.original/include/allegro/alinline.h	Sun Dec 19 12:18:46 1999
+++ allegro/include/allegro/alinline.h	Tue Dec 21 22:31:48 1999
@@ -206,6 +206,16 @@
 
 
 
+AL_INLINE(void, yield_timeslice, (void),
+{
+   ASSERT(system_driver);
+
+   if (system_driver->yield_timeslice)
+      system_driver->yield_timeslice();
+})
+
+
+
 /*******************************************/
 /************ Graphics routines ************/
 /*******************************************/
diff --recursive -U 3 -N allegro.3.9.30.original/include/allegro.h allegro/include/allegro.h
--- allegro.3.9.30.original/include/allegro.h	Tue Dec 21 19:36:53 1999
+++ allegro/include/allegro.h	Tue Dec 21 21:58:39 1999
@@ -167,6 +167,7 @@
    AL_METHOD(int, set_display_switch_callback, (int dir, AL_METHOD(void, cb, (void))));
    AL_METHOD(void, remove_display_switch_callback, (AL_METHOD(void, cb, (void))));
    AL_METHOD(void, display_switch_lock, (int lock, int foreground));
+   AL_METHOD(void, yield_timeslice, (void));
    AL_METHOD(_DRIVER_INFO *, gfx_drivers, (void));
    AL_METHOD(_DRIVER_INFO *, digi_drivers, (void));
    AL_METHOD(_DRIVER_INFO *, midi_drivers, (void));
diff --recursive -U 3 -N allegro.3.9.30.original/src/allegro.c allegro/src/allegro.c
--- allegro.3.9.30.original/src/allegro.c	Mon Nov 22 02:31:34 1999
+++ allegro/src/allegro.c	Tue Dec 21 21:59:41 1999
@@ -598,7 +598,7 @@
    sys_none_init,
    sys_none_exit,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
-   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    sys_no_driver, sys_no_driver, sys_no_driver, sys_no_driver,
    sys_no_driver, sys_no_driver, sys_no_driver
 };
diff --recursive -U 3 -N allegro.3.9.30.original/src/beos/bsystem.c allegro/src/beos/bsystem.c
--- allegro.3.9.30.original/src/beos/bsystem.c	Mon Nov 22 02:31:48 1999
+++ allegro/src/beos/bsystem.c	Tue Dec 21 22:22:53 1999
@@ -51,6 +51,7 @@
    NULL,                        // AL_METHOD(int, set_display_switch_callback, (int dir, AL_METHOD(void, cb, (void))));
    NULL,                        // AL_METHOD(int, remove_display_switch_callback, (AL_METHOD(void, cb, (void))));
    NULL,                        // AL_METHOD(void, display_switch_lock, (int lock));
+   NULL,                        // AL_METHOD(void, yield_timeslice, (void));
    NULL,                        // AL_METHOD(_DRIVER_INFO *, gfx_drivers, (void));
    NULL,                        // AL_METHOD(_DRIVER_INFO *, digi_drivers, (void));
    NULL,                        // AL_METHOD(_DRIVER_INFO *, midi_drivers, (void));
diff --recursive -U 3 -N allegro.3.9.30.original/src/dos/dsystem.c allegro/src/dos/dsystem.c
--- allegro.3.9.30.original/src/dos/dsystem.c	Mon Nov 22 02:31:48 1999
+++ allegro/src/dos/dsystem.c	Tue Dec 21 22:07:40 1999
@@ -90,6 +90,7 @@
 static void sys_dos_restore_console_state(void);
 static void sys_dos_read_palette(void);
 static void sys_dos_set_palette(struct RGB *p, int from, int to, int vsync);
+static void sys_dos_yield_timeslice(void);
 
 #ifdef ALLEGRO_DJGPP
    static void sys_dos_assert(char *msg);
@@ -115,7 +116,9 @@
    NULL, NULL, NULL, NULL, NULL,
    sys_dos_read_palette,
    sys_dos_set_palette,
-   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+   NULL, NULL, NULL, NULL, NULL,
+   sys_dos_yield_timeslice,
+   NULL, NULL, NULL, NULL, NULL, NULL, NULL
 };
 
 
@@ -557,5 +560,17 @@
 void _restore_vga_mode(void)
 {
    /* nothing to be done in DOS */
+}
+
+
+
+/* sys_dos_yield_timeslice:
+ *  Yields the remaining timeslice portion to the system
+ */
+static void sys_dos_yield_timeslice()
+{
+#ifdef ALLEGRO_DJGPP
+  __dpmi_yield();
+#endif
 }
 
diff --recursive -U 3 -N allegro.3.9.30.original/src/linux/lsystem.c allegro/src/linux/lsystem.c
--- allegro.3.9.30.original/src/linux/lsystem.c	Tue Dec 21 19:36:54 1999
+++ allegro/src/linux/lsystem.c	Tue Dec 21 22:28:26 1999
@@ -21,6 +21,14 @@
 #include <unistd.h>
 #include <string.h>
 
+#ifdef _POSIX_PRIORITY_SCHEDULING
+/*
+ * Manpages say systems providing sched_yield() define
+ * _POSIX_PRIORITY_SCHEDULING in unistd.h
+ */
+#include <sched.h>
+#endif
+
 #include "allegro.h"
 #include "allegro/aintern.h"
 #include "allegro/aintunix.h"
@@ -43,6 +51,7 @@
 static void sys_linux_exit(void);
 static void sys_linux_get_executable_name(char *output, int size);
 static void sys_linux_message (char *msg);
+static void sys_linux_yield_timeslice (void);
 
 #define make_getter(x) static _DRIVER_INFO *get_##x##_driver_list (void) { return _linux_##x##_driver_list; }
 	make_getter (gfx)
@@ -81,6 +90,7 @@
    __al_linux_set_display_switch_callback,
    __al_linux_remove_display_switch_callback,
    __al_linux_display_switch_lock,
+   sys_linux_yield_timeslice,
    get_gfx_driver_list,
    NULL, /* digi_driver_list */
    NULL, /* midi_driver_list */
@@ -241,4 +251,20 @@
    free(tmp);
 }
 
+
+
+/* sys_linux_yield_timeslice:
+ *  Yields remaining timeslice portion to the system
+ */
+static void sys_linux_yield_timeslice(void)
+{
+#ifdef _POSIX_PRIORITY_SCHEDULING
+  sched_yield();
+#else
+  struct timeval timeout;
+  timeout.tv_sec = 0;
+  timeout.tc_usec = 1;
+  select(0,NULL,NULL,NULL,&timeout);
+#endif
+}
 
diff --recursive -U 3 -N allegro.3.9.30.original/src/win/wsystem.c allegro/src/win/wsystem.c
--- allegro.3.9.30.original/src/win/wsystem.c	Tue Dec 21 19:36:54 1999
+++ allegro/src/win/wsystem.c	Tue Dec 21 22:23:39 1999
@@ -70,6 +70,7 @@
    sys_directx_set_display_switch_callback,
    sys_directx_remove_display_switch_callback,
    NULL,                        /* AL_METHOD(void, display_switch_lock, (int lock)); */
+   NULL,                        /* AL_METHOD(void, yield_timeslice, (void)); */
    NULL,                        /* AL_METHOD(_DRIVER_INFO *, gfx_drivers, (void)); */
    _get_digi_driver_list,       /* AL_METHOD(_DRIVER_INFO *, digi_drivers, (void)); */
    _get_midi_driver_list,       /* AL_METHOD(_DRIVER_INFO *, midi_drivers, (void)); */
diff --recursive -U 3 -N allegro.3.9.30.original/src/x/xsystem.c allegro/src/x/xsystem.c
--- allegro.3.9.30.original/src/x/xsystem.c	Tue Dec 21 19:36:55 1999
+++ allegro/src/x/xsystem.c	Tue Dec 21 22:26:00 1999
@@ -25,6 +25,13 @@
 #include <sys/time.h>
 #include <unistd.h>
 
+#ifdef _POSIX_PRIORITY_SCHEDULING
+/*
+ * Manpages say systems providing sched_yield() define
+ * _POSIX_PRIORITY_SCHEDULING in unistd.h
+ */
+#include <sched.h>
+#endif
 
 
 void (*_xwin_keyboard_interrupt)(int pressed, int code) = 0;
@@ -37,6 +44,7 @@
 static void _xwin_sysdrv_get_executable_name(char *output, int size);
 static void _xwin_sysdrv_set_window_title(char *name);
 static int _xwin_sysdrv_display_switch_mode(int mode);
+static void _xwin_sysdrv_yield_timeslice(void);
 static _DRIVER_INFO *_xwin_sysdrv_gfx_drivers(void);
 static _DRIVER_INFO *_xwin_sysdrv_keyboard_drivers(void);
 static _DRIVER_INFO *_xwin_sysdrv_mouse_drivers(void);
@@ -72,6 +80,7 @@
    NULL, /* set_display_switch_callback */
    NULL, /* remove_display_switch_callback */
    NULL, /* display_switch_lock */
+   _xwin_sysdrv_yield_timeslice,
    _xwin_sysdrv_gfx_drivers,
    NULL, /* digi_driver_list */
    NULL, /* midi_driver_list */
@@ -312,4 +321,18 @@
    return 0;
 }
 
+/* _xwin_sysdrv_yield_timeslice:
+ *  Yields remaining timeslice portion to the system
+ */
+static void _xwin_sysdrv_yield_timeslice(void)
+{
+#ifdef _POSIX_PRIORITY_SCHEDULING
+  sched_yield();
+#else
+  struct timeval timeout;
+  timeout.tv_sec = 0;
+  timeout.tc_usec = 1;
+  select(0,NULL,NULL,NULL,&timeout);
+#endif
+}
 


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