Commit 541462ab authored by Matthias Clasen's avatar Matthias Clasen Committed by Matthias Clasen

Don't link glib against libpthread. (#393812)

2007-01-07  Matthias Clasen  <mclasen@redhat.com>

	Don't link glib against libpthread.  (#393812)
	
	* configure.in: Link gthread against librt, not glib itself.

	* glib/gthread.h:
	* glib/gthread.c: Add a new thread function, gettime.

	* glib/gtimer.c: Use gettime instead of directly working with
	the various system interfaces.

	* gthread/gthread-impl.c:
	* gthread/gthread-posix.c:
	* gthread/gthread-win32.c: Implement gettime.
	

svn path=/trunk/; revision=5227
parent 8a688f12
2007-01-07 Matthias Clasen <mclasen@redhat.com>
Don't link glib against libpthread. (#393812)
* configure.in: Link gthread against librt, not glib itself.
* glib/gthread.h:
* glib/gthread.c: Add a new thread function, gettime.
* glib/gtimer.c: Use gettime instead of directly working with
the various system interfaces.
* gthread/gthread-impl.c:
* gthread/gthread-posix.c:
* gthread/gthread-win32.c: Implement gettime.
2007-01-07 Matthias Clasen <mclasen@redhat.com> 2007-01-07 Matthias Clasen <mclasen@redhat.com>
* m4macros/glib-2.0.m4: Use PKG_PROG_PKG_CONFIG. (#392636, * m4macros/glib-2.0.m4: Use PKG_PROG_PKG_CONFIG. (#392636,
......
...@@ -1984,7 +1984,7 @@ fi ...@@ -1984,7 +1984,7 @@ fi
AC_CHECK_FUNCS(clock_gettime, [], [ AC_CHECK_FUNCS(clock_gettime, [], [
AC_CHECK_LIB(rt, clock_gettime, [ AC_CHECK_LIB(rt, clock_gettime, [
AC_DEFINE(HAVE_CLOCK_GETTIME, 1) AC_DEFINE(HAVE_CLOCK_GETTIME, 1)
LIBS="$LIBS -lrt" G_THREAD_LIBS="$G_THREAD_LIBS -lrt"
]) ])
]) ])
......
...@@ -38,6 +38,11 @@ ...@@ -38,6 +38,11 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifndef G_OS_WIN32
#include <sys/time.h>
#include <time.h>
#endif /* G_OS_WIN32 */
#include <string.h> #include <string.h>
#include "glib.h" #include "glib.h"
...@@ -70,6 +75,7 @@ struct _GStaticPrivateNode ...@@ -70,6 +75,7 @@ struct _GStaticPrivateNode
static void g_thread_cleanup (gpointer data); static void g_thread_cleanup (gpointer data);
static void g_thread_fail (void); static void g_thread_fail (void);
static guint64 gettime (void);
/* Global variables */ /* Global variables */
...@@ -99,7 +105,9 @@ GThreadFunctions g_thread_functions_for_glib_use = { ...@@ -99,7 +105,9 @@ GThreadFunctions g_thread_functions_for_glib_use = {
NULL, /* thread_join */ NULL, /* thread_join */
NULL, /* thread_exit */ NULL, /* thread_exit */
NULL, /* thread_set_priority */ NULL, /* thread_set_priority */
NULL /* thread_self */ NULL, /* thread_self */
NULL, /* thread_equal */
gettime /* gettime */
}; };
/* Local data */ /* Local data */
...@@ -535,6 +543,24 @@ g_thread_fail (void) ...@@ -535,6 +543,24 @@ g_thread_fail (void)
g_error ("The thread system is not yet initialized."); g_error ("The thread system is not yet initialized.");
} }
static guint64
gettime (void)
{
#ifdef G_OS_WIN32
guint64 v;
GetSystemTimeAsFileTime ((FILETIME *)&v);
return v;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec * 1e9 + tv.tv_usec * 1000;
#endif
}
static gpointer static gpointer
g_thread_create_proxy (gpointer data) g_thread_create_proxy (gpointer data)
{ {
......
...@@ -106,6 +106,7 @@ struct _GThreadFunctions ...@@ -106,6 +106,7 @@ struct _GThreadFunctions
void (*thread_self) (gpointer thread); void (*thread_self) (gpointer thread);
gboolean (*thread_equal) (gpointer thread1, gboolean (*thread_equal) (gpointer thread1,
gpointer thread2); gpointer thread2);
guint64 (*gettime) (void);
}; };
GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use; GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
......
...@@ -48,60 +48,21 @@ ...@@ -48,60 +48,21 @@
#endif /* G_OS_WIN32 */ #endif /* G_OS_WIN32 */
#include "glib.h" #include "glib.h"
#include "gthread.h"
#include "galias.h" #include "galias.h"
#define G_NSEC_PER_SEC 1000000000 #define G_NSEC_PER_SEC 1000000000
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK) #define GETTIME(v) (v = G_THREAD_UF (gettime, ()))
#define USE_CLOCK_GETTIME 1
#endif
struct _GTimer struct _GTimer
{ {
#ifdef G_OS_WIN32
guint64 start; guint64 start;
guint64 end; guint64 end;
#elif USE_CLOCK_GETTIME
struct timespec start;
struct timespec end;
gint clock;
#else /* uses gettimeofday */
struct timeval start;
struct timeval end;
#endif
guint active : 1; guint active : 1;
}; };
#ifdef G_OS_WIN32
# define GETTIME(v) \
GetSystemTimeAsFileTime ((FILETIME *)&v)
#elif USE_CLOCK_GETTIME
# define GETTIME(v) \
clock_gettime (posix_clock, &v)
#else
# define GETTIME(v) \
gettimeofday (&v, NULL)
#endif
#ifdef USE_CLOCK_GETTIME
static gint posix_clock = 0;
static void
init_posix_clock (void)
{
static gboolean initialized = FALSE;
if (!initialized)
{
initialized = TRUE;
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
posix_clock = CLOCK_MONOTONIC;
else
posix_clock = CLOCK_REALTIME;
}
}
#endif
GTimer* GTimer*
g_timer_new (void) g_timer_new (void)
...@@ -111,10 +72,6 @@ g_timer_new (void) ...@@ -111,10 +72,6 @@ g_timer_new (void)
timer = g_new (GTimer, 1); timer = g_new (GTimer, 1);
timer->active = TRUE; timer->active = TRUE;
#ifdef USE_CLOCK_GETTIME
init_posix_clock ();
#endif
GETTIME (timer->start); GETTIME (timer->start);
return timer; return timer;
...@@ -145,7 +102,7 @@ g_timer_stop (GTimer *timer) ...@@ -145,7 +102,7 @@ g_timer_stop (GTimer *timer)
timer->active = FALSE; timer->active = FALSE;
GETTIME(timer->end); GETTIME (timer->end);
} }
void void
...@@ -159,13 +116,7 @@ g_timer_reset (GTimer *timer) ...@@ -159,13 +116,7 @@ g_timer_reset (GTimer *timer)
void void
g_timer_continue (GTimer *timer) g_timer_continue (GTimer *timer)
{ {
#ifdef G_OS_WIN32
guint64 elapsed; guint64 elapsed;
#elif USE_CLOCK_GETTIME
struct timespec elapsed;
#else
struct timeval elapsed;
#endif
g_return_if_fail (timer != NULL); g_return_if_fail (timer != NULL);
g_return_if_fail (timer->active == FALSE); g_return_if_fail (timer->active == FALSE);
...@@ -175,60 +126,12 @@ g_timer_continue (GTimer *timer) ...@@ -175,60 +126,12 @@ g_timer_continue (GTimer *timer)
* elapsed interval. * elapsed interval.
*/ */
#ifdef G_OS_WIN32
elapsed = timer->end - timer->start; elapsed = timer->end - timer->start;
GETTIME (timer->start); GETTIME (timer->start);
timer->start -= elapsed; timer->start -= elapsed;
#elif USE_CLOCK_GETTIME
if (timer->start.tv_nsec > timer->end.tv_nsec)
{
timer->end.tv_nsec += G_NSEC_PER_SEC;
timer->end.tv_sec--;
}
elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
GETTIME (timer->start);
if (timer->start.tv_nsec < elapsed.tv_nsec)
{
timer->start.tv_nsec += G_NSEC_PER_SEC;
timer->start.tv_sec--;
}
timer->start.tv_nsec -= elapsed.tv_nsec;
timer->start.tv_sec -= elapsed.tv_sec;
#else
if (timer->start.tv_usec > timer->end.tv_usec)
{
timer->end.tv_usec += G_USEC_PER_SEC;
timer->end.tv_sec--;
}
elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
GETTIME (timer->start);
if (timer->start.tv_usec < elapsed.tv_usec)
{
timer->start.tv_usec += G_USEC_PER_SEC;
timer->start.tv_sec--;
}
timer->start.tv_usec -= elapsed.tv_usec;
timer->start.tv_sec -= elapsed.tv_sec;
#endif /* !G_OS_WIN32 */
timer->active = TRUE; timer->active = TRUE;
} }
...@@ -237,17 +140,10 @@ g_timer_elapsed (GTimer *timer, ...@@ -237,17 +140,10 @@ g_timer_elapsed (GTimer *timer,
gulong *microseconds) gulong *microseconds)
{ {
gdouble total; gdouble total;
#ifdef G_OS_WIN32
gint64 elapsed; gint64 elapsed;
#elif USE_CLOCK_GETTIME
struct timespec elapsed;
#else
struct timeval elapsed;
#endif
g_return_val_if_fail (timer != NULL, 0); g_return_val_if_fail (timer != NULL, 0);
#ifdef G_OS_WIN32
if (timer->active) if (timer->active)
GETTIME (timer->end); GETTIME (timer->end);
...@@ -257,55 +153,6 @@ g_timer_elapsed (GTimer *timer, ...@@ -257,55 +153,6 @@ g_timer_elapsed (GTimer *timer,
if (microseconds) if (microseconds)
*microseconds = (elapsed / 10) % 1000000; *microseconds = (elapsed / 10) % 1000000;
#elif USE_CLOCK_GETTIME
if (timer->active)
GETTIME (timer->end);
if (timer->start.tv_nsec > timer->end.tv_nsec)
{
timer->end.tv_nsec += G_NSEC_PER_SEC;
timer->end.tv_sec--;
}
elapsed.tv_nsec = timer->end.tv_nsec - timer->start.tv_nsec;
elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
total = elapsed.tv_sec + ((gdouble) elapsed.tv_nsec / (gdouble) G_NSEC_PER_SEC);
if (total < 0)
{
total = 0;
if (microseconds)
*microseconds = 0;
}
else if (microseconds)
*microseconds = elapsed.tv_nsec / 1000;
#else
if (timer->active)
GETTIME (timer->end);
if (timer->start.tv_usec > timer->end.tv_usec)
{
timer->end.tv_usec += G_USEC_PER_SEC;
timer->end.tv_sec--;
}
elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / (gdouble) G_USEC_PER_SEC);
if (total < 0)
{
total = 0;
if (microseconds)
*microseconds = 0;
}
else if (microseconds)
*microseconds = elapsed.tv_usec;
#endif
return total; return total;
} }
......
...@@ -332,7 +332,8 @@ g_thread_init (GThreadFunctions* init) ...@@ -332,7 +332,8 @@ g_thread_init (GThreadFunctions* init)
init->thread_join && init->thread_join &&
init->thread_exit && init->thread_exit &&
init->thread_set_priority && init->thread_set_priority &&
init->thread_self); init->thread_self &&
init->gettime);
/* if somebody is calling g_thread_init (), it means that he wants to /* if somebody is calling g_thread_init (), it means that he wants to
* have thread support, so check this * have thread support, so check this
......
...@@ -119,7 +119,12 @@ static gulong g_thread_min_stack_size = 0; ...@@ -119,7 +119,12 @@ static gulong g_thread_min_stack_size = 0;
#define G_MUTEX_SIZE (sizeof (pthread_mutex_t)) #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
#if defined(_SC_THREAD_STACK_MIN) || defined (HAVE_PRIORITIES) static gint posix_clock = 0;
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
#define USE_CLOCK_GETTIME 1
#endif
#if defined(_SC_THREAD_STACK_MIN) || defined (HAVE_PRIORITIES) || defined (USE_CLOCK_GETTIME)
#define HAVE_G_THREAD_IMPL_INIT #define HAVE_G_THREAD_IMPL_INIT
static void static void
g_thread_impl_init(void) g_thread_impl_init(void)
...@@ -142,6 +147,10 @@ g_thread_impl_init(void) ...@@ -142,6 +147,10 @@ g_thread_impl_init(void)
# endif # endif
#endif /* HAVE_PRIORITIES */ #endif /* HAVE_PRIORITIES */
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
posix_clock = CLOCK_MONOTONIC;
else
posix_clock = CLOCK_REALTIME;
} }
#endif /* _SC_THREAD_STACK_MIN || HAVE_PRIORITIES */ #endif /* _SC_THREAD_STACK_MIN || HAVE_PRIORITIES */
...@@ -415,6 +424,24 @@ g_thread_equal_posix_impl (gpointer thread1, gpointer thread2) ...@@ -415,6 +424,24 @@ g_thread_equal_posix_impl (gpointer thread1, gpointer thread2)
return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0); return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
} }
static guint64
g_gettime_posix_impl (void)
{
#ifdef USE_CLOCK_GETTIME
struct timespec tv;
clock_gettime (posix_clock, &tv);
return tv.tv_sec * 1e9 + tv.tv_nsec;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec * 1e9 + tv.tv_usec * 1000;
#endif
}
static GThreadFunctions g_thread_functions_for_glib_use_default = static GThreadFunctions g_thread_functions_for_glib_use_default =
{ {
g_mutex_new_posix_impl, g_mutex_new_posix_impl,
...@@ -437,5 +464,6 @@ static GThreadFunctions g_thread_functions_for_glib_use_default = ...@@ -437,5 +464,6 @@ static GThreadFunctions g_thread_functions_for_glib_use_default =
g_thread_exit_posix_impl, g_thread_exit_posix_impl,
g_thread_set_priority_posix_impl, g_thread_set_priority_posix_impl,
g_thread_self_posix_impl, g_thread_self_posix_impl,
g_thread_equal_posix_impl g_thread_equal_posix_impl,
g_gettime_posix_impl
}; };
...@@ -545,6 +545,16 @@ g_thread_join_win32_impl (gpointer thread) ...@@ -545,6 +545,16 @@ g_thread_join_win32_impl (gpointer thread)
g_free (target); g_free (target);
} }
static guint64
g_gettime_win32_impl (void)
{
guint64 v;
GetSystemTimeAsFileTime ((FILETIME *)&v);
return v;
}
static GThreadFunctions g_thread_functions_for_glib_use_default = static GThreadFunctions g_thread_functions_for_glib_use_default =
{ {
g_mutex_new_win32_impl, /* mutex */ g_mutex_new_win32_impl, /* mutex */
...@@ -567,7 +577,8 @@ static GThreadFunctions g_thread_functions_for_glib_use_default = ...@@ -567,7 +577,8 @@ static GThreadFunctions g_thread_functions_for_glib_use_default =
g_thread_exit_win32_impl, g_thread_exit_win32_impl,
g_thread_set_priority_win32_impl, g_thread_set_priority_win32_impl,
g_thread_self_win32_impl, g_thread_self_win32_impl,
NULL /* no equal function necessary */ NULL, /* no equal function necessary */
g_gettime_win32_impl
}; };
#define HAVE_G_THREAD_IMPL_INIT #define HAVE_G_THREAD_IMPL_INIT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment