Blame glib/gthread-win32.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * gthread.c: solaris thread system implementation
Packit ae235b
 * Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
Packit ae235b
 * Copyright 2001 Hans Breuer
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* The GMutex and GCond implementations in this file are some of the
Packit ae235b
 * lowest-level code in GLib.  All other parts of GLib (messages,
Packit ae235b
 * memory, slices, etc) assume that they can freely use these facilities
Packit ae235b
 * without risking recursion.
Packit ae235b
 *
Packit ae235b
 * As such, these functions are NOT permitted to call any other part of
Packit ae235b
 * GLib.
Packit ae235b
 *
Packit ae235b
 * The thread manipulation functions (create, exit, join, etc.) have
Packit ae235b
 * more freedom -- they can do as they please.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
#include "glib-init.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gthreadprivate.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
Packit ae235b
#include <windows.h>
Packit ae235b
Packit ae235b
#include <process.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_abort (gint         status,
Packit ae235b
                const gchar *function)
Packit ae235b
{
Packit ae235b
  fprintf (stderr, "GLib (gthread-win32.c): Unexpected error from C library during '%s': %s.  Aborting.\n",
Packit ae235b
           strerror (status), function);
Packit ae235b
  g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Starting with Vista and Windows 2008, we have access to the
Packit ae235b
 * CONDITION_VARIABLE and SRWLock primatives on Windows, which are
Packit ae235b
 * pretty reasonable approximations of the primatives specified in
Packit ae235b
 * POSIX 2001 (pthread_cond_t and pthread_mutex_t respectively).
Packit ae235b
 *
Packit ae235b
 * Both of these types are structs containing a single pointer.  That
Packit ae235b
 * pointer is used as an atomic bitfield to support user-space mutexes
Packit ae235b
 * that only get the kernel involved in cases of contention (similar
Packit ae235b
 * to how futex()-based mutexes work on Linux).  The biggest advantage
Packit ae235b
 * of these new types is that they can be statically initialised to
Packit ae235b
 * zero.  That means that they are completely ABI compatible with our
Packit ae235b
 * GMutex and GCond APIs.
Packit ae235b
 *
Packit ae235b
 * Unfortunately, Windows XP lacks these facilities and GLib still
Packit ae235b
 * needs to support Windows XP.  Our approach here is as follows:
Packit ae235b
 *
Packit ae235b
 *   - avoid depending on structure declarations at compile-time by
Packit ae235b
 *     declaring our own GMutex and GCond strutures to be
Packit ae235b
 *     ABI-compatible with SRWLock and CONDITION_VARIABLE and using
Packit ae235b
 *     those instead
Packit ae235b
 *
Packit ae235b
 *   - avoid a hard dependency on the symbols used to manipulate these
Packit ae235b
 *     structures by doing a dynamic lookup of those symbols at
Packit ae235b
 *     runtime
Packit ae235b
 *
Packit ae235b
 *   - if the symbols are not available, emulate them using other
Packit ae235b
 *     primatives
Packit ae235b
 *
Packit ae235b
 * Using this approach also allows us to easily build a GLib that lacks
Packit ae235b
 * support for Windows XP or to remove this code entirely when XP is no
Packit ae235b
 * longer supported (end of line is currently April 8, 2014).
Packit ae235b
 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  void     (__stdcall * CallThisOnThreadExit)        (void);              /* fake */
Packit ae235b
Packit ae235b
  void     (__stdcall * InitializeSRWLock)           (gpointer lock);
Packit ae235b
  void     (__stdcall * DeleteSRWLock)               (gpointer lock);     /* fake */
Packit ae235b
  void     (__stdcall * AcquireSRWLockExclusive)     (gpointer lock);
Packit ae235b
  BOOLEAN  (__stdcall * TryAcquireSRWLockExclusive)  (gpointer lock);
Packit ae235b
  void     (__stdcall * ReleaseSRWLockExclusive)     (gpointer lock);
Packit ae235b
  void     (__stdcall * AcquireSRWLockShared)        (gpointer lock);
Packit ae235b
  BOOLEAN  (__stdcall * TryAcquireSRWLockShared)     (gpointer lock);
Packit ae235b
  void     (__stdcall * ReleaseSRWLockShared)        (gpointer lock);
Packit ae235b
Packit ae235b
  void     (__stdcall * InitializeConditionVariable) (gpointer cond);
Packit ae235b
  void     (__stdcall * DeleteConditionVariable)     (gpointer cond);     /* fake */
Packit ae235b
  BOOL     (__stdcall * SleepConditionVariableSRW)   (gpointer cond,
Packit ae235b
                                                      gpointer lock,
Packit ae235b
                                                      DWORD    timeout,
Packit ae235b
                                                      ULONG    flags);
Packit ae235b
  void     (__stdcall * WakeAllConditionVariable)    (gpointer cond);
Packit ae235b
  void     (__stdcall * WakeConditionVariable)       (gpointer cond);
Packit ae235b
} GThreadImplVtable;
Packit ae235b
Packit ae235b
static GThreadImplVtable g_thread_impl_vtable;
Packit ae235b
Packit ae235b
/* {{{1 GMutex */
Packit ae235b
void
Packit ae235b
g_mutex_init (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.InitializeSRWLock (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_clear (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  if (g_thread_impl_vtable.DeleteSRWLock != NULL)
Packit ae235b
    g_thread_impl_vtable.DeleteSRWLock (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_lock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.AcquireSRWLockExclusive (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_mutex_trylock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  return g_thread_impl_vtable.TryAcquireSRWLockExclusive (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_unlock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.ReleaseSRWLockExclusive (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GRecMutex */
Packit ae235b
Packit ae235b
static CRITICAL_SECTION *
Packit ae235b
g_rec_mutex_impl_new (void)
Packit ae235b
{
Packit ae235b
  CRITICAL_SECTION *cs;
Packit ae235b
Packit ae235b
  cs = g_slice_new (CRITICAL_SECTION);
Packit ae235b
  InitializeCriticalSection (cs);
Packit ae235b
Packit ae235b
  return cs;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_rec_mutex_impl_free (CRITICAL_SECTION *cs)
Packit ae235b
{
Packit ae235b
  DeleteCriticalSection (cs);
Packit ae235b
  g_slice_free (CRITICAL_SECTION, cs);
Packit ae235b
}
Packit ae235b
Packit ae235b
static CRITICAL_SECTION *
Packit ae235b
g_rec_mutex_get_impl (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  CRITICAL_SECTION *impl = mutex->p;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (mutex->p == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_rec_mutex_impl_new ();
Packit ae235b
      if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL)
Packit ae235b
        g_rec_mutex_impl_free (impl);
Packit ae235b
      impl = mutex->p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rec_mutex_init (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  mutex->p = g_rec_mutex_impl_new ();
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rec_mutex_clear (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_rec_mutex_impl_free (mutex->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rec_mutex_lock (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  EnterCriticalSection (g_rec_mutex_get_impl (mutex));
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rec_mutex_unlock (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  LeaveCriticalSection (mutex->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_rec_mutex_trylock (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  return TryEnterCriticalSection (g_rec_mutex_get_impl (mutex));
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GRWLock */
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_init (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.InitializeSRWLock (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_clear (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  if (g_thread_impl_vtable.DeleteSRWLock != NULL)
Packit ae235b
    g_thread_impl_vtable.DeleteSRWLock (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_writer_lock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.AcquireSRWLockExclusive (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_rw_lock_writer_trylock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  return g_thread_impl_vtable.TryAcquireSRWLockExclusive (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_writer_unlock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.ReleaseSRWLockExclusive (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_reader_lock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.AcquireSRWLockShared (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_rw_lock_reader_trylock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  return g_thread_impl_vtable.TryAcquireSRWLockShared (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_rw_lock_reader_unlock (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.ReleaseSRWLockShared (lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GCond */
Packit ae235b
void
Packit ae235b
g_cond_init (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.InitializeConditionVariable (cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_clear (GCond *cond)
Packit ae235b
{
Packit ae235b
  if (g_thread_impl_vtable.DeleteConditionVariable)
Packit ae235b
    g_thread_impl_vtable.DeleteConditionVariable (cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_signal (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.WakeConditionVariable (cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_broadcast (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.WakeAllConditionVariable (cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_wait (GCond  *cond,
Packit ae235b
             GMutex *entered_mutex)
Packit ae235b
{
Packit ae235b
  g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, INFINITE, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_cond_wait_until (GCond  *cond,
Packit ae235b
                   GMutex *entered_mutex,
Packit ae235b
                   gint64  end_time)
Packit ae235b
{
Packit ae235b
  gint64 span;
Packit ae235b
Packit ae235b
  span = end_time - g_get_monotonic_time ();
Packit ae235b
Packit ae235b
  if G_UNLIKELY (span < 0)
Packit ae235b
    span = 0;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * G_MAXINT32)
Packit ae235b
    span = INFINITE;
Packit ae235b
Packit ae235b
  return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GPrivate */
Packit ae235b
Packit ae235b
typedef struct _GPrivateDestructor GPrivateDestructor;
Packit ae235b
Packit ae235b
struct _GPrivateDestructor
Packit ae235b
{
Packit ae235b
  DWORD               index;
Packit ae235b
  GDestroyNotify      notify;
Packit ae235b
  GPrivateDestructor *next;
Packit ae235b
};
Packit ae235b
Packit ae235b
static GPrivateDestructor * volatile g_private_destructors;
Packit ae235b
static CRITICAL_SECTION g_private_lock;
Packit ae235b
Packit ae235b
static DWORD
Packit ae235b
g_private_get_impl (GPrivate *key)
Packit ae235b
{
Packit ae235b
  DWORD impl = (DWORD) key->p;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == 0)
Packit ae235b
    {
Packit ae235b
      EnterCriticalSection (&g_private_lock);
Packit ae235b
      impl = (DWORD) key->p;
Packit ae235b
      if (impl == 0)
Packit ae235b
        {
Packit ae235b
          GPrivateDestructor *destructor;
Packit ae235b
Packit ae235b
          impl = TlsAlloc ();
Packit ae235b
Packit ae235b
          if (impl == TLS_OUT_OF_INDEXES)
Packit ae235b
            g_thread_abort (0, "TlsAlloc");
Packit ae235b
Packit ae235b
          if (key->notify != NULL)
Packit ae235b
            {
Packit ae235b
              destructor = malloc (sizeof (GPrivateDestructor));
Packit ae235b
              if G_UNLIKELY (destructor == NULL)
Packit ae235b
                g_thread_abort (errno, "malloc");
Packit ae235b
              destructor->index = impl;
Packit ae235b
              destructor->notify = key->notify;
Packit ae235b
              destructor->next = g_private_destructors;
Packit ae235b
Packit ae235b
              /* We need to do an atomic store due to the unlocked
Packit ae235b
               * access to the destructor list from the thread exit
Packit ae235b
               * function.
Packit ae235b
               *
Packit ae235b
               * It can double as a sanity check...
Packit ae235b
               */
Packit ae235b
              if (InterlockedCompareExchangePointer (&g_private_destructors, destructor,
Packit ae235b
                                                     destructor->next) != destructor->next)
Packit ae235b
                g_thread_abort (0, "g_private_get_impl(1)");
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* Ditto, due to the unlocked access on the fast path */
Packit ae235b
          if (InterlockedCompareExchangePointer (&key->p, impl, NULL) != NULL)
Packit ae235b
            g_thread_abort (0, "g_private_get_impl(2)");
Packit ae235b
        }
Packit ae235b
      LeaveCriticalSection (&g_private_lock);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
g_private_get (GPrivate *key)
Packit ae235b
{
Packit ae235b
  return TlsGetValue (g_private_get_impl (key));
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_private_set (GPrivate *key,
Packit ae235b
               gpointer  value)
Packit ae235b
{
Packit ae235b
  TlsSetValue (g_private_get_impl (key), value);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_private_replace (GPrivate *key,
Packit ae235b
                   gpointer  value)
Packit ae235b
{
Packit ae235b
  DWORD impl = g_private_get_impl (key);
Packit ae235b
  gpointer old;
Packit ae235b
Packit ae235b
  old = TlsGetValue (impl);
Packit ae235b
  if (old && key->notify)
Packit ae235b
    key->notify (old);
Packit ae235b
  TlsSetValue (impl, value);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GThread */
Packit ae235b
Packit ae235b
#define win32_check_for_error(what) G_STMT_START{			\
Packit ae235b
  if (!(what))								\
Packit ae235b
    g_error ("file %s: line %d (%s): error %s during %s",		\
Packit ae235b
	     __FILE__, __LINE__, G_STRFUNC,				\
Packit ae235b
	     g_win32_error_message (GetLastError ()), #what);		\
Packit ae235b
  }G_STMT_END
Packit ae235b
Packit ae235b
#define G_MUTEX_SIZE (sizeof (gpointer))
Packit ae235b
Packit ae235b
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GRealThread thread;
Packit ae235b
Packit ae235b
  GThreadFunc proxy;
Packit ae235b
  HANDLE      handle;
Packit ae235b
} GThreadWin32;
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_free (GRealThread *thread)
Packit ae235b
{
Packit ae235b
  GThreadWin32 *wt = (GThreadWin32 *) thread;
Packit ae235b
Packit ae235b
  win32_check_for_error (CloseHandle (wt->handle));
Packit ae235b
  g_slice_free (GThreadWin32, wt);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_exit (void)
Packit ae235b
{
Packit ae235b
  _endthreadex (0);
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint __stdcall
Packit ae235b
g_thread_win32_proxy (gpointer data)
Packit ae235b
{
Packit ae235b
  GThreadWin32 *self = data;
Packit ae235b
Packit ae235b
  self->proxy (self);
Packit ae235b
Packit ae235b
  g_system_thread_exit ();
Packit ae235b
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
GRealThread *
Packit ae235b
g_system_thread_new (GThreadFunc   func,
Packit ae235b
                     gulong        stack_size,
Packit ae235b
                     GError      **error)
Packit ae235b
{
Packit ae235b
  GThreadWin32 *thread;
Packit ae235b
  guint ignore;
Packit ae235b
Packit ae235b
  thread = g_slice_new0 (GThreadWin32);
Packit ae235b
  thread->proxy = func;
Packit ae235b
Packit ae235b
  thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread, 0, &ignore);
Packit ae235b
Packit ae235b
  if (thread->handle == NULL)
Packit ae235b
    {
Packit ae235b
      gchar *win_error = g_win32_error_message (GetLastError ());
Packit ae235b
      g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
Packit ae235b
                   "Error creating thread: %s", win_error);
Packit ae235b
      g_free (win_error);
Packit ae235b
      g_slice_free (GThreadWin32, thread);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (GRealThread *) thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_thread_yield (void)
Packit ae235b
{
Packit ae235b
  Sleep(0);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_wait (GRealThread *thread)
Packit ae235b
{
Packit ae235b
  GThreadWin32 *wt = (GThreadWin32 *) thread;
Packit ae235b
Packit ae235b
  win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
Packit ae235b
}
Packit ae235b
Packit ae235b
#define EXCEPTION_SET_THREAD_NAME ((DWORD) 0x406D1388)
Packit ae235b
Packit ae235b
#ifndef _MSC_VER
Packit ae235b
static void *SetThreadName_VEH_handle = NULL;
Packit ae235b
Packit ae235b
static LONG __stdcall
Packit ae235b
SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
Packit ae235b
{
Packit ae235b
  if (ExceptionInfo->ExceptionRecord != NULL &&
Packit ae235b
      ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SET_THREAD_NAME)
Packit ae235b
    return EXCEPTION_CONTINUE_EXECUTION;
Packit ae235b
Packit ae235b
  return EXCEPTION_CONTINUE_SEARCH;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
typedef struct _THREADNAME_INFO
Packit ae235b
{
Packit ae235b
  DWORD  dwType;	/* must be 0x1000 */
Packit ae235b
  LPCSTR szName;	/* pointer to name (in user addr space) */
Packit ae235b
  DWORD  dwThreadID;	/* thread ID (-1=caller thread) */
Packit ae235b
  DWORD  dwFlags;	/* reserved for future use, must be zero */
Packit ae235b
} THREADNAME_INFO;
Packit ae235b
Packit ae235b
static void
Packit ae235b
SetThreadName (DWORD  dwThreadID,
Packit ae235b
               LPCSTR szThreadName)
Packit ae235b
{
Packit ae235b
   THREADNAME_INFO info;
Packit ae235b
   DWORD infosize;
Packit ae235b
Packit ae235b
   info.dwType = 0x1000;
Packit ae235b
   info.szName = szThreadName;
Packit ae235b
   info.dwThreadID = dwThreadID;
Packit ae235b
   info.dwFlags = 0;
Packit ae235b
Packit ae235b
   infosize = sizeof (info) / sizeof (DWORD);
Packit ae235b
Packit ae235b
#ifdef _MSC_VER
Packit ae235b
   __try
Packit ae235b
     {
Packit ae235b
       RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info;;
Packit ae235b
     }
Packit ae235b
   __except (EXCEPTION_EXECUTE_HANDLER)
Packit ae235b
     {
Packit ae235b
     }
Packit ae235b
#else
Packit ae235b
   /* Without a debugger we *must* have an exception handler,
Packit ae235b
    * otherwise raising an exception will crash the process.
Packit ae235b
    */
Packit ae235b
   if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL))
Packit ae235b
     return;
Packit ae235b
Packit ae235b
   RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info;;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_set_name (const gchar *name)
Packit ae235b
{
Packit ae235b
  SetThreadName ((DWORD) -1, name);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
Packit ae235b
Packit ae235b
static CRITICAL_SECTION g_thread_xp_lock;
Packit ae235b
static DWORD            g_thread_xp_waiter_tls;
Packit ae235b
Packit ae235b
/* {{{2 GThreadWaiter utility class for CONDITION_VARIABLE emulation */
Packit ae235b
typedef struct _GThreadXpWaiter GThreadXpWaiter;
Packit ae235b
struct _GThreadXpWaiter
Packit ae235b
{
Packit ae235b
  HANDLE                     event;
Packit ae235b
  volatile GThreadXpWaiter  *next;
Packit ae235b
  volatile GThreadXpWaiter **my_owner;
Packit ae235b
};
Packit ae235b
Packit ae235b
static GThreadXpWaiter *
Packit ae235b
g_thread_xp_waiter_get (void)
Packit ae235b
{
Packit ae235b
  GThreadXpWaiter *waiter;
Packit ae235b
Packit ae235b
  waiter = TlsGetValue (g_thread_xp_waiter_tls);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (waiter == NULL)
Packit ae235b
    {
Packit ae235b
      waiter = malloc (sizeof (GThreadXpWaiter));
Packit ae235b
      if (waiter == NULL)
Packit ae235b
        g_thread_abort (GetLastError (), "malloc");
Packit ae235b
      waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
Packit ae235b
      if (waiter->event == NULL)
Packit ae235b
        g_thread_abort (GetLastError (), "CreateEvent");
Packit ae235b
      waiter->my_owner = NULL;
Packit ae235b
Packit ae235b
      TlsSetValue (g_thread_xp_waiter_tls, waiter);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return waiter;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_CallThisOnThreadExit (void)
Packit ae235b
{
Packit ae235b
  GThreadXpWaiter *waiter;
Packit ae235b
Packit ae235b
  waiter = TlsGetValue (g_thread_xp_waiter_tls);
Packit ae235b
Packit ae235b
  if (waiter != NULL)
Packit ae235b
    {
Packit ae235b
      TlsSetValue (g_thread_xp_waiter_tls, NULL);
Packit ae235b
      CloseHandle (waiter->event);
Packit ae235b
      free (waiter);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{2 SRWLock emulation */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  CRITICAL_SECTION  writer_lock;
Packit ae235b
  gboolean          ever_shared;    /* protected by writer_lock */
Packit ae235b
  gboolean          writer_locked;  /* protected by writer_lock */
Packit ae235b
Packit ae235b
  /* below is only ever touched if ever_shared becomes true */
Packit ae235b
  CRITICAL_SECTION  atomicity;
Packit ae235b
  GThreadXpWaiter  *queued_writer; /* protected by atomicity lock */
Packit ae235b
  gint              num_readers;   /* protected by atomicity lock */
Packit ae235b
} GThreadSRWLock;
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_InitializeSRWLock (gpointer mutex)
Packit ae235b
{
Packit ae235b
  *(GThreadSRWLock * volatile *) mutex = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_DeleteSRWLock (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
Packit ae235b
Packit ae235b
  if (lock)
Packit ae235b
    {
Packit ae235b
      if (lock->ever_shared)
Packit ae235b
        DeleteCriticalSection (&lock->atomicity);
Packit ae235b
Packit ae235b
      DeleteCriticalSection (&lock->writer_lock);
Packit ae235b
      free (lock);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GThreadSRWLock * __stdcall
Packit ae235b
g_thread_xp_get_srwlock (GThreadSRWLock * volatile *lock)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *result;
Packit ae235b
Packit ae235b
  /* It looks like we're missing some barriers here, but this code only
Packit ae235b
   * ever runs on Windows XP, which in turn only ever runs on hardware
Packit ae235b
   * with a relatively rigid memory model.  The 'volatile' will take
Packit ae235b
   * care of the compiler.
Packit ae235b
   */
Packit ae235b
  result = *lock;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (result == NULL)
Packit ae235b
    {
Packit ae235b
      EnterCriticalSection (&g_thread_xp_lock);
Packit ae235b
Packit ae235b
      /* Check again */
Packit ae235b
      result = *lock;
Packit ae235b
      if (result == NULL)
Packit ae235b
        {
Packit ae235b
          result = malloc (sizeof (GThreadSRWLock));
Packit ae235b
Packit ae235b
          if (result == NULL)
Packit ae235b
            g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
          InitializeCriticalSection (&result->writer_lock);
Packit ae235b
          result->writer_locked = FALSE;
Packit ae235b
          result->ever_shared = FALSE;
Packit ae235b
          *lock = result;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      LeaveCriticalSection (&g_thread_xp_lock);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_AcquireSRWLockExclusive (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
Packit ae235b
Packit ae235b
  EnterCriticalSection (&lock->writer_lock);
Packit ae235b
Packit ae235b
  /* CRITICAL_SECTION is reentrant, but SRWLock is not.
Packit ae235b
   * Detect the deadlock that would occur on later Windows version.
Packit ae235b
   */
Packit ae235b
  g_assert (!lock->writer_locked);
Packit ae235b
  lock->writer_locked = TRUE;
Packit ae235b
Packit ae235b
  if (lock->ever_shared)
Packit ae235b
    {
Packit ae235b
      GThreadXpWaiter *waiter = NULL;
Packit ae235b
Packit ae235b
      EnterCriticalSection (&lock->atomicity);
Packit ae235b
      if (lock->num_readers > 0)
Packit ae235b
        lock->queued_writer = waiter = g_thread_xp_waiter_get ();
Packit ae235b
      LeaveCriticalSection (&lock->atomicity);
Packit ae235b
Packit ae235b
      if (waiter != NULL)
Packit ae235b
        WaitForSingleObject (waiter->event, INFINITE);
Packit ae235b
Packit ae235b
      lock->queued_writer = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static BOOLEAN __stdcall
Packit ae235b
g_thread_xp_TryAcquireSRWLockExclusive (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
Packit ae235b
Packit ae235b
  if (!TryEnterCriticalSection (&lock->writer_lock))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  /* CRITICAL_SECTION is reentrant, but SRWLock is not.
Packit ae235b
   * Ensure that this properly returns FALSE (as SRWLock would).
Packit ae235b
   */
Packit ae235b
  if G_UNLIKELY (lock->writer_locked)
Packit ae235b
    {
Packit ae235b
      LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  lock->writer_locked = TRUE;
Packit ae235b
Packit ae235b
  if (lock->ever_shared)
Packit ae235b
    {
Packit ae235b
      gboolean available;
Packit ae235b
Packit ae235b
      EnterCriticalSection (&lock->atomicity);
Packit ae235b
      available = lock->num_readers == 0;
Packit ae235b
      LeaveCriticalSection (&lock->atomicity);
Packit ae235b
Packit ae235b
      if (!available)
Packit ae235b
        {
Packit ae235b
          LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_ReleaseSRWLockExclusive (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
Packit ae235b
Packit ae235b
  lock->writer_locked = FALSE;
Packit ae235b
Packit ae235b
  /* We need this until we fix some weird parts of GLib that try to
Packit ae235b
   * unlock freshly-allocated mutexes.
Packit ae235b
   */
Packit ae235b
  if (lock != NULL)
Packit ae235b
    LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_xp_srwlock_become_reader (GThreadSRWLock *lock)
Packit ae235b
{
Packit ae235b
  if G_UNLIKELY (!lock->ever_shared)
Packit ae235b
    {
Packit ae235b
      InitializeCriticalSection (&lock->atomicity);
Packit ae235b
      lock->queued_writer = NULL;
Packit ae235b
      lock->num_readers = 0;
Packit ae235b
Packit ae235b
      lock->ever_shared = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  EnterCriticalSection (&lock->atomicity);
Packit ae235b
  lock->num_readers++;
Packit ae235b
  LeaveCriticalSection (&lock->atomicity);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_AcquireSRWLockShared (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
Packit ae235b
Packit ae235b
  EnterCriticalSection (&lock->writer_lock);
Packit ae235b
Packit ae235b
  /* See g_thread_xp_AcquireSRWLockExclusive */
Packit ae235b
  g_assert (!lock->writer_locked);
Packit ae235b
Packit ae235b
  g_thread_xp_srwlock_become_reader (lock);
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
static BOOLEAN __stdcall
Packit ae235b
g_thread_xp_TryAcquireSRWLockShared (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
Packit ae235b
Packit ae235b
  if (!TryEnterCriticalSection (&lock->writer_lock))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  /* See g_thread_xp_AcquireSRWLockExclusive */
Packit ae235b
  if G_UNLIKELY (lock->writer_locked)
Packit ae235b
    {
Packit ae235b
      LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_thread_xp_srwlock_become_reader (lock);
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&lock->writer_lock);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_ReleaseSRWLockShared (gpointer mutex)
Packit ae235b
{
Packit ae235b
  GThreadSRWLock *lock = g_thread_xp_get_srwlock (mutex);
Packit ae235b
Packit ae235b
  EnterCriticalSection (&lock->atomicity);
Packit ae235b
Packit ae235b
  lock->num_readers--;
Packit ae235b
Packit ae235b
  if (lock->num_readers == 0 && lock->queued_writer)
Packit ae235b
    SetEvent (lock->queued_writer->event);
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&lock->atomicity);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{2 CONDITION_VARIABLE emulation */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  volatile GThreadXpWaiter  *first;
Packit ae235b
  volatile GThreadXpWaiter **last_ptr;
Packit ae235b
} GThreadXpCONDITION_VARIABLE;
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_InitializeConditionVariable (gpointer cond)
Packit ae235b
{
Packit ae235b
  *(GThreadXpCONDITION_VARIABLE * volatile *) cond = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_DeleteConditionVariable (gpointer cond)
Packit ae235b
{
Packit ae235b
  GThreadXpCONDITION_VARIABLE *cv = *(GThreadXpCONDITION_VARIABLE * volatile *) cond;
Packit ae235b
Packit ae235b
  if (cv)
Packit ae235b
    free (cv);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GThreadXpCONDITION_VARIABLE * __stdcall
Packit ae235b
g_thread_xp_get_condition_variable (GThreadXpCONDITION_VARIABLE * volatile *cond)
Packit ae235b
{
Packit ae235b
  GThreadXpCONDITION_VARIABLE *result;
Packit ae235b
Packit ae235b
  /* It looks like we're missing some barriers here, but this code only
Packit ae235b
   * ever runs on Windows XP, which in turn only ever runs on hardware
Packit ae235b
   * with a relatively rigid memory model.  The 'volatile' will take
Packit ae235b
   * care of the compiler.
Packit ae235b
   */
Packit ae235b
  result = *cond;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (result == NULL)
Packit ae235b
    {
Packit ae235b
      result = malloc (sizeof (GThreadXpCONDITION_VARIABLE));
Packit ae235b
Packit ae235b
      if (result == NULL)
Packit ae235b
        g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
      result->first = NULL;
Packit ae235b
      result->last_ptr = &result->first;
Packit ae235b
Packit ae235b
      if (InterlockedCompareExchangePointer (cond, result, NULL) != NULL)
Packit ae235b
        {
Packit ae235b
          free (result);
Packit ae235b
          result = *cond;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static BOOL __stdcall
Packit ae235b
g_thread_xp_SleepConditionVariableSRW (gpointer cond,
Packit ae235b
                                       gpointer mutex,
Packit ae235b
                                       DWORD    timeout,
Packit ae235b
                                       ULONG    flags)
Packit ae235b
{
Packit ae235b
  GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
Packit ae235b
  GThreadXpWaiter *waiter = g_thread_xp_waiter_get ();
Packit ae235b
  DWORD status;
Packit ae235b
Packit ae235b
  waiter->next = NULL;
Packit ae235b
Packit ae235b
  EnterCriticalSection (&g_thread_xp_lock);
Packit ae235b
  waiter->my_owner = cv->last_ptr;
Packit ae235b
  *cv->last_ptr = waiter;
Packit ae235b
  cv->last_ptr = &waiter->next;
Packit ae235b
  LeaveCriticalSection (&g_thread_xp_lock);
Packit ae235b
Packit ae235b
  g_mutex_unlock (mutex);
Packit ae235b
  status = WaitForSingleObject (waiter->event, timeout);
Packit ae235b
Packit ae235b
  if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
Packit ae235b
    g_thread_abort (GetLastError (), "WaitForSingleObject");
Packit ae235b
  g_mutex_lock (mutex);
Packit ae235b
Packit ae235b
  if (status == WAIT_TIMEOUT)
Packit ae235b
    {
Packit ae235b
      EnterCriticalSection (&g_thread_xp_lock);
Packit ae235b
      if (waiter->my_owner)
Packit ae235b
        {
Packit ae235b
          if (waiter->next)
Packit ae235b
            waiter->next->my_owner = waiter->my_owner;
Packit ae235b
          else
Packit ae235b
            cv->last_ptr = waiter->my_owner;
Packit ae235b
          *waiter->my_owner = waiter->next;
Packit ae235b
          waiter->my_owner = NULL;
Packit ae235b
        }
Packit ae235b
      LeaveCriticalSection (&g_thread_xp_lock);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return status == WAIT_OBJECT_0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_WakeConditionVariable (gpointer cond)
Packit ae235b
{
Packit ae235b
  GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
Packit ae235b
  volatile GThreadXpWaiter *waiter;
Packit ae235b
Packit ae235b
  EnterCriticalSection (&g_thread_xp_lock);
Packit ae235b
Packit ae235b
  waiter = cv->first;
Packit ae235b
  if (waiter != NULL)
Packit ae235b
    {
Packit ae235b
      waiter->my_owner = NULL;
Packit ae235b
      cv->first = waiter->next;
Packit ae235b
      if (cv->first != NULL)
Packit ae235b
        cv->first->my_owner = &cv->first;
Packit ae235b
      else
Packit ae235b
        cv->last_ptr = &cv->first;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (waiter != NULL)
Packit ae235b
    SetEvent (waiter->event);
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&g_thread_xp_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __stdcall
Packit ae235b
g_thread_xp_WakeAllConditionVariable (gpointer cond)
Packit ae235b
{
Packit ae235b
  GThreadXpCONDITION_VARIABLE *cv = g_thread_xp_get_condition_variable (cond);
Packit ae235b
  volatile GThreadXpWaiter *waiter;
Packit ae235b
Packit ae235b
  EnterCriticalSection (&g_thread_xp_lock);
Packit ae235b
Packit ae235b
  waiter = cv->first;
Packit ae235b
  cv->first = NULL;
Packit ae235b
  cv->last_ptr = &cv->first;
Packit ae235b
Packit ae235b
  while (waiter != NULL)
Packit ae235b
    {
Packit ae235b
      volatile GThreadXpWaiter *next;
Packit ae235b
Packit ae235b
      next = waiter->next;
Packit ae235b
      SetEvent (waiter->event);
Packit ae235b
      waiter->my_owner = NULL;
Packit ae235b
      waiter = next;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  LeaveCriticalSection (&g_thread_xp_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{2 XP Setup */
Packit ae235b
static void
Packit ae235b
g_thread_xp_init (void)
Packit ae235b
{
Packit ae235b
  static const GThreadImplVtable g_thread_xp_impl_vtable = {
Packit ae235b
    g_thread_xp_CallThisOnThreadExit,
Packit ae235b
    g_thread_xp_InitializeSRWLock,
Packit ae235b
    g_thread_xp_DeleteSRWLock,
Packit ae235b
    g_thread_xp_AcquireSRWLockExclusive,
Packit ae235b
    g_thread_xp_TryAcquireSRWLockExclusive,
Packit ae235b
    g_thread_xp_ReleaseSRWLockExclusive,
Packit ae235b
    g_thread_xp_AcquireSRWLockShared,
Packit ae235b
    g_thread_xp_TryAcquireSRWLockShared,
Packit ae235b
    g_thread_xp_ReleaseSRWLockShared,
Packit ae235b
    g_thread_xp_InitializeConditionVariable,
Packit ae235b
    g_thread_xp_DeleteConditionVariable,
Packit ae235b
    g_thread_xp_SleepConditionVariableSRW,
Packit ae235b
    g_thread_xp_WakeAllConditionVariable,
Packit ae235b
    g_thread_xp_WakeConditionVariable
Packit ae235b
  };
Packit ae235b
Packit ae235b
  InitializeCriticalSection (&g_thread_xp_lock);
Packit ae235b
  g_thread_xp_waiter_tls = TlsAlloc ();
Packit ae235b
Packit ae235b
  g_thread_impl_vtable = g_thread_xp_impl_vtable;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 Epilogue */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_thread_lookup_native_funcs (void)
Packit ae235b
{
Packit ae235b
  GThreadImplVtable native_vtable = { 0, };
Packit ae235b
  HMODULE kernel32;
Packit ae235b
Packit ae235b
  kernel32 = GetModuleHandle ("KERNEL32.DLL");
Packit ae235b
Packit ae235b
  if (kernel32 == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
#define GET_FUNC(name) if ((native_vtable.name = (void *) GetProcAddress (kernel32, #name)) == NULL) return FALSE
Packit ae235b
  GET_FUNC(InitializeSRWLock);
Packit ae235b
  GET_FUNC(AcquireSRWLockExclusive);
Packit ae235b
  GET_FUNC(TryAcquireSRWLockExclusive);
Packit ae235b
  GET_FUNC(ReleaseSRWLockExclusive);
Packit ae235b
  GET_FUNC(AcquireSRWLockShared);
Packit ae235b
  GET_FUNC(TryAcquireSRWLockShared);
Packit ae235b
  GET_FUNC(ReleaseSRWLockShared);
Packit ae235b
Packit ae235b
  GET_FUNC(InitializeConditionVariable);
Packit ae235b
  GET_FUNC(SleepConditionVariableSRW);
Packit ae235b
  GET_FUNC(WakeAllConditionVariable);
Packit ae235b
  GET_FUNC(WakeConditionVariable);
Packit ae235b
#undef GET_FUNC
Packit ae235b
Packit ae235b
  g_thread_impl_vtable = native_vtable;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_thread_win32_init (void)
Packit ae235b
{
Packit ae235b
  if (!g_thread_lookup_native_funcs ())
Packit ae235b
    g_thread_xp_init ();
Packit ae235b
Packit ae235b
  InitializeCriticalSection (&g_private_lock);
Packit ae235b
Packit ae235b
#ifndef _MSC_VER
Packit ae235b
  SetThreadName_VEH_handle = AddVectoredExceptionHandler (1, &SetThreadName_VEH);
Packit ae235b
  if (SetThreadName_VEH_handle == NULL)
Packit ae235b
    {
Packit ae235b
      /* This is bad, but what can we do? */
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_thread_win32_thread_detach (void)
Packit ae235b
{
Packit ae235b
  gboolean dtors_called;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      GPrivateDestructor *dtor;
Packit ae235b
Packit ae235b
      /* We go by the POSIX book on this one.
Packit ae235b
       *
Packit ae235b
       * If we call a destructor then there is a chance that some new
Packit ae235b
       * TLS variables got set by code called in that destructor.
Packit ae235b
       *
Packit ae235b
       * Loop until nothing is left.
Packit ae235b
       */
Packit ae235b
      dtors_called = FALSE;
Packit ae235b
Packit ae235b
      for (dtor = g_private_destructors; dtor; dtor = dtor->next)
Packit ae235b
        {
Packit ae235b
          gpointer value;
Packit ae235b
Packit ae235b
          value = TlsGetValue (dtor->index);
Packit ae235b
          if (value != NULL && dtor->notify != NULL)
Packit ae235b
            {
Packit ae235b
              /* POSIX says to clear this before the call */
Packit ae235b
              TlsSetValue (dtor->index, NULL);
Packit ae235b
              dtor->notify (value);
Packit ae235b
              dtors_called = TRUE;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  while (dtors_called);
Packit ae235b
Packit ae235b
  if (g_thread_impl_vtable.CallThisOnThreadExit)
Packit ae235b
    g_thread_impl_vtable.CallThisOnThreadExit ();
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_thread_win32_process_detach (void)
Packit ae235b
{
Packit ae235b
#ifndef _MSC_VER
Packit ae235b
  if (SetThreadName_VEH_handle != NULL)
Packit ae235b
    {
Packit ae235b
      RemoveVectoredExceptionHandler (SetThreadName_VEH_handle);
Packit ae235b
      SetThreadName_VEH_handle = NULL;
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* vim:set foldmethod=marker: */