win32: enable gthreads
This commit is contained in:
parent
0d36fecf93
commit
f95c64cc6a
|
@ -0,0 +1,372 @@
|
|||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* gthread.c: thread related functions
|
||||
* 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 Lesser 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. 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/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "glib.h"
|
||||
#include "gthreadprivate.h"
|
||||
|
||||
#ifdef G_THREADS_ENABLED
|
||||
|
||||
static GSystemThread zero_thread; /* This is initialized to all zero */
|
||||
static gboolean thread_system_already_initialized = FALSE;
|
||||
static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
|
||||
|
||||
#include G_THREAD_SOURCE
|
||||
|
||||
#ifndef PRIORITY_LOW_VALUE
|
||||
# define PRIORITY_LOW_VALUE 0
|
||||
#endif
|
||||
|
||||
#ifndef PRIORITY_URGENT_VALUE
|
||||
# define PRIORITY_URGENT_VALUE 0
|
||||
#endif
|
||||
|
||||
#ifndef PRIORITY_NORMAL_VALUE
|
||||
# define PRIORITY_NORMAL_VALUE \
|
||||
((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
|
||||
#endif /* PRIORITY_NORMAL_VALUE */
|
||||
|
||||
#ifndef PRIORITY_HIGH_VALUE
|
||||
# define PRIORITY_HIGH_VALUE \
|
||||
((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
|
||||
#endif /* PRIORITY_HIGH_VALUE */
|
||||
|
||||
void g_mutex_init (void);
|
||||
void g_mem_init (void);
|
||||
void g_messages_init (void);
|
||||
void g_convert_init (void);
|
||||
void g_rand_init (void);
|
||||
void g_main_thread_init (void);
|
||||
|
||||
typedef struct _GMutexDebugInfo GMutexDebugInfo;
|
||||
struct _GMutexDebugInfo
|
||||
{
|
||||
gchar *location;
|
||||
GSystemThread owner;
|
||||
};
|
||||
|
||||
#define G_MUTEX_DEBUG_INFO(mutex) \
|
||||
(((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
|
||||
|
||||
static GMutex *
|
||||
g_mutex_new_errorcheck_impl (void)
|
||||
{
|
||||
GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
|
||||
GMutexDebugInfo *info;
|
||||
retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (GMutexDebugInfo));
|
||||
|
||||
info = G_MUTEX_DEBUG_INFO (retval);
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
info->location = "invalid";
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_lock_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
|
||||
GSystemThread self;
|
||||
g_thread_functions_for_glib_use.thread_self (&self);
|
||||
|
||||
if (g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to recursivly lock a mutex at '%s', "
|
||||
"previously locked at '%s'",
|
||||
loc, info->location);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_lock (mutex);
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_mutex_trylock_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
|
||||
GSystemThread self;
|
||||
g_thread_functions_for_glib_use.thread_self (&self);
|
||||
|
||||
if (g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to recursivly lock a mutex at '%s', "
|
||||
"previously locked at '%s'",
|
||||
loc, info->location);
|
||||
|
||||
if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
|
||||
return FALSE;
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_unlock_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
|
||||
GSystemThread self;
|
||||
g_thread_functions_for_glib_use.thread_self (&self);
|
||||
|
||||
if (g_system_thread_equal (info->owner, zero_thread))
|
||||
g_error ("Trying to unlock an unlocked mutex at '%s'", loc);
|
||||
|
||||
if (!g_system_thread_equal (info->owner, self))
|
||||
g_warning ("Trying to unlock a mutex at '%s', "
|
||||
"previously locked by a different thread at '%s'",
|
||||
loc, info->location);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
info->location = NULL;
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_free_errorcheck_impl (GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
|
||||
if (info && !g_system_thread_equal (info->owner, zero_thread))
|
||||
g_error ("Trying to free a locked mutex at '%s', "
|
||||
"which was previously locked at '%s'",
|
||||
loc, info->location);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_free (mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_wait_errorcheck_impl (GCond *cond,
|
||||
GMutex *mutex,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
|
||||
GSystemThread self;
|
||||
g_thread_functions_for_glib_use.thread_self (&self);
|
||||
|
||||
if (g_system_thread_equal (info->owner, zero_thread))
|
||||
g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'", loc);
|
||||
|
||||
if (!g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to use a mutex locked by another thread in "
|
||||
"g_cond_wait() at '%s'", loc);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
loc = info->location;
|
||||
|
||||
g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
g_cond_timed_wait_errorcheck_impl (GCond *cond,
|
||||
GMutex *mutex,
|
||||
GTimeVal *end_time,
|
||||
const gulong magic,
|
||||
gchar * const location)
|
||||
{
|
||||
GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
|
||||
gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
|
||||
gboolean retval;
|
||||
|
||||
GSystemThread self;
|
||||
g_thread_functions_for_glib_use.thread_self (&self);
|
||||
|
||||
if (g_system_thread_equal (info->owner, zero_thread))
|
||||
g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
|
||||
loc);
|
||||
|
||||
if (!g_system_thread_equal (info->owner, self))
|
||||
g_error ("Trying to use a mutex locked by another thread in "
|
||||
"g_cond_timed_wait() at '%s'", loc);
|
||||
|
||||
g_system_thread_assign (info->owner, zero_thread);
|
||||
loc = info->location;
|
||||
|
||||
retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
|
||||
mutex,
|
||||
end_time);
|
||||
|
||||
g_system_thread_assign (info->owner, self);
|
||||
info->location = loc;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/* unshadow function declaration. See gthread.h */
|
||||
#undef g_thread_init
|
||||
|
||||
void
|
||||
g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
|
||||
{
|
||||
GThreadFunctions errorcheck_functions;
|
||||
if (init)
|
||||
g_error ("Errorcheck mutexes can only be used for native "
|
||||
"thread implementations. Sorry." );
|
||||
|
||||
#ifdef HAVE_G_THREAD_IMPL_INIT
|
||||
/* This isn't called in g_thread_init, as it doesn't think to get
|
||||
* the default implementation, so we have to call it on our own.
|
||||
*
|
||||
* We must call this before copying
|
||||
* g_thread_functions_for_glib_use_default as the
|
||||
* implementation-specific init function might modify the contents
|
||||
* of g_thread_functions_for_glib_use_default based on operating
|
||||
* system version, C library version, or whatever. */
|
||||
g_thread_impl_init();
|
||||
#endif /* HAVE_G_THREAD_IMPL_INIT */
|
||||
|
||||
errorcheck_functions = g_thread_functions_for_glib_use_default;
|
||||
errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
|
||||
errorcheck_functions.mutex_lock =
|
||||
(void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
|
||||
errorcheck_functions.mutex_trylock =
|
||||
(gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
|
||||
errorcheck_functions.mutex_unlock =
|
||||
(void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
|
||||
errorcheck_functions.mutex_free =
|
||||
(void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
|
||||
errorcheck_functions.cond_wait =
|
||||
(void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
|
||||
errorcheck_functions.cond_timed_wait =
|
||||
(gboolean (*)(GCond *, GMutex *, GTimeVal *))
|
||||
g_cond_timed_wait_errorcheck_impl;
|
||||
|
||||
g_thread_init (&errorcheck_functions);
|
||||
}
|
||||
|
||||
void
|
||||
g_thread_init (GThreadFunctions* init)
|
||||
{
|
||||
gboolean supported;
|
||||
|
||||
if (thread_system_already_initialized)
|
||||
g_error ("GThread system may only be initialized once.");
|
||||
|
||||
thread_system_already_initialized = TRUE;
|
||||
|
||||
if (init == NULL)
|
||||
{
|
||||
#ifdef HAVE_G_THREAD_IMPL_INIT
|
||||
/* now do any initialization stuff required by the
|
||||
* implementation, but only if called with a NULL argument, of
|
||||
* course. Otherwise it's up to the user to do so. */
|
||||
g_thread_impl_init();
|
||||
#endif /* HAVE_G_THREAD_IMPL_INIT */
|
||||
init = &g_thread_functions_for_glib_use_default;
|
||||
}
|
||||
else
|
||||
g_thread_use_default_impl = FALSE;
|
||||
|
||||
g_thread_functions_for_glib_use = *init;
|
||||
if (g_thread_gettime_impl)
|
||||
g_thread_gettime = g_thread_gettime_impl;
|
||||
|
||||
supported = (init->mutex_new &&
|
||||
init->mutex_lock &&
|
||||
init->mutex_trylock &&
|
||||
init->mutex_unlock &&
|
||||
init->mutex_free &&
|
||||
init->cond_new &&
|
||||
init->cond_signal &&
|
||||
init->cond_broadcast &&
|
||||
init->cond_wait &&
|
||||
init->cond_timed_wait &&
|
||||
init->cond_free &&
|
||||
init->private_new &&
|
||||
init->private_get &&
|
||||
init->private_set &&
|
||||
init->thread_create &&
|
||||
init->thread_yield &&
|
||||
init->thread_join &&
|
||||
init->thread_exit &&
|
||||
init->thread_set_priority &&
|
||||
init->thread_self);
|
||||
|
||||
/* if somebody is calling g_thread_init (), it means that he wants to
|
||||
* have thread support, so check this
|
||||
*/
|
||||
if (!supported)
|
||||
{
|
||||
if (g_thread_use_default_impl)
|
||||
g_error ("Threads are not supported on this platform.");
|
||||
else
|
||||
g_error ("The supplied thread function vector is invalid.");
|
||||
}
|
||||
|
||||
g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
|
||||
g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
|
||||
g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
|
||||
g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
|
||||
|
||||
g_thread_init_glib ();
|
||||
}
|
||||
|
||||
#else /* !G_THREADS_ENABLED */
|
||||
|
||||
void
|
||||
g_thread_init (GThreadFunctions* init)
|
||||
{
|
||||
g_error ("GLib thread support is disabled.");
|
||||
}
|
||||
|
||||
void
|
||||
g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
|
||||
{
|
||||
g_error ("GLib thread support is disabled.");
|
||||
}
|
||||
|
||||
#endif /* !G_THREADS_ENABLED */
|
|
@ -0,0 +1,39 @@
|
|||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* gthread.c: fallback 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 Lesser 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. 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/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
static GThreadFunctions
|
||||
g_thread_functions_for_glib_use_default; /* is NULLified */
|
||||
|
||||
static guint64 (*g_thread_gettime_impl) (void) = NULL;
|
||||
|
||||
#define G_MUTEX_SIZE 0
|
|
@ -0,0 +1,467 @@
|
|||
/* 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 Lesser 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. 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/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
# include <sys/time.h>
|
||||
#endif
|
||||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SCHED_H
|
||||
#include <sched.h>
|
||||
#endif
|
||||
|
||||
#define posix_check_err(err, name) G_STMT_START{ \
|
||||
int error = (err); \
|
||||
if (error) \
|
||||
g_error ("file %s: line %d (%s): error '%s' during '%s'", \
|
||||
__FILE__, __LINE__, G_STRFUNC, \
|
||||
g_strerror (error), name); \
|
||||
}G_STMT_END
|
||||
|
||||
#define posix_check_cmd(cmd) posix_check_err (posix_error (cmd), #cmd)
|
||||
|
||||
#ifdef G_ENABLE_DEBUG
|
||||
static gboolean posix_check_cmd_prio_warned = FALSE;
|
||||
# define posix_check_cmd_prio(cmd) G_STMT_START{ \
|
||||
int err = posix_error (cmd); \
|
||||
if (err == EPERM) \
|
||||
{ \
|
||||
if (!posix_check_cmd_prio_warned) \
|
||||
{ \
|
||||
posix_check_cmd_prio_warned = TRUE; \
|
||||
g_warning ("Priorities can only be changed " \
|
||||
"(resp. increased) by root."); \
|
||||
} \
|
||||
} \
|
||||
else \
|
||||
posix_check_err (err, #cmd); \
|
||||
}G_STMT_END
|
||||
#else /* G_ENABLE_DEBUG */
|
||||
# define posix_check_cmd_prio(cmd) G_STMT_START{ \
|
||||
int err = posix_error (cmd); \
|
||||
if (err != EPERM) \
|
||||
posix_check_err (err, #cmd); \
|
||||
}G_STMT_END
|
||||
#endif /* G_ENABLE_DEBUG */
|
||||
|
||||
#if defined(G_THREADS_IMPL_POSIX)
|
||||
# define posix_error(what) (what)
|
||||
# define mutexattr_default NULL
|
||||
# define condattr_default NULL
|
||||
#elif defined(G_THREADS_IMPL_DCE)
|
||||
# define posix_error(what) ((what) == -1 ? errno : 0)
|
||||
# define pthread_key_create(a, b) pthread_keycreate (a, b)
|
||||
# define pthread_attr_init(a) pthread_attr_create (a)
|
||||
# define pthread_attr_destroy(a) pthread_attr_delete (a)
|
||||
# define pthread_create(a, b, c, d) pthread_create (a, *b, c, d)
|
||||
# define mutexattr_default (pthread_mutexattr_default)
|
||||
# define condattr_default (pthread_condattr_default)
|
||||
#else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
|
||||
# error This should not happen. Contact the GLib team.
|
||||
#endif
|
||||
|
||||
#if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
|
||||
# define HAVE_PRIORITIES 1
|
||||
static gint priority_normal_value;
|
||||
# ifdef __FreeBSD__
|
||||
/* FreeBSD threads use different priority values from the POSIX_
|
||||
* defines so we just set them here. The corresponding macros
|
||||
* PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY are implied to be
|
||||
* exported by the docs, but they aren't.
|
||||
*/
|
||||
# define PRIORITY_LOW_VALUE 0
|
||||
# define PRIORITY_URGENT_VALUE 31
|
||||
# else /* !__FreeBSD__ */
|
||||
# define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY
|
||||
# define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
|
||||
# endif /* !__FreeBSD__ */
|
||||
# define PRIORITY_NORMAL_VALUE priority_normal_value
|
||||
#endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
|
||||
|
||||
static gulong g_thread_min_stack_size = 0;
|
||||
|
||||
#define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
|
||||
|
||||
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
|
||||
#define USE_CLOCK_GETTIME 1
|
||||
static gint posix_clock = 0;
|
||||
#endif
|
||||
|
||||
#if defined(_SC_THREAD_STACK_MIN) || defined (HAVE_PRIORITIES) || defined (USE_CLOCK_GETTIME)
|
||||
#define HAVE_G_THREAD_IMPL_INIT
|
||||
static void
|
||||
g_thread_impl_init(void)
|
||||
{
|
||||
#ifdef _SC_THREAD_STACK_MIN
|
||||
g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
|
||||
#endif /* _SC_THREAD_STACK_MIN */
|
||||
#ifdef HAVE_PRIORITIES
|
||||
# ifdef G_THREADS_IMPL_POSIX
|
||||
{
|
||||
struct sched_param sched;
|
||||
int policy;
|
||||
posix_check_cmd (pthread_getschedparam (pthread_self(), &policy, &sched));
|
||||
priority_normal_value = sched.sched_priority;
|
||||
}
|
||||
# else /* G_THREADS_IMPL_DCE */
|
||||
posix_check_cmd (priority_normal_value =
|
||||
pthread_getprio (*(pthread_t*)thread,
|
||||
g_thread_priority_map [priority]));
|
||||
# endif
|
||||
#endif /* HAVE_PRIORITIES */
|
||||
|
||||
#ifdef USE_CLOCK_GETTIME
|
||||
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
|
||||
posix_clock = CLOCK_MONOTONIC;
|
||||
else
|
||||
posix_clock = CLOCK_REALTIME;
|
||||
#endif
|
||||
}
|
||||
#endif /* _SC_THREAD_STACK_MIN || HAVE_PRIORITIES */
|
||||
|
||||
static GMutex *
|
||||
g_mutex_new_posix_impl (void)
|
||||
{
|
||||
GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
|
||||
posix_check_cmd (pthread_mutex_init ((pthread_mutex_t *) result,
|
||||
mutexattr_default));
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_free_posix_impl (GMutex * mutex)
|
||||
{
|
||||
posix_check_cmd (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
|
||||
g_free (mutex);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
#ifdef G_THREADS_IMPL_POSIX
|
||||
if (result == EBUSY)
|
||||
return FALSE;
|
||||
#else /* G_THREADS_IMPL_DCE */
|
||||
if (result == 0)
|
||||
return FALSE;
|
||||
#endif
|
||||
|
||||
posix_check_err (posix_error (result), "pthread_mutex_trylock");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static GCond *
|
||||
g_cond_new_posix_impl (void)
|
||||
{
|
||||
GCond *result = (GCond *) g_new (pthread_cond_t, 1);
|
||||
posix_check_cmd (pthread_cond_init ((pthread_cond_t *) result,
|
||||
condattr_default));
|
||||
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
|
||||
therefore. */
|
||||
|
||||
#define G_NSEC_PER_SEC 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)
|
||||
{
|
||||
result = pthread_cond_wait ((pthread_cond_t *)cond,
|
||||
(pthread_mutex_t *) entered_mutex);
|
||||
timed_out = FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
end_time.tv_sec = abs_time->tv_sec;
|
||||
end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
|
||||
|
||||
g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE);
|
||||
|
||||
result = pthread_cond_timedwait ((pthread_cond_t *) cond,
|
||||
(pthread_mutex_t *) entered_mutex,
|
||||
&end_time);
|
||||
#ifdef G_THREADS_IMPL_POSIX
|
||||
timed_out = (result == ETIMEDOUT);
|
||||
#else /* G_THREADS_IMPL_DCE */
|
||||
timed_out = (result == -1) && (errno == EAGAIN);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!timed_out)
|
||||
posix_check_err (posix_error (result), "pthread_cond_timedwait");
|
||||
|
||||
return !timed_out;
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_free_posix_impl (GCond * cond)
|
||||
{
|
||||
posix_check_cmd (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_cmd (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
|
||||
g_private_set_posix_impl (GPrivate * private_key, gpointer value)
|
||||
{
|
||||
if (!private_key)
|
||||
return;
|
||||
pthread_setspecific (*(pthread_key_t *) private_key, value);
|
||||
}
|
||||
|
||||
static gpointer
|
||||
g_private_get_posix_impl (GPrivate * private_key)
|
||||
{
|
||||
if (!private_key)
|
||||
return NULL;
|
||||
#ifdef G_THREADS_IMPL_POSIX
|
||||
return pthread_getspecific (*(pthread_key_t *) private_key);
|
||||
#else /* G_THREADS_IMPL_DCE */
|
||||
{
|
||||
void* data;
|
||||
posix_check_cmd (pthread_getspecific (*(pthread_key_t *) private_key,
|
||||
&data));
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_create_posix_impl (GThreadFunc thread_func,
|
||||
gpointer arg,
|
||||
gulong stack_size,
|
||||
gboolean joinable,
|
||||
gboolean bound,
|
||||
GThreadPriority priority,
|
||||
gpointer thread,
|
||||
GError **error)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
gint ret;
|
||||
|
||||
g_return_if_fail (thread_func);
|
||||
g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
|
||||
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
|
||||
|
||||
posix_check_cmd (pthread_attr_init (&attr));
|
||||
|
||||
#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
|
||||
if (stack_size)
|
||||
{
|
||||
stack_size = MAX (g_thread_min_stack_size, stack_size);
|
||||
/* No error check here, because some systems can't do it and
|
||||
* we simply don't want threads to fail because of that. */
|
||||
pthread_attr_setstacksize (&attr, stack_size);
|
||||
}
|
||||
#endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
|
||||
|
||||
#ifdef PTHREAD_SCOPE_SYSTEM
|
||||
if (bound)
|
||||
/* No error check here, because some systems can't do it and we
|
||||
* simply don't want threads to fail because of that. */
|
||||
pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
|
||||
#endif /* PTHREAD_SCOPE_SYSTEM */
|
||||
|
||||
#ifdef G_THREADS_IMPL_POSIX
|
||||
posix_check_cmd (pthread_attr_setdetachstate (&attr,
|
||||
joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
|
||||
#endif /* G_THREADS_IMPL_POSIX */
|
||||
|
||||
#ifdef HAVE_PRIORITIES
|
||||
# ifdef G_THREADS_IMPL_POSIX
|
||||
{
|
||||
struct sched_param sched;
|
||||
posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
|
||||
sched.sched_priority = g_thread_priority_map [priority];
|
||||
posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
|
||||
}
|
||||
# else /* G_THREADS_IMPL_DCE */
|
||||
posix_check_cmd_prio
|
||||
(pthread_attr_setprio (&attr, g_thread_priority_map [priority]));
|
||||
# endif /* G_THREADS_IMPL_DCE */
|
||||
#endif /* HAVE_PRIORITIES */
|
||||
ret = posix_error (pthread_create (thread, &attr,
|
||||
(void* (*)(void*))thread_func, arg));
|
||||
|
||||
posix_check_cmd (pthread_attr_destroy (&attr));
|
||||
|
||||
if (ret == EAGAIN)
|
||||
{
|
||||
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
|
||||
"Error creating thread: %s", g_strerror (ret));
|
||||
return;
|
||||
}
|
||||
|
||||
posix_check_err (ret, "pthread_create");
|
||||
|
||||
#ifdef G_THREADS_IMPL_DCE
|
||||
if (!joinable)
|
||||
posix_check_cmd (pthread_detach (thread));
|
||||
#endif /* G_THREADS_IMPL_DCE */
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_yield_posix_impl (void)
|
||||
{
|
||||
POSIX_YIELD_FUNC;
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_join_posix_impl (gpointer thread)
|
||||
{
|
||||
gpointer ignore;
|
||||
posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore));
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_exit_posix_impl (void)
|
||||
{
|
||||
pthread_exit (NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
|
||||
{
|
||||
g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
|
||||
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
|
||||
#ifdef HAVE_PRIORITIES
|
||||
# ifdef G_THREADS_IMPL_POSIX
|
||||
{
|
||||
struct sched_param sched;
|
||||
int policy;
|
||||
posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
|
||||
&sched));
|
||||
sched.sched_priority = g_thread_priority_map [priority];
|
||||
posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
|
||||
&sched));
|
||||
}
|
||||
# else /* G_THREADS_IMPL_DCE */
|
||||
posix_check_cmd_prio (pthread_setprio (*(pthread_t*)thread,
|
||||
g_thread_priority_map [priority]));
|
||||
# endif
|
||||
#endif /* HAVE_PRIORITIES */
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_self_posix_impl (gpointer thread)
|
||||
{
|
||||
*(pthread_t*)thread = pthread_self();
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_thread_equal_posix_impl (gpointer thread1, gpointer thread2)
|
||||
{
|
||||
return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
|
||||
}
|
||||
|
||||
#ifdef USE_CLOCK_GETTIME
|
||||
static guint64
|
||||
gettime (void)
|
||||
{
|
||||
struct timespec tv;
|
||||
|
||||
clock_gettime (posix_clock, &tv);
|
||||
|
||||
return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_nsec;
|
||||
}
|
||||
static guint64 (*g_thread_gettime_impl)(void) = gettime;
|
||||
#else
|
||||
static guint64 (*g_thread_gettime_impl)(void) = 0;
|
||||
#endif
|
||||
|
||||
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,
|
||||
g_thread_create_posix_impl,
|
||||
g_thread_yield_posix_impl,
|
||||
g_thread_join_posix_impl,
|
||||
g_thread_exit_posix_impl,
|
||||
g_thread_set_priority_posix_impl,
|
||||
g_thread_self_posix_impl,
|
||||
g_thread_equal_posix_impl
|
||||
};
|
|
@ -0,0 +1,639 @@
|
|||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* gthread.c: solaris thread system implementation
|
||||
* Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
|
||||
* Copyright 2001 Hans Breuer
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. 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/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "glib.h"
|
||||
|
||||
#define STRICT
|
||||
#define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
|
||||
#include <windows.h>
|
||||
#undef STRICT
|
||||
|
||||
#include <process.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define win32_check_for_error(what) G_STMT_START{ \
|
||||
if (!(what)) \
|
||||
g_error ("file %s: line %d (%s): error %s during %s", \
|
||||
__FILE__, __LINE__, G_STRFUNC, \
|
||||
g_win32_error_message (GetLastError ()), #what); \
|
||||
}G_STMT_END
|
||||
|
||||
#define G_MUTEX_SIZE (sizeof (gpointer))
|
||||
|
||||
#define PRIORITY_LOW_VALUE THREAD_PRIORITY_BELOW_NORMAL
|
||||
#define PRIORITY_NORMAL_VALUE THREAD_PRIORITY_NORMAL
|
||||
#define PRIORITY_HIGH_VALUE THREAD_PRIORITY_ABOVE_NORMAL
|
||||
#define PRIORITY_URGENT_VALUE THREAD_PRIORITY_HIGHEST
|
||||
|
||||
static DWORD g_thread_self_tls;
|
||||
static DWORD g_private_tls;
|
||||
static DWORD g_cond_event_tls;
|
||||
static CRITICAL_SECTION g_thread_global_spinlock;
|
||||
|
||||
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
|
||||
|
||||
static GTryEnterCriticalSectionFunc try_enter_critical_section = NULL;
|
||||
|
||||
/* As noted in the docs, GPrivate is a limited resource, here we take
|
||||
* a rather low maximum to save memory, use GStaticPrivate instead. */
|
||||
#define G_PRIVATE_MAX 100
|
||||
|
||||
static GDestroyNotify g_private_destructors[G_PRIVATE_MAX];
|
||||
|
||||
static guint g_private_next = 0;
|
||||
|
||||
/* A "forward" declaration of this structure */
|
||||
static GThreadFunctions g_thread_functions_for_glib_use_default;
|
||||
|
||||
typedef struct _GThreadData GThreadData;
|
||||
struct _GThreadData
|
||||
{
|
||||
GThreadFunc func;
|
||||
gpointer data;
|
||||
HANDLE thread;
|
||||
gboolean joinable;
|
||||
};
|
||||
|
||||
struct _GCond
|
||||
{
|
||||
GPtrArray *array;
|
||||
CRITICAL_SECTION lock;
|
||||
};
|
||||
|
||||
static GMutex *
|
||||
g_mutex_new_win32_cs_impl (void)
|
||||
{
|
||||
CRITICAL_SECTION *cs = g_new (CRITICAL_SECTION, 1);
|
||||
gpointer *retval = g_new (gpointer, 1);
|
||||
|
||||
InitializeCriticalSection (cs);
|
||||
*retval = cs;
|
||||
return (GMutex *) retval;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_free_win32_cs_impl (GMutex *mutex)
|
||||
{
|
||||
gpointer *ptr = (gpointer *) mutex;
|
||||
CRITICAL_SECTION *cs = (CRITICAL_SECTION *) *ptr;
|
||||
|
||||
DeleteCriticalSection (cs);
|
||||
g_free (cs);
|
||||
g_free (mutex);
|
||||
}
|
||||
|
||||
/* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
|
||||
functions from gmem.c and gmessages.c; */
|
||||
|
||||
static void
|
||||
g_mutex_lock_win32_cs_impl (GMutex *mutex)
|
||||
{
|
||||
EnterCriticalSection (*(CRITICAL_SECTION **)mutex);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_mutex_trylock_win32_cs_impl (GMutex * mutex)
|
||||
{
|
||||
return try_enter_critical_section (*(CRITICAL_SECTION **)mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_unlock_win32_cs_impl (GMutex *mutex)
|
||||
{
|
||||
LeaveCriticalSection (*(CRITICAL_SECTION **)mutex);
|
||||
}
|
||||
|
||||
static GMutex *
|
||||
g_mutex_new_win32_impl (void)
|
||||
{
|
||||
HANDLE handle;
|
||||
HANDLE *retval;
|
||||
win32_check_for_error (handle = CreateMutex (NULL, FALSE, NULL));
|
||||
retval = g_new (HANDLE, 1);
|
||||
*retval = handle;
|
||||
return (GMutex *) retval;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_free_win32_impl (GMutex *mutex)
|
||||
{
|
||||
win32_check_for_error (CloseHandle (*(HANDLE *) mutex));
|
||||
g_free (mutex);
|
||||
}
|
||||
|
||||
/* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
|
||||
functions from gmem.c and gmessages.c; */
|
||||
|
||||
static void
|
||||
g_mutex_lock_win32_impl (GMutex *mutex)
|
||||
{
|
||||
WaitForSingleObject (*(HANDLE *) mutex, INFINITE);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_mutex_trylock_win32_impl (GMutex * mutex)
|
||||
{
|
||||
DWORD result;
|
||||
win32_check_for_error (WAIT_FAILED !=
|
||||
(result = WaitForSingleObject (*(HANDLE *)mutex, 0)));
|
||||
return result != WAIT_TIMEOUT;
|
||||
}
|
||||
|
||||
static void
|
||||
g_mutex_unlock_win32_impl (GMutex *mutex)
|
||||
{
|
||||
ReleaseMutex (*(HANDLE *) mutex);
|
||||
}
|
||||
|
||||
static GCond *
|
||||
g_cond_new_win32_impl (void)
|
||||
{
|
||||
GCond *retval = g_new (GCond, 1);
|
||||
|
||||
retval->array = g_ptr_array_new ();
|
||||
InitializeCriticalSection (&retval->lock);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_signal_win32_impl (GCond * cond)
|
||||
{
|
||||
EnterCriticalSection (&cond->lock);
|
||||
|
||||
if (cond->array->len > 0)
|
||||
{
|
||||
SetEvent (g_ptr_array_index (cond->array, 0));
|
||||
g_ptr_array_remove_index (cond->array, 0);
|
||||
}
|
||||
|
||||
LeaveCriticalSection (&cond->lock);
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_broadcast_win32_impl (GCond * cond)
|
||||
{
|
||||
guint i;
|
||||
EnterCriticalSection (&cond->lock);
|
||||
|
||||
for (i = 0; i < cond->array->len; i++)
|
||||
SetEvent (g_ptr_array_index (cond->array, i));
|
||||
|
||||
g_ptr_array_set_size (cond->array, 0);
|
||||
LeaveCriticalSection (&cond->lock);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_cond_wait_internal (GCond *cond,
|
||||
GMutex *entered_mutex,
|
||||
gulong milliseconds)
|
||||
{
|
||||
gulong retval;
|
||||
HANDLE event = TlsGetValue (g_cond_event_tls);
|
||||
|
||||
if (!event)
|
||||
{
|
||||
win32_check_for_error (event = CreateEvent (0, FALSE, FALSE, NULL));
|
||||
TlsSetValue (g_cond_event_tls, event);
|
||||
}
|
||||
|
||||
EnterCriticalSection (&cond->lock);
|
||||
|
||||
/* The event must not be signaled. Check this */
|
||||
g_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
|
||||
|
||||
g_ptr_array_add (cond->array, event);
|
||||
LeaveCriticalSection (&cond->lock);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_unlock (entered_mutex);
|
||||
|
||||
win32_check_for_error (WAIT_FAILED !=
|
||||
(retval = WaitForSingleObject (event, milliseconds)));
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_lock (entered_mutex);
|
||||
|
||||
if (retval == WAIT_TIMEOUT)
|
||||
{
|
||||
EnterCriticalSection (&cond->lock);
|
||||
g_ptr_array_remove (cond->array, event);
|
||||
|
||||
/* In the meantime we could have been signaled, so we must again
|
||||
* wait for the signal, this time with no timeout, to reset
|
||||
* it. retval is set again to honour the late arrival of the
|
||||
* signal */
|
||||
win32_check_for_error (WAIT_FAILED !=
|
||||
(retval = WaitForSingleObject (event, 0)));
|
||||
|
||||
LeaveCriticalSection (&cond->lock);
|
||||
}
|
||||
|
||||
#ifndef G_DISABLE_ASSERT
|
||||
EnterCriticalSection (&cond->lock);
|
||||
|
||||
/* Now event must not be inside the array, check this */
|
||||
g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
|
||||
|
||||
LeaveCriticalSection (&cond->lock);
|
||||
#endif /* !G_DISABLE_ASSERT */
|
||||
|
||||
return retval != WAIT_TIMEOUT;
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_wait_win32_impl (GCond *cond,
|
||||
GMutex *entered_mutex)
|
||||
{
|
||||
g_return_if_fail (cond != NULL);
|
||||
g_return_if_fail (entered_mutex != NULL);
|
||||
|
||||
g_cond_wait_internal (cond, entered_mutex, INFINITE);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_cond_timed_wait_win32_impl (GCond *cond,
|
||||
GMutex *entered_mutex,
|
||||
GTimeVal *abs_time)
|
||||
{
|
||||
GTimeVal current_time;
|
||||
gulong to_wait;
|
||||
|
||||
g_return_val_if_fail (cond != NULL, FALSE);
|
||||
g_return_val_if_fail (entered_mutex != NULL, FALSE);
|
||||
|
||||
if (!abs_time)
|
||||
to_wait = INFINITE;
|
||||
else
|
||||
{
|
||||
g_get_current_time (¤t_time);
|
||||
if (abs_time->tv_sec < current_time.tv_sec ||
|
||||
(abs_time->tv_sec == current_time.tv_sec &&
|
||||
abs_time->tv_usec <= current_time.tv_usec))
|
||||
to_wait = 0;
|
||||
else
|
||||
to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
|
||||
(abs_time->tv_usec - current_time.tv_usec) / 1000;
|
||||
}
|
||||
|
||||
return g_cond_wait_internal (cond, entered_mutex, to_wait);
|
||||
}
|
||||
|
||||
static void
|
||||
g_cond_free_win32_impl (GCond * cond)
|
||||
{
|
||||
DeleteCriticalSection (&cond->lock);
|
||||
g_ptr_array_free (cond->array, TRUE);
|
||||
g_free (cond);
|
||||
}
|
||||
|
||||
static GPrivate *
|
||||
g_private_new_win32_impl (GDestroyNotify destructor)
|
||||
{
|
||||
GPrivate *result;
|
||||
EnterCriticalSection (&g_thread_global_spinlock);
|
||||
if (g_private_next >= G_PRIVATE_MAX)
|
||||
{
|
||||
char buf[100];
|
||||
sprintf (buf,
|
||||
"Too many GPrivate allocated. Their number is limited to %d.",
|
||||
G_PRIVATE_MAX);
|
||||
MessageBox (NULL, buf, NULL, MB_ICONERROR|MB_SETFOREGROUND);
|
||||
if (IsDebuggerPresent ())
|
||||
G_BREAKPOINT ();
|
||||
abort ();
|
||||
}
|
||||
g_private_destructors[g_private_next] = destructor;
|
||||
result = GUINT_TO_POINTER (g_private_next);
|
||||
g_private_next++;
|
||||
LeaveCriticalSection (&g_thread_global_spinlock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* NOTE: the functions g_private_get and g_private_set may not use
|
||||
functions from gmem.c and gmessages.c */
|
||||
|
||||
static void
|
||||
g_private_set_win32_impl (GPrivate * private_key, gpointer value)
|
||||
{
|
||||
gpointer* array = TlsGetValue (g_private_tls);
|
||||
guint index = GPOINTER_TO_UINT (private_key);
|
||||
|
||||
if (index >= G_PRIVATE_MAX)
|
||||
return;
|
||||
|
||||
if (!array)
|
||||
{
|
||||
array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
|
||||
TlsSetValue (g_private_tls, array);
|
||||
}
|
||||
|
||||
array[index] = value;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
g_private_get_win32_impl (GPrivate * private_key)
|
||||
{
|
||||
gpointer* array = TlsGetValue (g_private_tls);
|
||||
guint index = GPOINTER_TO_UINT (private_key);
|
||||
|
||||
if (index >= G_PRIVATE_MAX || !array)
|
||||
return NULL;
|
||||
|
||||
return array[index];
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
|
||||
{
|
||||
GThreadData *target = *(GThreadData **)thread;
|
||||
|
||||
g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
|
||||
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
|
||||
|
||||
win32_check_for_error (SetThreadPriority (target->thread,
|
||||
g_thread_priority_map [priority]));
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_self_win32_impl (gpointer thread)
|
||||
{
|
||||
GThreadData *self = TlsGetValue (g_thread_self_tls);
|
||||
|
||||
if (!self)
|
||||
{
|
||||
/* This should only happen for the main thread! */
|
||||
HANDLE handle = GetCurrentThread ();
|
||||
HANDLE process = GetCurrentProcess ();
|
||||
self = g_new (GThreadData, 1);
|
||||
win32_check_for_error (DuplicateHandle (process, handle, process,
|
||||
&self->thread, 0, FALSE,
|
||||
DUPLICATE_SAME_ACCESS));
|
||||
win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
|
||||
self->func = NULL;
|
||||
self->data = NULL;
|
||||
self->joinable = FALSE;
|
||||
}
|
||||
|
||||
*(GThreadData **)thread = self;
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_exit_win32_impl (void)
|
||||
{
|
||||
GThreadData *self = TlsGetValue (g_thread_self_tls);
|
||||
guint i, private_max;
|
||||
gpointer *array = TlsGetValue (g_private_tls);
|
||||
HANDLE event = TlsGetValue (g_cond_event_tls);
|
||||
|
||||
EnterCriticalSection (&g_thread_global_spinlock);
|
||||
private_max = g_private_next;
|
||||
LeaveCriticalSection (&g_thread_global_spinlock);
|
||||
|
||||
if (array)
|
||||
{
|
||||
gboolean some_data_non_null;
|
||||
|
||||
do {
|
||||
some_data_non_null = FALSE;
|
||||
for (i = 0; i < private_max; i++)
|
||||
{
|
||||
GDestroyNotify destructor = g_private_destructors[i];
|
||||
GDestroyNotify data = array[i];
|
||||
|
||||
if (data)
|
||||
some_data_non_null = TRUE;
|
||||
|
||||
array[i] = NULL;
|
||||
|
||||
if (destructor && data)
|
||||
destructor (data);
|
||||
}
|
||||
} while (some_data_non_null);
|
||||
|
||||
free (array);
|
||||
|
||||
win32_check_for_error (TlsSetValue (g_private_tls, NULL));
|
||||
}
|
||||
|
||||
if (self)
|
||||
{
|
||||
if (!self->joinable)
|
||||
{
|
||||
win32_check_for_error (CloseHandle (self->thread));
|
||||
g_free (self);
|
||||
}
|
||||
win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
|
||||
}
|
||||
|
||||
if (event)
|
||||
{
|
||||
CloseHandle (event);
|
||||
win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
|
||||
}
|
||||
|
||||
_endthreadex (0);
|
||||
}
|
||||
|
||||
static guint __stdcall
|
||||
g_thread_proxy (gpointer data)
|
||||
{
|
||||
GThreadData *self = (GThreadData*) data;
|
||||
|
||||
win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
|
||||
|
||||
self->func (self->data);
|
||||
|
||||
g_thread_exit_win32_impl ();
|
||||
|
||||
g_assert_not_reached ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_create_win32_impl (GThreadFunc func,
|
||||
gpointer data,
|
||||
gulong stack_size,
|
||||
gboolean joinable,
|
||||
gboolean bound,
|
||||
GThreadPriority priority,
|
||||
gpointer thread,
|
||||
GError **error)
|
||||
{
|
||||
guint ignore;
|
||||
GThreadData *retval;
|
||||
|
||||
g_return_if_fail (func);
|
||||
g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
|
||||
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
|
||||
|
||||
retval = g_new(GThreadData, 1);
|
||||
retval->func = func;
|
||||
retval->data = data;
|
||||
|
||||
retval->joinable = joinable;
|
||||
|
||||
retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
|
||||
retval, 0, &ignore);
|
||||
|
||||
if (retval->thread == NULL)
|
||||
{
|
||||
gchar *win_error = g_win32_error_message (GetLastError ());
|
||||
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
|
||||
"Error creating thread: %s", win_error);
|
||||
g_free (retval);
|
||||
g_free (win_error);
|
||||
return;
|
||||
}
|
||||
|
||||
*(GThreadData **)thread = retval;
|
||||
|
||||
g_thread_set_priority_win32_impl (thread, priority);
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_yield_win32_impl (void)
|
||||
{
|
||||
Sleep(0);
|
||||
}
|
||||
|
||||
static void
|
||||
g_thread_join_win32_impl (gpointer thread)
|
||||
{
|
||||
GThreadData *target = *(GThreadData **)thread;
|
||||
|
||||
g_return_if_fail (target->joinable);
|
||||
|
||||
win32_check_for_error (WAIT_FAILED !=
|
||||
WaitForSingleObject (target->thread, INFINITE));
|
||||
|
||||
win32_check_for_error (CloseHandle (target->thread));
|
||||
g_free (target);
|
||||
}
|
||||
|
||||
static guint64
|
||||
g_thread_gettime_impl (void)
|
||||
{
|
||||
guint64 v;
|
||||
|
||||
/* Returns 100s of nanoseconds since start of 1601 */
|
||||
GetSystemTimeAsFileTime ((FILETIME *)&v);
|
||||
|
||||
/* Offset to Unix epoch */
|
||||
v -= G_GINT64_CONSTANT (116444736000000000);
|
||||
/* Convert to nanoseconds */
|
||||
v *= 100;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static GThreadFunctions g_thread_functions_for_glib_use_default =
|
||||
{
|
||||
g_mutex_new_win32_impl, /* mutex */
|
||||
g_mutex_lock_win32_impl,
|
||||
g_mutex_trylock_win32_impl,
|
||||
g_mutex_unlock_win32_impl,
|
||||
g_mutex_free_win32_impl,
|
||||
g_cond_new_win32_impl, /* condition */
|
||||
g_cond_signal_win32_impl,
|
||||
g_cond_broadcast_win32_impl,
|
||||
g_cond_wait_win32_impl,
|
||||
g_cond_timed_wait_win32_impl,
|
||||
g_cond_free_win32_impl,
|
||||
g_private_new_win32_impl, /* private thread data */
|
||||
g_private_get_win32_impl,
|
||||
g_private_set_win32_impl,
|
||||
g_thread_create_win32_impl, /* thread */
|
||||
g_thread_yield_win32_impl,
|
||||
g_thread_join_win32_impl,
|
||||
g_thread_exit_win32_impl,
|
||||
g_thread_set_priority_win32_impl,
|
||||
g_thread_self_win32_impl,
|
||||
NULL /* no equal function necessary */
|
||||
};
|
||||
|
||||
#define HAVE_G_THREAD_IMPL_INIT
|
||||
static void
|
||||
g_thread_impl_init ()
|
||||
{
|
||||
static gboolean beenhere = FALSE;
|
||||
HMODULE kernel32;
|
||||
|
||||
if (beenhere)
|
||||
return;
|
||||
|
||||
beenhere = TRUE;
|
||||
|
||||
win32_check_for_error (TLS_OUT_OF_INDEXES !=
|
||||
(g_thread_self_tls = TlsAlloc ()));
|
||||
win32_check_for_error (TLS_OUT_OF_INDEXES !=
|
||||
(g_private_tls = TlsAlloc ()));
|
||||
win32_check_for_error (TLS_OUT_OF_INDEXES !=
|
||||
(g_cond_event_tls = TlsAlloc ()));
|
||||
InitializeCriticalSection (&g_thread_global_spinlock);
|
||||
|
||||
/* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
|
||||
* if it is found, we can use the in general faster critical
|
||||
* sections instead of mutexes. See
|
||||
* http://world.std.com/~jmhart/csmutx.htm for some discussion.
|
||||
*/
|
||||
kernel32 = GetModuleHandle ("KERNEL32.DLL");
|
||||
if (kernel32)
|
||||
{
|
||||
try_enter_critical_section = (GTryEnterCriticalSectionFunc)
|
||||
GetProcAddress(kernel32, "TryEnterCriticalSection");
|
||||
|
||||
/* Even if TryEnterCriticalSection is found, it is not
|
||||
* necessarily working..., we have to check it */
|
||||
if (try_enter_critical_section &&
|
||||
try_enter_critical_section (&g_thread_global_spinlock))
|
||||
{
|
||||
LeaveCriticalSection (&g_thread_global_spinlock);
|
||||
|
||||
g_thread_functions_for_glib_use_default.mutex_new =
|
||||
g_mutex_new_win32_cs_impl;
|
||||
g_thread_functions_for_glib_use_default.mutex_lock =
|
||||
g_mutex_lock_win32_cs_impl;
|
||||
g_thread_functions_for_glib_use_default.mutex_trylock =
|
||||
g_mutex_trylock_win32_cs_impl;
|
||||
g_thread_functions_for_glib_use_default.mutex_unlock =
|
||||
g_mutex_unlock_win32_cs_impl;
|
||||
g_thread_functions_for_glib_use_default.mutex_free =
|
||||
g_mutex_free_win32_cs_impl;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -300,6 +300,14 @@
|
|||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="gthread"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\glib\gthread\gthread-impl.c"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<File
|
||||
RelativePath="..\glib\garray.c"
|
||||
>
|
||||
|
|
Binary file not shown.
|
@ -34,6 +34,7 @@
|
|||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <tchar.h>
|
||||
#include "gthread.h"
|
||||
#include "version.h"
|
||||
#include "CWindow.h"
|
||||
#include "../MMU.h"
|
||||
|
@ -1717,6 +1718,7 @@ int WINAPI WinMain (HINSTANCE hThisInstance,
|
|||
int nFunsterStil)
|
||||
|
||||
{
|
||||
g_thread_init (NULL);
|
||||
hAppInst=hThisInstance;
|
||||
OpenConsole(); // Init debug console
|
||||
|
||||
|
|
Loading…
Reference in New Issue