From ac4abbba3eacb7f4000ae5f800ac5efab36bc2c3 Mon Sep 17 00:00:00 2001 From: Sebastian Wilhelmi Date: Thu, 23 Mar 2000 16:23:50 +0000 Subject: [PATCH] After finding the right thread library (containing e.g. pthread_create) we 2000-03-23 Sebastian Wilhelmi * configure.in: After finding the right thread library (containing e.g. pthread_create) we now search for the right realtime library (containing e.g. sched_get_priority_max). Makes the output of the thread related libraries correct. * gtimer.c (g_usleep): The current implementation of g_usleep (simply calling select) doesn't work reliable for multi-threaded programs on some platforms (bad omen for the main loop....), so I changed the implementation for thread-using programs to wait for a GCond for the specified amount of time (NB: sleep and usleep are not MT-safe in general, because they often use signals). --- ChangeLog | 14 ++++++++++++++ ChangeLog.pre-2-0 | 14 ++++++++++++++ ChangeLog.pre-2-10 | 14 ++++++++++++++ ChangeLog.pre-2-12 | 14 ++++++++++++++ ChangeLog.pre-2-2 | 14 ++++++++++++++ ChangeLog.pre-2-4 | 14 ++++++++++++++ ChangeLog.pre-2-6 | 14 ++++++++++++++ ChangeLog.pre-2-8 | 14 ++++++++++++++ configure.in | 52 ++++++++++++++++++++++++++++------------------------ glib/gtimer.c | 39 +++++++++++++++++++++++++++++++++++---- gtimer.c | 39 +++++++++++++++++++++++++++++++++++---- 11 files changed, 210 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index c38b90e..01eee21 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index c38b90e..01eee21 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,17 @@ +2000-03-23 Sebastian Wilhelmi + + * configure.in: After finding the right thread library (containing + e.g. pthread_create) we now search for the right realtime library + (containing e.g. sched_get_priority_max). Makes the output of the + thread related libraries correct. + + * gtimer.c (g_usleep): The current implementation of g_usleep + (simply calling select) doesn't work reliable for multi-threaded + programs on some platforms (bad omen for the main loop....), so I + changed the implementation for thread-using programs to wait for a + GCond for the specified amount of time (NB: sleep and usleep are + not MT-safe in general, because they often use signals). + Wed Mar 22 16:49:57 2000 Owen Taylor * gmem.c (g_mem_chunk_area_compare): Fix indentation. diff --git a/configure.in b/configure.in index 7531b43..b1a074a 100644 --- a/configure.in +++ b/configure.in @@ -733,8 +733,6 @@ case $have_threads in G_THREAD_LIBS=error glib_save_LIBS="$LIBS" for thread_lib in "" pthread pthreads c_r thread dce; do - # This is not AC_CHECK_LIB to also work with function - # name mangling in header files. if test x"$thread_lib" = x; then add_thread_lib="" IN="" @@ -763,6 +761,31 @@ case $have_threads in break], [AC_MSG_RESULT(no)]) done + for thread_lib in "" rt; do + if test x"$thread_lib" = x; then + add_thread_lib="" + IN="" + else + add_thread_lib="-l$thread_lib" + IN=" in -l$thread_lib" + fi + LIBS="$glib_save_LIBS $add_thread_lib" + + AC_MSG_CHECKING(for sched_get_priority_min$IN) + AC_TRY_RUN([#include + #include + int main() { + errno = 0; + return sched_get_priority_min(SCHED_OTHER)==-1 + && errno != 0; + }], + [AC_MSG_RESULT(yes) + G_THREAD_LIBS="$G_THREAD_LIBS $add_thread_lib" + posix_priority_min="sched_get_priority_min(SCHED_OTHER)" + posix_priority_max="sched_get_priority_max(SCHED_OTHER)" + break], + [AC_MSG_RESULT(no)]) + done LIBS="$glib_save_LIBS" mutex_has_default=yes mutex_default_type='pthread_mutex_t' @@ -884,8 +907,8 @@ if test x"$enable_threads" = xyes; then # The signature for the POSIX version is: # int getpwuid_r(uid_t, struct passwd *, char *, size_t, struct passwd **) AC_TRY_COMPILE([#include - #include - #include ], + #include + #include ], [getpwuid_r((uid_t)0, NULL, NULL, (size_t)0, NULL);], [AC_DEFINE(HAVE_GETPWUID_R_POSIX) AC_MSG_RESULT(yes)], @@ -907,26 +930,7 @@ if test x"$enable_threads" = xyes; then [AC_MSG_RESULT(yes) AC_DEFINE(HAVE_PTHREAD_ATTR_SETSTACKSIZE)], [AC_MSG_RESULT(no)]) - # If sched_get_priority_min(SCHED_OTHER) returns something - # negative, we ignore it. AC_MSG_CHECKING(for minimal/maximal thread priority) - AC_TRY_RUN([#include - int main () - { return sched_get_priority_min(SCHED_OTHER) == -1;}], - [posix_priority_min="sched_get_priority_min(SCHED_OTHER)" - posix_priority_max="sched_get_priority_max(SCHED_OTHER)"], - [posix_priority_min=none]) - # On some platforms we need to link to -lrt to use the - # sched_* functions - LIBS="$LIBS -lrt" - AC_TRY_RUN([#include - int main () - { return sched_get_priority_min(SCHED_OTHER) == -1;}], - [posix_priority_min="sched_get_priority_min(SCHED_OTHER)" - posix_priority_max="sched_get_priority_max(SCHED_OTHER)" - G_THREAD_LIBS="$G_THREAD_LIBS -lrt"], - [posix_priority_min=none]) - LIBS="$glib_save_LIBS $G_THREAD_LIBS" if test x"$posix_priority_min" = xnone; then AC_EGREP_CPP(PX_PRIO_MIN,[#include PX_PRIO_MIN],,[ @@ -939,7 +943,7 @@ if test x"$enable_threads" = xyes; then posix_priority_min=PRI_OTHER_MIN posix_priority_max=PRI_OTHER_MAX]) fi - if test x"$posix_priority_min" = xnone; then + if test x"$posix_priority_min" = x; then AC_MSG_RESULT(none found) AC_MSG_WARN($POSIX_NO_PRIORITIES) posix_priority_min=-1 diff --git a/glib/gtimer.c b/glib/gtimer.c index d613a50..4d68b77 100644 --- a/glib/gtimer.c +++ b/glib/gtimer.c @@ -205,10 +205,41 @@ g_usleep (gulong microseconds) #ifdef G_OS_WIN32 Sleep (microseconds / 1000); #else - struct timeval tv; - tv.tv_sec = microseconds / G_MICROSEC; - tv.tv_usec = microseconds % G_MICROSEC; - select(0, NULL, NULL, NULL, &tv); + if (g_thread_supported ()) + { + static GStaticMutex mutex = G_STATIC_MUTEX_INIT; + static GCond* cond = NULL; + GTimeVal end_time; + + g_get_current_time (&end_time); + + end_time.tv_sec += microseconds / G_MICROSEC; + end_time.tv_usec += microseconds % G_MICROSEC; + + if (end_time.tv_usec >= G_MICROSEC) + { + end_time.tv_usec -= G_MICROSEC; + end_time.tv_sec += 1; + } + + g_static_mutex_lock (&mutex); + + if (!cond) + cond = g_cond_new (); + + while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), + &end_time)) + /* do nothing */; + + g_static_mutex_unlock (&mutex); + } + else + { + struct timeval tv; + tv.tv_sec = microseconds / G_MICROSEC; + tv.tv_usec = microseconds % G_MICROSEC; + select(0, NULL, NULL, NULL, &tv); + } #endif } diff --git a/gtimer.c b/gtimer.c index d613a50..4d68b77 100644 --- a/gtimer.c +++ b/gtimer.c @@ -205,10 +205,41 @@ g_usleep (gulong microseconds) #ifdef G_OS_WIN32 Sleep (microseconds / 1000); #else - struct timeval tv; - tv.tv_sec = microseconds / G_MICROSEC; - tv.tv_usec = microseconds % G_MICROSEC; - select(0, NULL, NULL, NULL, &tv); + if (g_thread_supported ()) + { + static GStaticMutex mutex = G_STATIC_MUTEX_INIT; + static GCond* cond = NULL; + GTimeVal end_time; + + g_get_current_time (&end_time); + + end_time.tv_sec += microseconds / G_MICROSEC; + end_time.tv_usec += microseconds % G_MICROSEC; + + if (end_time.tv_usec >= G_MICROSEC) + { + end_time.tv_usec -= G_MICROSEC; + end_time.tv_sec += 1; + } + + g_static_mutex_lock (&mutex); + + if (!cond) + cond = g_cond_new (); + + while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), + &end_time)) + /* do nothing */; + + g_static_mutex_unlock (&mutex); + } + else + { + struct timeval tv; + tv.tv_sec = microseconds / G_MICROSEC; + tv.tv_usec = microseconds % G_MICROSEC; + select(0, NULL, NULL, NULL, &tv); + } #endif } -- 2.7.4