[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
Peter Wang wrote:
The question is how long to sleep for. The ~100 times per second not
completely arbitrary. You pretty much cannot make a thread sleep for
less than 10 ms on Linux (and others). Maybe if you tweak some kernel
options...?
Well, on my newly installed kernel I found an option to tweak the kernel
timer.. I'm not sure if this effects scheduling (and thusly how much you
can sleep), but certain indications say yes.
Though if the loop runs once every 10ms, I'd think sleeping for 1ms
would be optimal. If the thread can sleep for less than 10ms it should
improve responsiveness, and if not, nothing changes.
Here's a patch (made from the 4.1.14 source) that times the loop using
the usecs directly instead of trying to convert to TIMERS_PER_SEC,
sleeps as little as 1ms between tests (where available), and as well can
handle a pause in the process for about an hour or so, when the
microsecond time difference would overflow the unsigned long (the
current code would suffer the same fate).
- Kitty Cat
--- uthreads.c.orig 2004-07-07 23:38:24.000000000 -0700
+++ uthreads.c 2004-07-08 00:02:25.000000000 -0700
@@ -65,23 +65,24 @@
{
struct timeval old_time, new_time;
struct timeval delay;
- unsigned long interval, i;
+ unsigned long interval;
int n;
block_all_signals();
+ interval = 0;
gettimeofday(&old_time, 0);
while (1) {
gettimeofday(&new_time, 0);
- interval = ((new_time.tv_sec - old_time.tv_sec) * 1000000L +
- (new_time.tv_usec - old_time.tv_usec));
+ /* add the new time difference to the remainder of the old difference */
+ interval += ((new_time.tv_sec - old_time.tv_sec) * 1000000L +
+ (new_time.tv_usec - old_time.tv_usec));
old_time = new_time;
- while (interval) {
- i = MIN (interval, INT_MAX/(TIMERS_PER_SECOND/100));
- interval -= i;
- i = i * (TIMERS_PER_SECOND/100) / 10000L;
+ /* run the callbacks for each 10ms elapsed */
+ while (interval > 10000) {
+ interval -= 10000;
pthread_mutex_lock(&cli_mutex);
@@ -98,8 +99,9 @@
pthread_mutex_unlock(&cli_mutex);
}
+ /* rest a little bit before checking again */
delay.tv_sec = 0;
- delay.tv_usec = 10000;
+ delay.tv_usec = 1000;
select(0, NULL, NULL, NULL, &delay);
pthread_testcancel();
}