1998-12-15 06:28:02 +01:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* gthread.c: posix thread system implementation
|
|
|
|
* Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
1999-02-24 07:14:27 +01:00
|
|
|
/*
|
|
|
|
* Modified by the GLib Team and others 1997-1999. See the AUTHORS
|
|
|
|
* file for a list of people on the GLib Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
1998-12-15 06:28:02 +01:00
|
|
|
/*
|
|
|
|
* MT safe
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
Merge in current Win32 version. Almost no Unix code touched.
* README.win32: More text.
* config.h.win32 glibconfig.h.win32: Update to match the
corresponding generated files on Unix.
* makefile.msc: Update with new source files, and gthread
library. Use the compiler flag -MD instead of using -D_DLL and
"/nodefaultlib:libc msvcrt.lib" in the link phase.
* glib.def: Include new functions, drop removed ones.
* glib.h: Add comments about main loop and polling on Win32. (In
general, it's only for the GIMP's use.) Add Win32 IO Channel
functions. Remove the obsoleted old IO Channel stuff (which was
in #if 0 already).
* giowin32.c: New file.
* gmain.c: Include config.h, conditionalize <sys/time.h>
inclusion. Add g_poll implementation for Win32 (only for the
GIMP's needs for now, it's hard or even impossible to be as clean
and generic as on Unix). Implement g_get_current_time on Win32. If
threads aren't supported, don't try to wake up main thread's
loop. On Win32, use a semaphore and not a pipe to wake up the main
loop.
* gmessages.c: On Win32, allocate a console window if the standard
output handle is invalid before writing to stdout, and reopen stdout
to that console window.
* giochannel.c: Conditionalize unistd.h inclusion. Some indentation
cleanup.
* gstrfuncs.c: Include <signal.h>.
* gutils.c: On Win32, also check the HOMEDRIVE and HOMEPATH
environment variables.
* gmodule-dl.c gmodule-dld.c: In
_g_module_build_path, don't add the "lib" prefix and
".so" or ".sl" suffix if already there.
* gmodule-win32.c: Likewise for the ".dll" suffix.
* gthread-posix.c: Conditionalize <sys/time.h> inclusion.
1999-01-17 00:46:42 +01:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
1998-12-15 06:28:02 +01:00
|
|
|
#include <sys/time.h>
|
Merge in current Win32 version. Almost no Unix code touched.
* README.win32: More text.
* config.h.win32 glibconfig.h.win32: Update to match the
corresponding generated files on Unix.
* makefile.msc: Update with new source files, and gthread
library. Use the compiler flag -MD instead of using -D_DLL and
"/nodefaultlib:libc msvcrt.lib" in the link phase.
* glib.def: Include new functions, drop removed ones.
* glib.h: Add comments about main loop and polling on Win32. (In
general, it's only for the GIMP's use.) Add Win32 IO Channel
functions. Remove the obsoleted old IO Channel stuff (which was
in #if 0 already).
* giowin32.c: New file.
* gmain.c: Include config.h, conditionalize <sys/time.h>
inclusion. Add g_poll implementation for Win32 (only for the
GIMP's needs for now, it's hard or even impossible to be as clean
and generic as on Unix). Implement g_get_current_time on Win32. If
threads aren't supported, don't try to wake up main thread's
loop. On Win32, use a semaphore and not a pipe to wake up the main
loop.
* gmessages.c: On Win32, allocate a console window if the standard
output handle is invalid before writing to stdout, and reopen stdout
to that console window.
* giochannel.c: Conditionalize unistd.h inclusion. Some indentation
cleanup.
* gstrfuncs.c: Include <signal.h>.
* gutils.c: On Win32, also check the HOMEDRIVE and HOMEPATH
environment variables.
* gmodule-dl.c gmodule-dld.c: In
_g_module_build_path, don't add the "lib" prefix and
".so" or ".sl" suffix if already there.
* gmodule-win32.c: Likewise for the ".dll" suffix.
* gthread-posix.c: Conditionalize <sys/time.h> inclusion.
1999-01-17 00:46:42 +01:00
|
|
|
#endif
|
1998-12-15 06:28:02 +01:00
|
|
|
|
|
|
|
#define posix_print_error( name, num ) \
|
|
|
|
g_error( "file %s: line %d (%s): error %s during %s", \
|
|
|
|
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
|
|
|
|
g_strerror((num)), #name )
|
|
|
|
|
1999-03-11 18:38:51 +01:00
|
|
|
#if defined(G_THREADS_IMPL_POSIX)
|
|
|
|
# define posix_check_for_error( what ) G_STMT_START{ \
|
|
|
|
int error = (what); \
|
|
|
|
if( error ) { posix_print_error( what, error ); } \
|
|
|
|
}G_STMT_END
|
1999-03-31 11:28:59 +02:00
|
|
|
# define mutexattr_default NULL
|
|
|
|
# define condattr_default NULL
|
1999-03-11 18:38:51 +01:00
|
|
|
#elif defined(G_THREADS_IMPL_DCE)
|
|
|
|
# define posix_check_for_error( what ) G_STMT_START{ \
|
|
|
|
if( (what) == -1 ) { posix_print_error( what, errno ); } \
|
|
|
|
}G_STMT_END
|
1999-03-31 11:28:59 +02:00
|
|
|
# define mutexattr_default (&pthread_mutexattr_default)
|
|
|
|
# define condattr_default (&pthread_condattr_default)
|
1999-03-11 18:38:51 +01:00
|
|
|
#else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
|
|
|
|
# error This should not happen. Contact the GLb team.
|
|
|
|
#endif
|
1998-12-15 06:28:02 +01:00
|
|
|
|
|
|
|
static GMutex *
|
|
|
|
g_mutex_new_posix_impl (void)
|
|
|
|
{
|
|
|
|
GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
|
1999-03-31 11:28:59 +02:00
|
|
|
posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result,
|
|
|
|
mutexattr_default));
|
1998-12-15 06:28:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_mutex_free_posix_impl (GMutex * mutex)
|
|
|
|
{
|
|
|
|
posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
|
1998-12-17 05:36:04 +01:00
|
|
|
g_free (mutex);
|
1998-12-15 06:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
|
|
|
|
functions from gmem.c and gmessages.c; */
|
|
|
|
|
|
|
|
/* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
|
|
|
|
signature and semantic are right, but without error check then!!!!,
|
|
|
|
we might want to change this therefore. */
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_mutex_trylock_posix_impl (GMutex * mutex)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
|
1999-03-11 18:38:51 +01:00
|
|
|
|
|
|
|
#ifdef G_THREADS_IMPL_POSIX
|
|
|
|
if (result == EBUSY)
|
1998-12-15 06:28:02 +01:00
|
|
|
return FALSE;
|
1999-03-11 18:38:51 +01:00
|
|
|
#else /* G_THREADS_IMPL_DCE */
|
|
|
|
if (result == 0)
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
1998-12-15 06:28:02 +01:00
|
|
|
|
|
|
|
posix_check_for_error (result);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GCond *
|
|
|
|
g_cond_new_posix_impl (void)
|
|
|
|
{
|
|
|
|
GCond *result = (GCond *) g_new (pthread_cond_t, 1);
|
1999-03-31 11:28:59 +02:00
|
|
|
posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result,
|
|
|
|
condattr_default));
|
1998-12-15 06:28:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
|
|
|
|
can be taken directly, as signature and semantic are right, but
|
|
|
|
without error check then!!!!, we might want to change this
|
|
|
|
therfore. */
|
|
|
|
|
|
|
|
#define G_MICROSEC 1000000
|
|
|
|
#define G_NANOSEC 1000000000
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_cond_timed_wait_posix_impl (GCond * cond,
|
|
|
|
GMutex * entered_mutex,
|
|
|
|
GTimeVal * abs_time)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct timespec end_time;
|
|
|
|
gboolean timed_out;
|
|
|
|
|
|
|
|
g_return_val_if_fail (cond != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (entered_mutex != NULL, FALSE);
|
|
|
|
|
|
|
|
if (!abs_time)
|
|
|
|
{
|
|
|
|
g_cond_wait (cond, entered_mutex);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_time.tv_sec = abs_time->tv_sec;
|
|
|
|
end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
|
|
|
|
g_assert (end_time.tv_nsec < G_NANOSEC);
|
|
|
|
result = pthread_cond_timedwait ((pthread_cond_t *) cond,
|
|
|
|
(pthread_mutex_t *) entered_mutex,
|
|
|
|
&end_time);
|
1999-03-11 18:38:51 +01:00
|
|
|
|
|
|
|
#ifdef G_THREADS_IMPL_POSIX
|
1998-12-15 06:28:02 +01:00
|
|
|
timed_out = (result == ETIMEDOUT);
|
1999-03-11 18:38:51 +01:00
|
|
|
#else /* G_THREADS_IMPL_DCE */
|
|
|
|
timed_out = (result == -1) && (errno = EAGAIN);
|
|
|
|
#endif
|
|
|
|
|
1998-12-15 06:28:02 +01:00
|
|
|
if (!timed_out)
|
|
|
|
posix_check_for_error (result);
|
|
|
|
return !timed_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_cond_free_posix_impl (GCond * cond)
|
|
|
|
{
|
|
|
|
posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
|
|
|
|
g_free (cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GPrivate *
|
|
|
|
g_private_new_posix_impl (GDestroyNotify destructor)
|
|
|
|
{
|
|
|
|
GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
|
|
|
|
posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
|
|
|
|
destructor));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: the functions g_private_get and g_private_set may not use
|
|
|
|
functions from gmem.c and gmessages.c */
|
|
|
|
|
|
|
|
static void
|
1998-12-15 18:49:30 +01:00
|
|
|
g_private_set_posix_impl (GPrivate * private_key, gpointer value)
|
1998-12-15 06:28:02 +01:00
|
|
|
{
|
1998-12-15 18:49:30 +01:00
|
|
|
if (!private_key)
|
1998-12-15 06:28:02 +01:00
|
|
|
return;
|
|
|
|
|
1998-12-15 18:49:30 +01:00
|
|
|
pthread_setspecific (*(pthread_key_t *) private_key, value);
|
1998-12-15 06:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
1998-12-15 18:49:30 +01:00
|
|
|
g_private_get_posix_impl (GPrivate * private_key)
|
1998-12-15 06:28:02 +01:00
|
|
|
{
|
1998-12-15 18:49:30 +01:00
|
|
|
if (!private_key)
|
1998-12-15 06:28:02 +01:00
|
|
|
return NULL;
|
1999-03-11 18:38:51 +01:00
|
|
|
#ifdef G_THREADS_IMPL_POSIX
|
1998-12-15 18:49:30 +01:00
|
|
|
return pthread_getspecific (*(pthread_key_t *) private_key);
|
1999-03-11 18:38:51 +01:00
|
|
|
#else /* G_THREADS_IMPL_DCE */
|
1999-02-08 18:14:44 +01:00
|
|
|
{
|
|
|
|
void* data;
|
1999-03-11 18:38:51 +01:00
|
|
|
posix_check_for_error (pthread_getspecific (*(pthread_key_t *)
|
1999-03-12 16:28:36 +01:00
|
|
|
private_key, &data));
|
1999-02-08 18:14:44 +01:00
|
|
|
return data;
|
|
|
|
}
|
1999-03-11 18:38:51 +01:00
|
|
|
#endif
|
1998-12-15 06:28:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static GThreadFunctions g_thread_functions_for_glib_use_default =
|
|
|
|
{
|
|
|
|
g_mutex_new_posix_impl,
|
|
|
|
(void (*)(GMutex *)) pthread_mutex_lock,
|
|
|
|
g_mutex_trylock_posix_impl,
|
|
|
|
(void (*)(GMutex *)) pthread_mutex_unlock,
|
|
|
|
g_mutex_free_posix_impl,
|
|
|
|
g_cond_new_posix_impl,
|
|
|
|
(void (*)(GCond *)) pthread_cond_signal,
|
|
|
|
(void (*)(GCond *)) pthread_cond_broadcast,
|
|
|
|
(void (*)(GCond *, GMutex *)) pthread_cond_wait,
|
|
|
|
g_cond_timed_wait_posix_impl,
|
|
|
|
g_cond_free_posix_impl,
|
|
|
|
g_private_new_posix_impl,
|
|
|
|
g_private_get_posix_impl,
|
|
|
|
g_private_set_posix_impl
|
|
|
|
};
|