From 632943f359274cef42a6a77d4fa43caeedef7d87 Mon Sep 17 00:00:00 2001 From: Sebastian Wilhelmi Date: Thu, 7 Jun 2001 13:46:14 +0000 Subject: [PATCH] Look for nanosleep function. 2001-06-07 Sebastian Wilhelmi * configure.in: Look for nanosleep function. * gtimer.c: Use nanosleep for g_usleep, when found. * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. * gasyncqueue.c: Documentation updates. * gthreadpool.c: Use g_time_val_add now that we have it. * glib/glib-sections.txt, glib/tmpl/date.sgml: Add g_time_val_add. * glib/tmpl/threads.sgml: Updated. --- ChangeLog | 12 ++++++ ChangeLog.pre-2-0 | 12 ++++++ ChangeLog.pre-2-10 | 12 ++++++ ChangeLog.pre-2-12 | 12 ++++++ ChangeLog.pre-2-2 | 12 ++++++ ChangeLog.pre-2-4 | 12 ++++++ ChangeLog.pre-2-6 | 12 ++++++ ChangeLog.pre-2-8 | 12 ++++++ configure.in | 2 +- docs/reference/ChangeLog | 6 +++ docs/reference/glib/glib-sections.txt | 1 + docs/reference/glib/tmpl/date.sgml | 9 ++++ docs/reference/glib/tmpl/threads.sgml | 5 +++ gasyncqueue.c | 6 +++ glib/gasyncqueue.c | 6 +++ glib/gthreadpool.c | 14 ++---- glib/gtimer.c | 62 ++++++++++++++++++++++----- glib/gtimer.h | 18 +++++--- gthreadpool.c | 14 ++---- gtimer.c | 62 ++++++++++++++++++++++----- gtimer.h | 18 +++++--- 21 files changed, 262 insertions(+), 57 deletions(-) diff --git a/ChangeLog b/ChangeLog index 223f5a74d..83a31df22 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 223f5a74d..83a31df22 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,15 @@ +2001-06-07 Sebastian Wilhelmi + + * configure.in: Look for nanosleep function. + + * gtimer.c: Use nanosleep for g_usleep, when found. + + * gtimer.c, gtimer.h: Add g_time_val_add function. Closes #54271. + + * gasyncqueue.c: Documentation updates. + + * gthreadpool.c: Use g_time_val_add now that we have it. + 2001-06-01 Jon Trowbridge * gdate.c (g_date_update_julian): Changed to take a const diff --git a/configure.in b/configure.in index 774d765e1..58c33abda 100644 --- a/configure.in +++ b/configure.in @@ -460,7 +460,7 @@ GLIB_SIZEOF([$size_includes], ptrdiff_t, ptrdiff_t) GLIB_SIZEOF([$size_includes], intmax_t, intmax_t) # Check for some functions -AC_CHECK_FUNCS(lstat strerror strsignal memmove mkstemp vsnprintf stpcpy strcasecmp strncasecmp poll getcwd) +AC_CHECK_FUNCS(lstat strerror strsignal memmove mkstemp vsnprintf stpcpy strcasecmp strncasecmp poll getcwd nanosleep) # Check if bcopy can be used for overlapping copies, if memmove isn't found. # The check is borrowed from the PERL Configure script. diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 113ba61ad..296467836 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,9 @@ +2001-06-07 Sebastian Wilhelmi + + * glib/glib-sections.txt, glib/tmpl/date.sgml: Add g_time_val_add. + + * glib/tmpl/threads.sgml: Updated. + 2001-05-23 Sebastian Wilhelmi * glib/tmpl/arrays.sgml, glib/tmpl/arrays_pointers.sgml, diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 79d362c1d..0d377f5da 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -857,6 +857,7 @@ G_USEC_PER_SEC GTimeVal g_get_current_time g_usleep +g_time_val_add GDate diff --git a/docs/reference/glib/tmpl/date.sgml b/docs/reference/glib/tmpl/date.sgml index 60d0a4d6f..7d65991bc 100644 --- a/docs/reference/glib/tmpl/date.sgml +++ b/docs/reference/glib/tmpl/date.sgml @@ -101,6 +101,15 @@ rely on the exact length of the sleep. @microseconds: number of microseconds to pause + + + + + +@time: +@microseconds: + + Represents a day between January 1, Year 1 and a few thousand years in diff --git a/docs/reference/glib/tmpl/threads.sgml b/docs/reference/glib/tmpl/threads.sgml index 5dbbb170a..80f2c7141 100644 --- a/docs/reference/glib/tmpl/threads.sgml +++ b/docs/reference/glib/tmpl/threads.sgml @@ -1366,6 +1366,11 @@ This function can also be used, if g_thread_init() has not yet been called and will immediately return TRUE then. + +To easily calculate @abs_time a combination of g_get_current_time() +and g_time_val_add() can be used. + + @cond: a #GCond @mutex: a #GMutex, that is currently locked @abs_time: a #GTimeVal, determining the final time diff --git a/gasyncqueue.c b/gasyncqueue.c index 126bb1d84..39c035d86 100644 --- a/gasyncqueue.c +++ b/gasyncqueue.c @@ -343,6 +343,9 @@ g_async_queue_try_pop_unlocked (GAsyncQueue* queue) * Pop data from the @queue. If no data is received before @end_time, * #NULL is returned. * + * To easily calculate @end_time a combination of g_get_current_time() + * and g_time_val_add() can be used. + * * Return value: data from the queue or #NULL, when no data is * received before @end_time. **/ @@ -370,6 +373,9 @@ g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time) * #NULL is returned. This function must be called while holding the * @queue's lock. * + * To easily calculate @end_time a combination of g_get_current_time() + * and g_time_val_add() can be used. + * * Return value: data from the queue or #NULL, when no data is * received before @end_time. **/ diff --git a/glib/gasyncqueue.c b/glib/gasyncqueue.c index 126bb1d84..39c035d86 100644 --- a/glib/gasyncqueue.c +++ b/glib/gasyncqueue.c @@ -343,6 +343,9 @@ g_async_queue_try_pop_unlocked (GAsyncQueue* queue) * Pop data from the @queue. If no data is received before @end_time, * #NULL is returned. * + * To easily calculate @end_time a combination of g_get_current_time() + * and g_time_val_add() can be used. + * * Return value: data from the queue or #NULL, when no data is * received before @end_time. **/ @@ -370,6 +373,9 @@ g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time) * #NULL is returned. This function must be called while holding the * @queue's lock. * + * To easily calculate @end_time a combination of g_get_current_time() + * and g_time_val_add() can be used. + * * Return value: data from the queue or #NULL, when no data is * received before @end_time. **/ diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c index c2620ccbc..601022830 100644 --- a/glib/gthreadpool.c +++ b/glib/gthreadpool.c @@ -86,20 +86,12 @@ g_thread_pool_thread_proxy (gpointer data) * pool afterwards */ GTimeVal end_time; g_get_current_time (&end_time); - end_time.tv_usec += G_USEC_PER_SEC / 2; /* Halv a second */ - if (end_time.tv_usec >= G_USEC_PER_SEC) - { - end_time.tv_usec -= G_USEC_PER_SEC; - end_time.tv_sec += 1; - } - + g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */ task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time); } else - { - task = g_async_queue_pop_unlocked (pool->queue); - } - + task = g_async_queue_pop_unlocked (pool->queue); + if (task) { watcher = FALSE; diff --git a/glib/gtimer.c b/glib/gtimer.c index d5e06b011..2064131f1 100644 --- a/glib/gtimer.c +++ b/glib/gtimer.c @@ -38,6 +38,8 @@ #endif /* HAVE_UNISTD_H */ #ifndef G_OS_WIN32 #include +#include +#include #endif /* G_OS_WIN32 */ #ifdef G_OS_WIN32 @@ -193,7 +195,14 @@ g_usleep (gulong microseconds) { #ifdef G_OS_WIN32 Sleep (microseconds / 1000); -#else +#else /* !G_OS_WIN32 */ +# ifdef HAVE_NANOSLEEP + struct timespec request, remaining; + request.tv_sec = microseconds / G_USEC_PER_SEC; + request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC); + while (nanosleep (&request, &remaining) == EINTR) + request = remaining; +# else /* !HAVE_NANOSLEEP */ if (g_thread_supported ()) { static GStaticMutex mutex = G_STATIC_MUTEX_INIT; @@ -201,16 +210,13 @@ g_usleep (gulong microseconds) GTimeVal end_time; g_get_current_time (&end_time); - - end_time.tv_sec += microseconds / G_USEC_PER_SEC; - end_time.tv_usec += microseconds % G_USEC_PER_SEC; - - if (end_time.tv_usec >= G_USEC_PER_SEC) + if (microseconds > G_MAXLONG) { - end_time.tv_usec -= G_USEC_PER_SEC; - end_time.tv_sec += 1; + microseconds -= G_MAXLONG; + g_time_val_add (&end_time, G_MAXLONG); } - + g_time_val_add (&end_time, microseconds); + g_static_mutex_lock (&mutex); if (!cond) @@ -229,6 +235,42 @@ g_usleep (gulong microseconds) tv.tv_usec = microseconds % G_USEC_PER_SEC; select(0, NULL, NULL, NULL, &tv); } -#endif +# endif /* !HAVE_NANOSLEEP */ +#endif /* !G_OS_WIN32 */ } +/** + * g_time_val_add: + * @time: a #GTimeVal + * @microseconds: number of microseconds to add to @time + * + * Adds the given number of microseconds to @time. @microseconds can + * also be negative to decrease the value of @time. + **/ +void +g_time_val_add (GTimeVal *time, glong microseconds) +{ + g_return_if_fail (time->tv_usec >= 0 && time->tv_usec < G_USEC_PER_SEC); + + if (microseconds >= 0) + { + time->tv_usec += microseconds % G_USEC_PER_SEC; + time->tv_sec += microseconds / G_USEC_PER_SEC; + if (time->tv_usec >= G_USEC_PER_SEC) + { + time->tv_usec -= G_USEC_PER_SEC; + time->tv_sec++; + } + } + else + { + microseconds *= -1; + time->tv_usec -= microseconds % G_USEC_PER_SEC; + time->tv_sec -= microseconds / G_USEC_PER_SEC; + if (time->tv_usec < 0) + { + time->tv_usec += G_USEC_PER_SEC; + time->tv_sec--; + } + } +} diff --git a/glib/gtimer.h b/glib/gtimer.h index 533a778f1..823d63442 100644 --- a/glib/gtimer.h +++ b/glib/gtimer.h @@ -40,13 +40,17 @@ typedef struct _GTimer GTimer; #define G_USEC_PER_SEC 1000000 GTimer* g_timer_new (void); -void g_timer_destroy (GTimer *timer); -void g_timer_start (GTimer *timer); -void g_timer_stop (GTimer *timer); -void g_timer_reset (GTimer *timer); -gdouble g_timer_elapsed (GTimer *timer, - gulong *microseconds); -void g_usleep (gulong microseconds); +void g_timer_destroy (GTimer *timer); +void g_timer_start (GTimer *timer); +void g_timer_stop (GTimer *timer); +void g_timer_reset (GTimer *timer); +gdouble g_timer_elapsed (GTimer *timer, + gulong *microseconds); + +void g_usleep (gulong microseconds); + +void g_time_val_add (GTimeVal *time, + glong microseconds); G_END_DECLS diff --git a/gthreadpool.c b/gthreadpool.c index c2620ccbc..601022830 100644 --- a/gthreadpool.c +++ b/gthreadpool.c @@ -86,20 +86,12 @@ g_thread_pool_thread_proxy (gpointer data) * pool afterwards */ GTimeVal end_time; g_get_current_time (&end_time); - end_time.tv_usec += G_USEC_PER_SEC / 2; /* Halv a second */ - if (end_time.tv_usec >= G_USEC_PER_SEC) - { - end_time.tv_usec -= G_USEC_PER_SEC; - end_time.tv_sec += 1; - } - + g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */ task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time); } else - { - task = g_async_queue_pop_unlocked (pool->queue); - } - + task = g_async_queue_pop_unlocked (pool->queue); + if (task) { watcher = FALSE; diff --git a/gtimer.c b/gtimer.c index d5e06b011..2064131f1 100644 --- a/gtimer.c +++ b/gtimer.c @@ -38,6 +38,8 @@ #endif /* HAVE_UNISTD_H */ #ifndef G_OS_WIN32 #include +#include +#include #endif /* G_OS_WIN32 */ #ifdef G_OS_WIN32 @@ -193,7 +195,14 @@ g_usleep (gulong microseconds) { #ifdef G_OS_WIN32 Sleep (microseconds / 1000); -#else +#else /* !G_OS_WIN32 */ +# ifdef HAVE_NANOSLEEP + struct timespec request, remaining; + request.tv_sec = microseconds / G_USEC_PER_SEC; + request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC); + while (nanosleep (&request, &remaining) == EINTR) + request = remaining; +# else /* !HAVE_NANOSLEEP */ if (g_thread_supported ()) { static GStaticMutex mutex = G_STATIC_MUTEX_INIT; @@ -201,16 +210,13 @@ g_usleep (gulong microseconds) GTimeVal end_time; g_get_current_time (&end_time); - - end_time.tv_sec += microseconds / G_USEC_PER_SEC; - end_time.tv_usec += microseconds % G_USEC_PER_SEC; - - if (end_time.tv_usec >= G_USEC_PER_SEC) + if (microseconds > G_MAXLONG) { - end_time.tv_usec -= G_USEC_PER_SEC; - end_time.tv_sec += 1; + microseconds -= G_MAXLONG; + g_time_val_add (&end_time, G_MAXLONG); } - + g_time_val_add (&end_time, microseconds); + g_static_mutex_lock (&mutex); if (!cond) @@ -229,6 +235,42 @@ g_usleep (gulong microseconds) tv.tv_usec = microseconds % G_USEC_PER_SEC; select(0, NULL, NULL, NULL, &tv); } -#endif +# endif /* !HAVE_NANOSLEEP */ +#endif /* !G_OS_WIN32 */ } +/** + * g_time_val_add: + * @time: a #GTimeVal + * @microseconds: number of microseconds to add to @time + * + * Adds the given number of microseconds to @time. @microseconds can + * also be negative to decrease the value of @time. + **/ +void +g_time_val_add (GTimeVal *time, glong microseconds) +{ + g_return_if_fail (time->tv_usec >= 0 && time->tv_usec < G_USEC_PER_SEC); + + if (microseconds >= 0) + { + time->tv_usec += microseconds % G_USEC_PER_SEC; + time->tv_sec += microseconds / G_USEC_PER_SEC; + if (time->tv_usec >= G_USEC_PER_SEC) + { + time->tv_usec -= G_USEC_PER_SEC; + time->tv_sec++; + } + } + else + { + microseconds *= -1; + time->tv_usec -= microseconds % G_USEC_PER_SEC; + time->tv_sec -= microseconds / G_USEC_PER_SEC; + if (time->tv_usec < 0) + { + time->tv_usec += G_USEC_PER_SEC; + time->tv_sec--; + } + } +} diff --git a/gtimer.h b/gtimer.h index 533a778f1..823d63442 100644 --- a/gtimer.h +++ b/gtimer.h @@ -40,13 +40,17 @@ typedef struct _GTimer GTimer; #define G_USEC_PER_SEC 1000000 GTimer* g_timer_new (void); -void g_timer_destroy (GTimer *timer); -void g_timer_start (GTimer *timer); -void g_timer_stop (GTimer *timer); -void g_timer_reset (GTimer *timer); -gdouble g_timer_elapsed (GTimer *timer, - gulong *microseconds); -void g_usleep (gulong microseconds); +void g_timer_destroy (GTimer *timer); +void g_timer_start (GTimer *timer); +void g_timer_stop (GTimer *timer); +void g_timer_reset (GTimer *timer); +gdouble g_timer_elapsed (GTimer *timer, + gulong *microseconds); + +void g_usleep (gulong microseconds); + +void g_time_val_add (GTimeVal *time, + glong microseconds); G_END_DECLS