2001-05-22 14:28:06 +02:00
|
|
|
/* 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
|
2006-05-11 02:08:31 +02:00
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
2001-05-22 14:28:06 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* The GMutex and GCond implementations in this file are some of the
|
|
|
|
* lowest-level code in GLib. All other parts of GLib (messages,
|
|
|
|
* memory, slices, etc) assume that they can freely use these facilities
|
|
|
|
* without risking recursion.
|
|
|
|
*
|
|
|
|
* As such, these functions are NOT permitted to call any other part of
|
|
|
|
* GLib.
|
|
|
|
*
|
|
|
|
* The thread manipulation functions (create, exit, join, etc.) have
|
|
|
|
* more freedom -- they can do as they please.
|
2001-05-22 14:28:06 +02:00
|
|
|
*/
|
|
|
|
|
2006-05-10 01:07:27 +02:00
|
|
|
#include "config.h"
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
#include "gthread.h"
|
2011-09-18 04:00:27 +02:00
|
|
|
#include "gthreadprivate.h"
|
2011-09-21 20:36:53 +02:00
|
|
|
#include "gslice.h"
|
2001-05-22 14:28:06 +02:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <process.h>
|
When the sublangid is SUBLANG_DEFAULT, return the locale of the language's
2001-09-24 Bruno Haible <haible@clisp.cons.org>
* glib/gwin32.c (g_win32_getlocale): When the sublangid is
SUBLANG_DEFAULT, return the locale of the language's main country,
not a country-neutral locale. E.g. "en_US" instead of "en". Add
handling of LANG_SORBIAN. Fix typo for SUBLANG_CHINESE_SIMPLIFIED
(China == CN, CH == Switzerland). Ignore empty environment
variable values.
2001-09-28 Tor Lillqvist <tml@iki.fi>
* glib/makefile.{mingw,msc}.in: Add localcharset.o. Just copy the
source file from libcharset and compile in this directory.
* glib/giochannel.c: Mark rest of g_set_error strings for
translation, too.
* glib/giowin32.c: Add some debugging output functions, call them
when debugging.
(create_events, g_io_win32_msg_write): Free message fetched with
g_win32_error_message ().
(g_io_win32_check): Indentation fixes.
(g_io_win32_fd_read,g_io_win32_sock_read): Don't always return
G_IO_STATUS_NORMAL. Do return G_IO_STATUS_EOF if we got 0 bytes,
like on Unix. This helps making the test programs run
successfully.
* glib/gmain.c (g_poll): Return the code ifdeffed out with
TEST_WITHOUT_THIS. Can't remember why it was ifdeffed out. Things
seem to work as previously with the code in place. Especially
spawn-test didn't work with the code ifdeffed out (Bug#61067).
* glib/grand.c (g_rand_new): Don't try to use /dev/urandom unless
on Unix.
* glib/gspawn-win32-helper.c (WinMain): Remove Sleep(10000)
accidentally left in.
gthread:
2001-09-28 Tor Lillqvist <tml@iki.fi>
* gthread-win32.c: Use an extra level of indirection for GMutex.
It is now a pointer either to a pointer to a CRITICAL_SECTION
struct, or to a mutex HANDLE. This is needed in case the user
defines G_ERRORCHECK_MUTEXES. G_MUTEX_SIZE must correctly reflect
the size of *GMutex, but this used to vary depending on whether we
at run-time chose to use CRITICAL_SECTIONs or mutexes.
(g_mutex_free_win32_cs_impl, g_cond_free_win32_impl): Call
DeleteCriticalSection() when done with it.
* gthread-impl.c (g_thread_init_with_errorcheck_mutexes): Call
g_thread_impl_init() before accessing
g_thread_functions_for_glib_use_default, as the
g_thread_impl_init() function might modify it.
po:
2001-09-28 Tor Lillqvist <tml@iki.fi>
* POTFILES.in: Add iochannel.c and giowin32.c.
* sv.po: Remove a bogus fuzziness indicator.
2001-09-28 00:07:00 +02:00
|
|
|
#include <stdlib.h>
|
2006-03-02 13:05:40 +01:00
|
|
|
#include <stdio.h>
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
static void
|
|
|
|
g_thread_abort (gint status,
|
|
|
|
const gchar *function)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "GLib (gthread-win32.c): Unexpected error from C library during '%s': %s. Aborting.\n",
|
|
|
|
strerror (status), function);
|
|
|
|
abort ();
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* Starting with Vista and Windows 2008, we have access to the
|
|
|
|
* CONDITION_VARIABLE and SRWLock primatives on Windows, which are
|
|
|
|
* pretty reasonable approximations of the primatives specified in
|
|
|
|
* POSIX 2001 (pthread_cond_t and pthread_mutex_t respectively).
|
|
|
|
*
|
|
|
|
* Both of these types are structs containing a single pointer. That
|
|
|
|
* pointer is used as an atomic bitfield to support user-space mutexes
|
|
|
|
* that only get the kernel involved in cases of contention (similar
|
|
|
|
* to how futex()-based mutexes work on Linux). The biggest advantage
|
|
|
|
* of these new types is that they can be statically initialised to
|
|
|
|
* zero. This allows us to use them directly and still support:
|
|
|
|
*
|
|
|
|
* GMutex mutex = G_MUTEX_INIT;
|
|
|
|
*
|
|
|
|
* and
|
|
|
|
*
|
|
|
|
* GCond cond = G_COND_INIT;
|
|
|
|
*
|
|
|
|
* Unfortunately, Windows XP lacks these facilities and GLib still
|
|
|
|
* needs to support Windows XP. Our approach here is as follows:
|
|
|
|
*
|
|
|
|
* - avoid depending on structure declarations at compile-time by
|
|
|
|
* declaring our own GMutex and GCond strutures to be
|
|
|
|
* ABI-compatible with SRWLock and CONDITION_VARIABLE and using
|
|
|
|
* those instead
|
|
|
|
*
|
|
|
|
* - avoid a hard dependency on the symbols used to manipulate these
|
|
|
|
* structures by doing a dynamic lookup of those symbols at
|
|
|
|
* runtime
|
|
|
|
*
|
|
|
|
* - if the symbols are not available, emulate them using other
|
|
|
|
* primatives
|
|
|
|
*
|
|
|
|
* Using this approach also allows us to easily build a GLib that lacks
|
|
|
|
* support for Windows XP or to remove this code entirely when XP is no
|
|
|
|
* longer supported (end of line is currently April 8, 2014).
|
|
|
|
*/
|
|
|
|
typedef struct
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-19 05:07:24 +02:00
|
|
|
void (__stdcall * CallThisOnThreadExit) (void); /* fake */
|
|
|
|
|
|
|
|
void (__stdcall * InitializeSRWLock) (gpointer lock);
|
|
|
|
void (__stdcall * DeleteSRWLock) (gpointer lock); /* fake */
|
|
|
|
void (__stdcall * AcquireSRWLockExclusive) (gpointer lock);
|
|
|
|
BOOLEAN (__stdcall * TryAcquireSRWLockExclusive) (gpointer lock);
|
|
|
|
void (__stdcall * ReleaseSRWLockExclusive) (gpointer lock);
|
2011-09-20 16:06:57 +02:00
|
|
|
void (__stdcall * AcquireSRWLockShared) (gpointer lock);
|
|
|
|
BOOLEAN (__stdcall * TryAcquireSRWLockShared) (gpointer lock);
|
|
|
|
void (__stdcall * ReleaseSRWLockShared) (gpointer lock);
|
2011-09-19 05:07:24 +02:00
|
|
|
|
|
|
|
void (__stdcall * InitializeConditionVariable) (gpointer cond);
|
|
|
|
void (__stdcall * DeleteConditionVariable) (gpointer cond); /* fake */
|
|
|
|
BOOL (__stdcall * SleepConditionVariableSRW) (gpointer cond,
|
|
|
|
gpointer lock,
|
|
|
|
DWORD timeout,
|
|
|
|
ULONG flags);
|
|
|
|
void (__stdcall * WakeAllConditionVariable) (gpointer cond);
|
|
|
|
void (__stdcall * WakeConditionVariable) (gpointer cond);
|
2011-09-17 00:05:23 +02:00
|
|
|
} GThreadImplVtable;
|
|
|
|
|
|
|
|
static GThreadImplVtable g_thread_impl_vtable;
|
|
|
|
|
|
|
|
/* {{{1 GMutex */
|
|
|
|
void
|
|
|
|
g_mutex_init (GMutex *mutex)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.InitializeSRWLock (mutex);
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_mutex_clear (GMutex *mutex)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
if (g_thread_impl_vtable.DeleteSRWLock != NULL)
|
|
|
|
g_thread_impl_vtable.DeleteSRWLock (mutex);
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_mutex_lock (GMutex *mutex)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.AcquireSRWLockExclusive (mutex);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
gboolean
|
|
|
|
g_mutex_trylock (GMutex *mutex)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
return g_thread_impl_vtable.TryAcquireSRWLockExclusive (mutex);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_mutex_unlock (GMutex *mutex)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.ReleaseSRWLockExclusive (mutex);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-21 20:36:53 +02:00
|
|
|
/* {{{1 GRecMutex */
|
|
|
|
|
|
|
|
static CRITICAL_SECTION *
|
|
|
|
g_rec_mutex_impl_new (void)
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION *cs;
|
|
|
|
|
|
|
|
cs = g_slice_new (CRITICAL_SECTION);
|
|
|
|
InitializeCriticalSection (cs);
|
|
|
|
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_rec_mutex_impl_free (CRITICAL_SECTION *cs)
|
|
|
|
{
|
|
|
|
DeleteCriticalSection (cs);
|
|
|
|
g_slice_free (CRITICAL_SECTION, cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CRITICAL_SECTION *
|
|
|
|
g_rec_mutex_get_impl (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION *impl = mutex->impl;
|
|
|
|
|
|
|
|
if G_UNLIKELY (mutex->impl == NULL)
|
|
|
|
{
|
|
|
|
impl = g_rec_mutex_impl_new ();
|
|
|
|
if (InterlockedCompareExchangePointer (&mutex->impl, impl, NULL) != NULL)
|
|
|
|
g_rec_mutex_impl_free (impl);
|
|
|
|
impl = mutex->impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_init (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
mutex->impl = g_rec_mutex_impl_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_clear (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
if (mutex->impl)
|
|
|
|
g_rec_mutex_impl_free (mutex->impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_lock (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
EnterCriticalSection (g_rec_mutex_get_impl (mutex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_unlock (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection (mutex->impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
g_rec_mutex_trylock (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex));
|
|
|
|
}
|
|
|
|
|
2011-09-21 16:19:36 +02:00
|
|
|
/* {{{1 GRWLock */
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_init (GRWLock *lock)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.InitializeSRWLock (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_clear (GRWLock *lock)
|
|
|
|
{
|
|
|
|
if (g_thread_impl_vtable.DeleteSRWLock != NULL)
|
|
|
|
g_thread_impl_vtable.DeleteSRWLock (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_writer_lock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.AcquireSRWLockExclusive (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
g_rw_lock_writer_trylock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
return g_thread_impl_vtable.TryAcquireSRWLockExclusive (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_writer_unlock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.ReleaseSRWLockExclusive (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_reader_lock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.AcquireSRWLockShared (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
g_rw_lock_reader_trylock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
return g_thread_impl_vtable.TryAcquireSRWLockShared (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_reader_unlock (GRWLock *lock)
|
|
|
|
{
|
|
|
|
g_thread_impl_vtable.ReleaseSRWLockShared (lock);
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{1 GCond */
|
|
|
|
void
|
|
|
|
g_cond_init (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.InitializeConditionVariable (cond);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_cond_clear (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
if (g_thread_impl_vtable.DeleteConditionVariable)
|
|
|
|
g_thread_impl_vtable.DeleteConditionVariable (cond);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_cond_signal (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.WakeConditionVariable (cond);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_cond_broadcast (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.WakeAllConditionVariable (cond);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
void
|
|
|
|
g_cond_wait (GCond *cond,
|
|
|
|
GMutex *entered_mutex)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, INFINITE, 0);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
gboolean
|
|
|
|
g_cond_timedwait (GCond *cond,
|
|
|
|
GMutex *entered_mutex,
|
|
|
|
gint64 abs_time)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
gint64 span;
|
|
|
|
FILETIME ft;
|
|
|
|
gint64 now;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
GetSystemTimeAsFileTime (&ft);
|
|
|
|
memmove (&now, &ft, sizeof (FILETIME));
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
now -= G_GINT64_CONSTANT (116444736000000000);
|
|
|
|
now /= 10;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
span = abs_time - now;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
if G_UNLIKELY (span < 0)
|
|
|
|
span = 0;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * G_MAXINT32)
|
|
|
|
span = INFINITE;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
gboolean
|
|
|
|
g_cond_timed_wait (GCond *cond,
|
|
|
|
GMutex *entered_mutex,
|
|
|
|
GTimeVal *abs_time)
|
|
|
|
{
|
|
|
|
if (abs_time)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
gint64 micros;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
micros = abs_time->tv_sec;
|
|
|
|
micros *= 1000000;
|
|
|
|
micros += abs_time->tv_usec;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
return g_cond_timedwait (cond, entered_mutex, micros);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_cond_wait (cond, entered_mutex);
|
|
|
|
return TRUE;
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{1 GPrivate */
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-18 04:00:27 +02:00
|
|
|
typedef struct _GPrivateDestructor GPrivateDestructor;
|
|
|
|
|
|
|
|
struct _GPrivateDestructor
|
|
|
|
{
|
|
|
|
DWORD index;
|
|
|
|
GDestroyNotify notify;
|
|
|
|
GPrivateDestructor *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GPrivateDestructor * volatile g_private_destructors;
|
2011-09-30 20:22:04 +02:00
|
|
|
static CRITICAL_SECTION g_private_lock;
|
2011-09-18 04:00:27 +02:00
|
|
|
|
2011-09-30 20:22:04 +02:00
|
|
|
static DWORD
|
|
|
|
g_private_get_impl (GPrivate *key)
|
2011-09-18 04:00:27 +02:00
|
|
|
{
|
2011-09-30 20:22:04 +02:00
|
|
|
DWORD impl = (DWORD) key->p;
|
2011-09-18 04:00:27 +02:00
|
|
|
|
2011-09-30 20:22:04 +02:00
|
|
|
if G_UNLIKELY (impl == 0)
|
|
|
|
{
|
|
|
|
EnterCriticalSection (&g_private_lock);
|
|
|
|
impl = (DWORD) key->p;
|
|
|
|
if (impl == 0)
|
|
|
|
{
|
|
|
|
GPrivateDestructor *destructor;
|
2011-09-18 04:00:27 +02:00
|
|
|
|
2011-09-30 20:22:04 +02:00
|
|
|
impl = TlsAlloc ();
|
2011-09-18 04:00:27 +02:00
|
|
|
|
2011-09-30 20:22:04 +02:00
|
|
|
if (impl == TLS_OUT_OF_INDEXES)
|
|
|
|
g_thread_abort (0, "TlsAlloc");
|
|
|
|
|
|
|
|
if (key->notify != NULL)
|
|
|
|
{
|
|
|
|
destructor = malloc (sizeof (GPrivateDestructor));
|
|
|
|
if G_UNLIKELY (destructor == NULL)
|
|
|
|
g_thread_abort (errno, "malloc");
|
|
|
|
destructor->index = impl;
|
|
|
|
destructor->notify = key->notify;
|
|
|
|
destructor->next = g_private_destructors;
|
|
|
|
|
|
|
|
/* We need to do an atomic store due to the unlocked
|
|
|
|
* access to the destructor list from the thread exit
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* It can double as a sanity check...
|
|
|
|
*/
|
|
|
|
if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
|
|
|
|
destructor->next) != destructor->next)
|
|
|
|
g_thread_abort (0, "g_private_get_impl(1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ditto, due to the unlocked access on the fast path */
|
|
|
|
if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
|
|
|
|
g_thread_abort (0, "g_private_get_impl(2)");
|
|
|
|
}
|
|
|
|
LeaveCriticalSection (&g_private_lock);
|
|
|
|
}
|
2011-09-18 07:10:07 +02:00
|
|
|
|
2011-09-30 20:22:04 +02:00
|
|
|
return impl;
|
2011-09-18 04:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2011-09-18 07:10:07 +02:00
|
|
|
g_private_get (GPrivate *key)
|
2011-09-18 04:00:27 +02:00
|
|
|
{
|
2011-09-30 20:22:04 +02:00
|
|
|
return TlsGetValue (g_private_get_impl (key));
|
2011-09-18 04:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-18 07:10:07 +02:00
|
|
|
g_private_set (GPrivate *key,
|
2011-09-18 04:00:27 +02:00
|
|
|
gpointer value)
|
|
|
|
{
|
2011-09-30 20:22:04 +02:00
|
|
|
TlsSetValue (g_private_get_impl (key), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_private_replace (GPrivate *key,
|
|
|
|
gpointer value)
|
|
|
|
{
|
|
|
|
DWORD impl = g_private_get_impl (key);
|
|
|
|
gpointer old;
|
2011-09-18 07:10:07 +02:00
|
|
|
|
2011-10-03 03:13:14 +02:00
|
|
|
old = TlsGetValue (impl);
|
2011-09-30 20:22:04 +02:00
|
|
|
if (old && key->notify)
|
|
|
|
key->notify (old);
|
|
|
|
TlsSetValue (impl, value);
|
2011-09-18 04:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{1 GThread */
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
#include "glib.h"
|
|
|
|
#include "gthreadprivate.h"
|
2006-05-11 02:08:31 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
#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
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
#define G_MUTEX_SIZE (sizeof (gpointer))
|
|
|
|
|
|
|
|
static DWORD g_thread_self_tls;
|
|
|
|
|
|
|
|
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
|
|
|
|
|
|
|
|
typedef struct _GThreadData GThreadData;
|
|
|
|
struct _GThreadData
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-17 00:05:23 +02:00
|
|
|
GThreadFunc func;
|
|
|
|
gpointer data;
|
|
|
|
HANDLE thread;
|
|
|
|
gboolean joinable;
|
|
|
|
};
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-19 05:58:12 +02:00
|
|
|
void
|
|
|
|
g_system_thread_self (gpointer thread)
|
2006-05-11 02:08:31 +02:00
|
|
|
{
|
2001-05-22 14:28:06 +02:00
|
|
|
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);
|
2006-05-11 02:08:31 +02:00
|
|
|
win32_check_for_error (DuplicateHandle (process, handle, process,
|
|
|
|
&self->thread, 0, FALSE,
|
2001-05-22 14:28:06 +02:00
|
|
|
DUPLICATE_SAME_ACCESS));
|
|
|
|
win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
|
|
|
|
self->func = NULL;
|
2006-05-11 02:08:31 +02:00
|
|
|
self->data = NULL;
|
2001-05-22 14:28:06 +02:00
|
|
|
self->joinable = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(GThreadData **)thread = self;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:18:17 +02:00
|
|
|
void
|
|
|
|
g_system_thread_exit (void)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
|
|
|
GThreadData *self = TlsGetValue (g_thread_self_tls);
|
2011-09-18 04:00:27 +02:00
|
|
|
gboolean dtors_called;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-09-18 04:00:27 +02:00
|
|
|
do
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-09-18 04:00:27 +02:00
|
|
|
GPrivateDestructor *dtor;
|
|
|
|
|
|
|
|
/* We go by the POSIX book on this one.
|
|
|
|
*
|
|
|
|
* If we call a destructor then there is a chance that some new
|
|
|
|
* TLS variables got set by code called in that destructor.
|
|
|
|
*
|
|
|
|
* Loop until nothing is left.
|
|
|
|
*/
|
|
|
|
dtors_called = FALSE;
|
|
|
|
|
|
|
|
for (dtor = g_private_destructors; dtor; dtor = dtor->next)
|
|
|
|
{
|
|
|
|
gpointer value;
|
|
|
|
|
|
|
|
value = TlsGetValue (dtor->index);
|
|
|
|
if (value != NULL && dtor->notify != NULL)
|
|
|
|
{
|
|
|
|
/* POSIX says to clear this before the call */
|
|
|
|
TlsSetValue (dtor->index, NULL);
|
|
|
|
dtor->notify (value);
|
|
|
|
dtors_called = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
2011-09-18 04:00:27 +02:00
|
|
|
while (dtors_called);
|
2006-05-11 02:08:31 +02:00
|
|
|
|
2001-05-22 14:28:06 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
if (g_thread_impl_vtable.CallThisOnThreadExit)
|
|
|
|
g_thread_impl_vtable.CallThisOnThreadExit ();
|
2001-05-22 14:28:06 +02:00
|
|
|
|
|
|
|
_endthreadex (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint __stdcall
|
|
|
|
g_thread_proxy (gpointer data)
|
|
|
|
{
|
|
|
|
GThreadData *self = (GThreadData*) data;
|
|
|
|
|
|
|
|
win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
|
2006-05-11 02:08:31 +02:00
|
|
|
|
2001-05-22 14:28:06 +02:00
|
|
|
self->func (self->data);
|
|
|
|
|
2011-09-19 05:18:17 +02:00
|
|
|
g_system_thread_exit ();
|
2001-05-22 14:28:06 +02:00
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:58:12 +02:00
|
|
|
void
|
|
|
|
g_system_thread_create (GThreadFunc func,
|
|
|
|
gpointer data,
|
|
|
|
gulong stack_size,
|
|
|
|
gboolean joinable,
|
|
|
|
gpointer thread,
|
|
|
|
GError **error)
|
2006-05-11 02:08:31 +02:00
|
|
|
{
|
2001-05-22 14:28:06 +02:00
|
|
|
guint ignore;
|
|
|
|
GThreadData *retval;
|
|
|
|
|
|
|
|
g_return_if_fail (func);
|
2006-05-11 02:08:31 +02:00
|
|
|
|
2001-05-22 14:28:06 +02:00
|
|
|
retval = g_new(GThreadData, 1);
|
|
|
|
retval->func = func;
|
|
|
|
retval->data = data;
|
2006-05-11 02:08:31 +02:00
|
|
|
|
2001-05-22 14:28:06 +02:00
|
|
|
retval->joinable = joinable;
|
|
|
|
|
2006-05-11 02:08:31 +02:00
|
|
|
retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
|
2001-05-22 14:28:06 +02:00
|
|
|
retval, 0, &ignore);
|
|
|
|
|
|
|
|
if (retval->thread == NULL)
|
|
|
|
{
|
2001-06-07 12:34:22 +02:00
|
|
|
gchar *win_error = g_win32_error_message (GetLastError ());
|
2006-05-11 02:08:31 +02:00
|
|
|
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
|
2001-06-07 12:34:22 +02:00
|
|
|
"Error creating thread: %s", win_error);
|
|
|
|
g_free (retval);
|
|
|
|
g_free (win_error);
|
2001-05-22 14:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(GThreadData **)thread = retval;
|
|
|
|
}
|
|
|
|
|
2011-09-19 03:39:35 +02:00
|
|
|
void
|
|
|
|
g_thread_yield (void)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
|
|
|
Sleep(0);
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:58:12 +02:00
|
|
|
void
|
|
|
|
g_system_thread_join (gpointer thread)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
|
|
|
GThreadData *target = *(GThreadData **)thread;
|
|
|
|
|
|
|
|
g_return_if_fail (target->joinable);
|
|
|
|
|
2006-05-11 02:08:31 +02:00
|
|
|
win32_check_for_error (WAIT_FAILED !=
|
2001-05-22 14:28:06 +02:00
|
|
|
WaitForSingleObject (target->thread, INFINITE));
|
|
|
|
|
|
|
|
win32_check_for_error (CloseHandle (target->thread));
|
|
|
|
g_free (target);
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:10:25 +02:00
|
|
|
gboolean
|
|
|
|
g_system_thread_equal (gpointer thread1,
|
|
|
|
gpointer thread2)
|
|
|
|
{
|
|
|
|
return ((GSystemThread*)thread1)->dummy_pointer == ((GSystemThread*)thread2)->dummy_pointer;
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
|
|
|
|
|
|
|
|
static CRITICAL_SECTION g_thread_xp_lock;
|
2011-09-18 04:00:27 +02:00
|
|
|
static DWORD g_thread_xp_waiter_tls;
|
2011-09-17 00:05:23 +02:00
|
|
|
|
|
|
|
/* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
|
|
|
|
typedef struct _GThreadXpWaiter GThreadXpWaiter;
|
|
|
|
struct _GThreadXpWaiter
|
|
|
|
{
|
|
|
|
HANDLE event;
|
|
|
|
volatile GThreadXpWaiter *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GThreadXpWaiter *
|
|
|
|
g_thread_xp_waiter_get (void)
|
|
|
|
{
|
|
|
|
GThreadXpWaiter *waiter;
|
|
|
|
|
|
|
|
waiter = TlsGetValue (g_thread_xp_waiter_tls);
|
|
|
|
|
|
|
|
if G_UNLIKELY (waiter == NULL)
|
|
|
|
{
|
|
|
|
waiter = malloc (sizeof (GThreadXpWaiter));
|
|
|
|
if (waiter == NULL)
|
|
|
|
g_thread_abort (GetLastError (), "malloc");
|
|
|
|
waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
|
|
|
|
if (waiter->event == NULL)
|
|
|
|
g_thread_abort (GetLastError (), "CreateEvent");
|
|
|
|
|
|
|
|
TlsSetValue (g_thread_xp_waiter_tls, waiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return waiter;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_CallThisOnThreadExit (void)
|
|
|
|
{
|
|
|
|
GThreadXpWaiter *waiter;
|
|
|
|
|
|
|
|
waiter = TlsGetValue (g_thread_xp_waiter_tls);
|
|
|
|
|
|
|
|
if (waiter != NULL)
|
|
|
|
{
|
|
|
|
TlsSetValue (g_thread_xp_waiter_tls, NULL);
|
|
|
|
CloseHandle (waiter->event);
|
|
|
|
free (waiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{2 SRWLock emulation */
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-09-20 06:04:43 +02:00
|
|
|
CRITICAL_SECTION writer_lock;
|
2011-09-20 16:06:57 +02:00
|
|
|
gboolean ever_shared; /* protected by writer_lock */
|
2011-09-26 10:44:41 +02:00
|
|
|
gboolean writer_locked; /* protected by writer_lock */
|
2011-09-20 16:06:57 +02:00
|
|
|
|
|
|
|
/* below is only ever touched if ever_shared becomes true */
|
|
|
|
CRITICAL_SECTION atomicity;
|
|
|
|
GThreadXpWaiter *queued_writer; /* protected by atomicity lock */
|
|
|
|
gint num_readers; /* protected by atomicity lock */
|
2011-09-17 00:05:23 +02:00
|
|
|
} GThreadSRWLock;
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_InitializeSRWLock (gpointer mutex)
|
|
|
|
{
|
|
|
|
*(GThreadSRWLock * volatile *) mutex = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_DeleteSRWLock (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
|
|
|
|
|
|
|
|
if (lock)
|
|
|
|
{
|
2011-09-20 16:06:57 +02:00
|
|
|
if (lock->ever_shared)
|
|
|
|
DeleteCriticalSection (&lock->atomicity);
|
|
|
|
|
2011-09-20 06:04:43 +02:00
|
|
|
DeleteCriticalSection (&lock->writer_lock);
|
2011-09-17 00:05:23 +02:00
|
|
|
free (lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static GThreadSRWLock * __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *result;
|
|
|
|
|
|
|
|
/* It looks like we're missing some barriers here, but this code only
|
|
|
|
* ever runs on Windows XP, which in turn only ever runs on hardware
|
|
|
|
* with a relatively rigid memory model. The 'volatile' will take
|
|
|
|
* care of the compiler.
|
|
|
|
*/
|
|
|
|
result = *lock;
|
|
|
|
|
|
|
|
if G_UNLIKELY (result == NULL)
|
|
|
|
{
|
|
|
|
EnterCriticalSection (&g_thread_xp_lock);
|
|
|
|
|
|
|
|
result = malloc (sizeof (GThreadSRWLock));
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
g_thread_abort (errno, "malloc");
|
|
|
|
|
2011-09-20 06:04:43 +02:00
|
|
|
InitializeCriticalSection (&result->writer_lock);
|
2011-09-26 10:44:41 +02:00
|
|
|
result->writer_locked = FALSE;
|
2011-09-20 16:06:57 +02:00
|
|
|
result->ever_shared = FALSE;
|
2011-09-17 00:05:23 +02:00
|
|
|
*lock = result;
|
|
|
|
|
|
|
|
LeaveCriticalSection (&g_thread_xp_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
|
|
|
|
|
2011-09-20 06:04:43 +02:00
|
|
|
EnterCriticalSection (&lock->writer_lock);
|
2011-09-20 16:06:57 +02:00
|
|
|
|
2011-09-26 10:44:41 +02:00
|
|
|
/* CRITICAL_SECTION is reentrant, but SRWLock is not.
|
|
|
|
* Detect the deadlock that would occur on later Windows version.
|
|
|
|
*/
|
|
|
|
g_assert (!lock->writer_locked);
|
|
|
|
lock->writer_locked = TRUE;
|
|
|
|
|
2011-09-20 16:06:57 +02:00
|
|
|
if (lock->ever_shared)
|
|
|
|
{
|
|
|
|
GThreadXpWaiter *waiter = NULL;
|
|
|
|
|
|
|
|
EnterCriticalSection (&lock->atomicity);
|
|
|
|
if (lock->num_readers > 0)
|
|
|
|
lock->queued_writer = waiter = g_thread_xp_waiter_get ();
|
|
|
|
LeaveCriticalSection (&lock->atomicity);
|
|
|
|
|
|
|
|
if (waiter != NULL)
|
|
|
|
WaitForSingleObject (waiter->event, INFINITE);
|
|
|
|
|
|
|
|
lock->queued_writer = NULL;
|
|
|
|
}
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static BOOLEAN __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
|
|
|
|
|
2011-09-20 06:05:34 +02:00
|
|
|
if (!TryEnterCriticalSection (&lock->writer_lock))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-09-26 10:44:41 +02:00
|
|
|
/* CRITICAL_SECTION is reentrant, but SRWLock is not.
|
|
|
|
* Ensure that this properly returns FALSE (as SRWLock would).
|
|
|
|
*/
|
|
|
|
if G_UNLIKELY (lock->writer_locked)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock->writer_locked = TRUE;
|
|
|
|
|
2011-09-20 16:06:57 +02:00
|
|
|
if (lock->ever_shared)
|
|
|
|
{
|
|
|
|
gboolean available;
|
|
|
|
|
|
|
|
EnterCriticalSection (&lock->atomicity);
|
|
|
|
available = lock->num_readers == 0;
|
|
|
|
LeaveCriticalSection (&lock->atomicity);
|
|
|
|
|
|
|
|
if (!available)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-20 06:05:34 +02:00
|
|
|
return TRUE;
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
|
|
|
|
|
2011-09-26 10:44:41 +02:00
|
|
|
lock->writer_locked = FALSE;
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* We need this until we fix some weird parts of GLib that try to
|
|
|
|
* unlock freshly-allocated mutexes.
|
|
|
|
*/
|
|
|
|
if (lock != NULL)
|
2011-09-20 06:04:43 +02:00
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
|
|
|
|
2011-09-20 16:06:57 +02:00
|
|
|
static void
|
|
|
|
g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
|
|
|
|
{
|
|
|
|
if G_UNLIKELY (!lock->ever_shared)
|
|
|
|
{
|
|
|
|
InitializeCriticalSection (&lock->atomicity);
|
|
|
|
lock->queued_writer = NULL;
|
|
|
|
lock->num_readers = 0;
|
|
|
|
|
|
|
|
lock->ever_shared = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection (&lock->atomicity);
|
|
|
|
lock->num_readers++;
|
|
|
|
LeaveCriticalSection (&lock->atomicity);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
g_thread_xp_AcquireSRWLockShared (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
|
|
|
|
|
|
|
|
EnterCriticalSection (&lock->writer_lock);
|
|
|
|
|
2011-09-26 10:44:41 +02:00
|
|
|
/* See g_thread_xp_AcquireSRWLockExclusive */
|
|
|
|
g_assert (!lock->writer_locked);
|
|
|
|
|
2011-09-20 16:06:57 +02:00
|
|
|
g_thread_xp_srwlock_become_reader (lock);
|
|
|
|
|
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOLEAN __stdcall
|
|
|
|
g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
|
|
|
|
|
|
|
|
if (!TryEnterCriticalSection (&lock->writer_lock))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-09-26 10:44:41 +02:00
|
|
|
/* See g_thread_xp_AcquireSRWLockExclusive */
|
|
|
|
if G_UNLIKELY (lock->writer_locked)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-20 16:06:57 +02:00
|
|
|
g_thread_xp_srwlock_become_reader (lock);
|
|
|
|
|
|
|
|
LeaveCriticalSection (&lock->writer_lock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
|
|
|
|
{
|
|
|
|
GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
|
|
|
|
|
|
|
|
EnterCriticalSection (&lock->atomicity);
|
|
|
|
|
|
|
|
lock->num_readers--;
|
|
|
|
|
|
|
|
if (lock->num_readers == 0 && lock->queued_writer)
|
|
|
|
SetEvent (lock->queued_writer->event);
|
|
|
|
|
|
|
|
LeaveCriticalSection (&lock->atomicity);
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{2 CONDITION_VARIABLE emulation */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
volatile GThreadXpWaiter *first;
|
|
|
|
volatile GThreadXpWaiter **last_ptr;
|
|
|
|
} GThreadXpCONDITION_VARIABLE;
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_InitializeConditionVariable (gpointer cond)
|
|
|
|
{
|
|
|
|
*(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_DeleteConditionVariable (gpointer cond)
|
|
|
|
{
|
|
|
|
GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
|
|
|
|
|
|
|
|
if (cv)
|
|
|
|
free (cv);
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static GThreadXpCONDITION_VARIABLE * __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
|
|
|
|
{
|
|
|
|
GThreadXpCONDITION_VARIABLE *result;
|
|
|
|
|
|
|
|
/* It looks like we're missing some barriers here, but this code only
|
|
|
|
* ever runs on Windows XP, which in turn only ever runs on hardware
|
|
|
|
* with a relatively rigid memory model. The 'volatile' will take
|
|
|
|
* care of the compiler.
|
|
|
|
*/
|
|
|
|
result = *cond;
|
|
|
|
|
|
|
|
if G_UNLIKELY (result == NULL)
|
|
|
|
{
|
|
|
|
result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
g_thread_abort (errno, "malloc");
|
|
|
|
|
|
|
|
result->first = NULL;
|
|
|
|
result->last_ptr = &result->first;
|
|
|
|
|
|
|
|
if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
|
|
|
|
{
|
|
|
|
free (result);
|
|
|
|
result = *cond;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static BOOL __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_SleepConditionVariableSRW (gpointer cond,
|
|
|
|
gpointer mutex,
|
|
|
|
DWORD timeout,
|
|
|
|
ULONG flags)
|
|
|
|
{
|
|
|
|
GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
|
|
|
|
GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
|
|
|
|
DWORD status;
|
|
|
|
|
|
|
|
waiter->next = NULL;
|
|
|
|
|
|
|
|
EnterCriticalSection (&g_thread_xp_lock);
|
|
|
|
*cv->last_ptr = waiter;
|
|
|
|
cv->last_ptr = &waiter->next;
|
|
|
|
LeaveCriticalSection (&g_thread_xp_lock);
|
|
|
|
|
|
|
|
g_mutex_unlock (mutex);
|
|
|
|
status = WaitForSingleObject (waiter->event, timeout);
|
|
|
|
|
|
|
|
if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
|
|
|
|
g_thread_abort (GetLastError (), "WaitForSingleObject");
|
|
|
|
|
|
|
|
g_mutex_lock (mutex);
|
|
|
|
|
|
|
|
return status == WAIT_OBJECT_0;
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_WakeConditionVariable (gpointer cond)
|
|
|
|
{
|
|
|
|
GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
|
|
|
|
volatile GThreadXpWaiter *waiter;
|
|
|
|
|
|
|
|
EnterCriticalSection (&g_thread_xp_lock);
|
|
|
|
waiter = cv->first;
|
|
|
|
if (waiter != NULL)
|
|
|
|
{
|
|
|
|
cv->first = waiter->next;
|
|
|
|
if (cv->first == NULL)
|
|
|
|
cv->last_ptr = &cv->first;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection (&g_thread_xp_lock);
|
|
|
|
|
|
|
|
if (waiter != NULL)
|
|
|
|
SetEvent (waiter->event);
|
|
|
|
}
|
|
|
|
|
2011-09-19 05:07:24 +02:00
|
|
|
static void __stdcall
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_WakeAllConditionVariable (gpointer cond)
|
|
|
|
{
|
|
|
|
GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
|
|
|
|
volatile GThreadXpWaiter *waiter;
|
|
|
|
|
|
|
|
EnterCriticalSection (&g_thread_xp_lock);
|
|
|
|
waiter = cv->first;
|
|
|
|
cv->first = NULL;
|
|
|
|
cv->last_ptr = &cv->first;
|
|
|
|
LeaveCriticalSection (&g_thread_xp_lock);
|
|
|
|
|
|
|
|
while (waiter != NULL)
|
|
|
|
{
|
|
|
|
volatile GThreadXpWaiter *next;
|
|
|
|
|
|
|
|
next = waiter->next;
|
|
|
|
SetEvent (waiter->event);
|
|
|
|
waiter = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{2 XP Setup */
|
|
|
|
static void
|
|
|
|
g_thread_xp_init (void)
|
|
|
|
{
|
|
|
|
static const GThreadImplVtable g_thread_xp_impl_vtable = {
|
|
|
|
g_thread_xp_CallThisOnThreadExit,
|
|
|
|
g_thread_xp_InitializeSRWLock,
|
|
|
|
g_thread_xp_DeleteSRWLock,
|
|
|
|
g_thread_xp_AcquireSRWLockExclusive,
|
|
|
|
g_thread_xp_TryAcquireSRWLockExclusive,
|
|
|
|
g_thread_xp_ReleaseSRWLockExclusive,
|
2011-09-20 16:06:57 +02:00
|
|
|
g_thread_xp_AcquireSRWLockShared,
|
|
|
|
g_thread_xp_TryAcquireSRWLockShared,
|
|
|
|
g_thread_xp_ReleaseSRWLockShared,
|
2011-09-17 00:05:23 +02:00
|
|
|
g_thread_xp_InitializeConditionVariable,
|
|
|
|
g_thread_xp_DeleteConditionVariable,
|
|
|
|
g_thread_xp_SleepConditionVariableSRW,
|
|
|
|
g_thread_xp_WakeAllConditionVariable,
|
|
|
|
g_thread_xp_WakeConditionVariable
|
|
|
|
};
|
|
|
|
|
|
|
|
InitializeCriticalSection (&g_thread_xp_lock);
|
|
|
|
g_thread_xp_waiter_tls = TlsAlloc ();
|
|
|
|
|
|
|
|
g_thread_impl_vtable = g_thread_xp_impl_vtable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{1 Epilogue */
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_thread_lookup_native_funcs (void)
|
|
|
|
{
|
|
|
|
GThreadImplVtable native_vtable = { 0, };
|
|
|
|
HMODULE kernel32;
|
|
|
|
|
|
|
|
kernel32 = GetModuleHandle ("KERNEL32.DLL");
|
|
|
|
|
|
|
|
if (kernel32 == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
#define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
|
|
|
|
GET_FUNC(InitializeSRWLock);
|
|
|
|
GET_FUNC(AcquireSRWLockExclusive);
|
|
|
|
GET_FUNC(TryAcquireSRWLockExclusive);
|
|
|
|
GET_FUNC(ReleaseSRWLockExclusive);
|
2011-09-20 16:06:57 +02:00
|
|
|
GET_FUNC(AcquireSRWLockShared);
|
|
|
|
GET_FUNC(TryAcquireSRWLockShared);
|
|
|
|
GET_FUNC(ReleaseSRWLockShared);
|
2011-09-17 00:05:23 +02:00
|
|
|
|
|
|
|
GET_FUNC(InitializeConditionVariable);
|
|
|
|
GET_FUNC(SleepConditionVariableSRW);
|
|
|
|
GET_FUNC(WakeAllConditionVariable);
|
|
|
|
GET_FUNC(WakeConditionVariable);
|
|
|
|
#undef GET_FUNC
|
|
|
|
|
|
|
|
g_thread_impl_vtable = native_vtable;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_GNUC_INTERNAL void
|
|
|
|
g_thread_DllMain (void)
|
|
|
|
{
|
|
|
|
if (g_thread_lookup_native_funcs ())
|
|
|
|
fprintf (stderr, "(debug) GThread using native mode\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "(debug) GThread using Windows XP mode\n");
|
|
|
|
g_thread_xp_init ();
|
|
|
|
}
|
2011-09-21 06:33:16 +02:00
|
|
|
|
|
|
|
win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
|
2011-09-30 20:22:04 +02:00
|
|
|
InitializeCriticalSection (&g_private_lock);
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set foldmethod=marker: */
|
|
|
|
|