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
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-05-22 14:28:06 +02:00
|
|
|
*
|
|
|
|
* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2001-05-22 14:28:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-10-17 08:14:18 +02:00
|
|
|
#include "glib.h"
|
2012-11-10 16:58:19 +01:00
|
|
|
#include "glib-init.h"
|
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);
|
2016-03-30 16:01:30 +02:00
|
|
|
g_abort ();
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
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
|
2020-06-12 15:02:30 +02:00
|
|
|
* CONDITION_VARIABLE and SRWLock primitives on Windows, which are
|
|
|
|
* pretty reasonable approximations of the primitives specified in
|
2011-09-17 00:05:23 +02:00
|
|
|
* 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
|
2011-10-03 02:51:38 +02:00
|
|
|
* zero. That means that they are completely ABI compatible with our
|
|
|
|
* GMutex and GCond APIs.
|
2011-09-17 00:05:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* {{{1 GMutex */
|
|
|
|
void
|
|
|
|
g_mutex_init (GMutex *mutex)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
InitializeSRWLock ((gpointer) mutex);
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
AcquireSRWLockExclusive ((gpointer) 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
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
return TryAcquireSRWLockExclusive ((gpointer) 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
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
ReleaseSRWLockExclusive ((gpointer) 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)
|
|
|
|
{
|
2011-10-03 02:59:15 +02:00
|
|
|
CRITICAL_SECTION *impl = mutex->p;
|
2011-09-21 20:36:53 +02:00
|
|
|
|
2011-10-03 02:59:15 +02:00
|
|
|
if G_UNLIKELY (mutex->p == NULL)
|
2011-09-21 20:36:53 +02:00
|
|
|
{
|
|
|
|
impl = g_rec_mutex_impl_new ();
|
2011-10-03 02:59:15 +02:00
|
|
|
if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL)
|
2011-09-21 20:36:53 +02:00
|
|
|
g_rec_mutex_impl_free (impl);
|
2011-10-03 02:59:15 +02:00
|
|
|
impl = mutex->p;
|
2011-09-21 20:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_init (GRecMutex *mutex)
|
|
|
|
{
|
2011-10-03 02:59:15 +02:00
|
|
|
mutex->p = g_rec_mutex_impl_new ();
|
2011-09-21 20:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_clear (GRecMutex *mutex)
|
|
|
|
{
|
2011-10-03 02:59:15 +02:00
|
|
|
g_rec_mutex_impl_free (mutex->p);
|
2011-09-21 20:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_lock (GRecMutex *mutex)
|
|
|
|
{
|
|
|
|
EnterCriticalSection (g_rec_mutex_get_impl (mutex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rec_mutex_unlock (GRecMutex *mutex)
|
|
|
|
{
|
2011-10-03 02:59:15 +02:00
|
|
|
LeaveCriticalSection (mutex->p);
|
2011-09-21 20:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
InitializeSRWLock ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_clear (GRWLock *lock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_writer_lock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
AcquireSRWLockExclusive ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
g_rw_lock_writer_trylock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
return TryAcquireSRWLockExclusive ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_writer_unlock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
ReleaseSRWLockExclusive ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_reader_lock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
AcquireSRWLockShared ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
g_rw_lock_reader_trylock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
return TryAcquireSRWLockShared ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_rw_lock_reader_unlock (GRWLock *lock)
|
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
ReleaseSRWLockShared ((gpointer) lock);
|
2011-09-21 16:19:36 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{1 GCond */
|
|
|
|
void
|
|
|
|
g_cond_init (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
InitializeConditionVariable ((gpointer) 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
|
|
|
void
|
|
|
|
g_cond_signal (GCond *cond)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
WakeConditionVariable ((gpointer) 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
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
WakeAllConditionVariable ((gpointer) 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
|
|
|
{
|
2018-05-29 17:10:05 +02:00
|
|
|
SleepConditionVariableSRW ((gpointer) cond, (gpointer) entered_mutex, INFINITE, 0);
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
gboolean
|
2011-10-14 05:24:23 +02:00
|
|
|
g_cond_wait_until (GCond *cond,
|
|
|
|
GMutex *entered_mutex,
|
|
|
|
gint64 end_time)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2018-05-10 13:28:49 +02:00
|
|
|
gint64 span, start_time;
|
|
|
|
DWORD span_millis;
|
|
|
|
gboolean signalled;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2018-05-10 13:28:49 +02:00
|
|
|
start_time = g_get_monotonic_time ();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
span = end_time - start_time;
|
|
|
|
|
|
|
|
if G_UNLIKELY (span < 0)
|
|
|
|
span_millis = 0;
|
|
|
|
else if G_UNLIKELY (span > G_GINT64_CONSTANT (1000) * (DWORD) INFINITE)
|
|
|
|
span_millis = INFINITE;
|
|
|
|
else
|
|
|
|
/* Round up so we don't time out too early */
|
|
|
|
span_millis = (span + 1000 - 1) / 1000;
|
|
|
|
|
|
|
|
/* We never want to wait infinitely */
|
|
|
|
if (span_millis >= INFINITE)
|
|
|
|
span_millis = INFINITE - 1;
|
|
|
|
|
2018-05-29 17:10:05 +02:00
|
|
|
signalled = SleepConditionVariableSRW ((gpointer) cond, (gpointer) entered_mutex, span_millis, 0);
|
2018-05-10 13:28:49 +02:00
|
|
|
if (signalled)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* In case we didn't wait long enough after a timeout, wait again for the
|
|
|
|
* remaining time */
|
|
|
|
start_time = g_get_monotonic_time ();
|
|
|
|
}
|
|
|
|
while (start_time < end_time);
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2018-05-10 13:28:49 +02:00
|
|
|
return signalled;
|
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;
|
|
|
|
};
|
|
|
|
|
2020-11-16 17:44:29 +01:00
|
|
|
static GPrivateDestructor *g_private_destructors; /* (atomic) prepend-only */
|
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
|
|
|
{
|
2021-05-09 23:23:31 +02:00
|
|
|
DWORD impl = (DWORD) GPOINTER_TO_UINT(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);
|
2021-05-09 23:23:31 +02:00
|
|
|
impl = (UINT_PTR) key->p;
|
2011-09-30 20:22:04 +02:00
|
|
|
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
|
|
|
|
2020-05-13 10:29:13 +02:00
|
|
|
if G_UNLIKELY (impl == 0)
|
|
|
|
{
|
|
|
|
/* Ignore TLS index 0 temporarily (as 0 is the indicator that we
|
|
|
|
* haven't allocated TLS yet) and alloc again;
|
|
|
|
* See https://gitlab.gnome.org/GNOME/glib/-/issues/2058 */
|
|
|
|
DWORD impl2 = TlsAlloc ();
|
|
|
|
TlsFree (impl);
|
|
|
|
impl = impl2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (impl == TLS_OUT_OF_INDEXES || impl == 0)
|
2011-09-30 20:22:04 +02:00
|
|
|
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;
|
2020-11-16 17:44:29 +01:00
|
|
|
destructor->next = g_atomic_pointer_get (&g_private_destructors);
|
2011-09-30 20:22:04 +02:00
|
|
|
|
|
|
|
/* 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...
|
|
|
|
*/
|
2020-11-16 17:44:29 +01:00
|
|
|
if (!g_atomic_pointer_compare_and_exchange (&g_private_destructors,
|
|
|
|
destructor->next,
|
|
|
|
destructor))
|
2011-09-30 20:22:04 +02:00
|
|
|
g_thread_abort (0, "g_private_get_impl(1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ditto, due to the unlocked access on the fast path */
|
2020-11-16 17:44:29 +01:00
|
|
|
if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
|
2011-09-30 20:22:04 +02:00
|
|
|
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);
|
2020-09-30 17:16:11 +02:00
|
|
|
TlsSetValue (impl, value);
|
2011-09-30 20:22:04 +02:00
|
|
|
if (old && key->notify)
|
|
|
|
key->notify (old);
|
2011-09-18 04:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{1 GThread */
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
|
|
|
|
|
2011-10-13 04:53:52 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GRealThread thread;
|
|
|
|
|
2011-10-13 05:22:31 +02:00
|
|
|
GThreadFunc proxy;
|
|
|
|
HANDLE handle;
|
2011-10-13 04:53:52 +02:00
|
|
|
} GThreadWin32;
|
|
|
|
|
2011-10-13 04:03:14 +02:00
|
|
|
void
|
|
|
|
g_system_thread_free (GRealThread *thread)
|
|
|
|
{
|
2011-10-13 04:53:52 +02:00
|
|
|
GThreadWin32 *wt = (GThreadWin32 *) thread;
|
|
|
|
|
2011-10-13 05:22:31 +02:00
|
|
|
win32_check_for_error (CloseHandle (wt->handle));
|
2011-10-13 04:53:52 +02:00
|
|
|
g_slice_free (GThreadWin32, wt);
|
2011-10-13 04:03:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 05:18:17 +02:00
|
|
|
void
|
|
|
|
g_system_thread_exit (void)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2022-01-19 14:19:05 +01:00
|
|
|
/* In static compilation, DllMain doesn't exist and so DLL_THREAD_DETACH
|
|
|
|
* case is never called and thread destroy notifications are not triggered.
|
|
|
|
* To ensure that notifications are correctly triggered in static
|
|
|
|
* compilation mode, we call directly the "detach" function here right
|
|
|
|
* before terminating the thread.
|
|
|
|
* As all win32 threads initialized through the glib API are run through
|
|
|
|
* the same proxy function g_thread_win32_proxy() which calls systematically
|
|
|
|
* g_system_thread_exit() when finishing, we obtain the same behavior as
|
|
|
|
* with dynamic compilation.
|
|
|
|
*
|
|
|
|
* WARNING: unfortunately this mechanism cannot work with threads created
|
|
|
|
* directly from the Windows API using CreateThread() or _beginthread/ex().
|
|
|
|
* It only works with threads created by using the glib API with
|
|
|
|
* g_system_thread_new(). If users need absolutely to use a thread NOT
|
|
|
|
* created with glib API under Windows and in static compilation mode, they
|
|
|
|
* should not use glib functions within their thread or they may encounter
|
|
|
|
* memory leaks when the thread finishes.
|
|
|
|
*/
|
|
|
|
#ifdef GLIB_STATIC_COMPILATION
|
|
|
|
g_thread_win32_thread_detach ();
|
|
|
|
#endif
|
|
|
|
|
2001-05-22 14:28:06 +02:00
|
|
|
_endthreadex (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint __stdcall
|
2011-10-13 04:49:39 +02:00
|
|
|
g_thread_win32_proxy (gpointer data)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-10-13 05:22:31 +02:00
|
|
|
GThreadWin32 *self = data;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-10-13 05:22:31 +02:00
|
|
|
self->proxy (self);
|
2001-05-22 14:28:06 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:23:30 +01:00
|
|
|
gboolean
|
2019-12-24 14:33:30 +01:00
|
|
|
g_system_thread_get_scheduler_settings (GThreadSchedulerSettings *scheduler_settings)
|
|
|
|
{
|
|
|
|
HANDLE current_thread = GetCurrentThread ();
|
|
|
|
scheduler_settings->thread_prio = GetThreadPriority (current_thread);
|
2020-01-18 12:23:30 +01:00
|
|
|
|
|
|
|
return TRUE;
|
2019-12-24 14:33:30 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 04:29:13 +02:00
|
|
|
GRealThread *
|
2019-12-24 14:33:30 +01:00
|
|
|
g_system_thread_new (GThreadFunc proxy,
|
|
|
|
gulong stack_size,
|
|
|
|
const GThreadSchedulerSettings *scheduler_settings,
|
|
|
|
const char *name,
|
|
|
|
GThreadFunc func,
|
|
|
|
gpointer data,
|
|
|
|
GError **error)
|
2006-05-11 02:08:31 +02:00
|
|
|
{
|
2011-10-13 04:53:52 +02:00
|
|
|
GThreadWin32 *thread;
|
2016-11-17 23:17:30 +01:00
|
|
|
GRealThread *base_thread;
|
2001-05-22 14:28:06 +02:00
|
|
|
guint ignore;
|
2019-09-17 16:27:50 +02:00
|
|
|
const gchar *message = NULL;
|
2019-12-24 14:33:30 +01:00
|
|
|
int thread_prio;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-10-13 04:53:52 +02:00
|
|
|
thread = g_slice_new0 (GThreadWin32);
|
2016-11-17 23:17:30 +01:00
|
|
|
thread->proxy = proxy;
|
2019-09-17 16:27:50 +02:00
|
|
|
thread->handle = (HANDLE) NULL;
|
2016-11-17 23:17:30 +01:00
|
|
|
base_thread = (GRealThread*)thread;
|
|
|
|
base_thread->ref_count = 2;
|
|
|
|
base_thread->ours = TRUE;
|
|
|
|
base_thread->thread.joinable = TRUE;
|
|
|
|
base_thread->thread.func = func;
|
|
|
|
base_thread->thread.data = data;
|
|
|
|
base_thread->name = g_strdup (name);
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2019-09-17 16:27:50 +02:00
|
|
|
thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread,
|
|
|
|
CREATE_SUSPENDED, &ignore);
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-10-13 05:22:31 +02:00
|
|
|
if (thread->handle == NULL)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2019-09-17 16:27:50 +02:00
|
|
|
message = "Error creating thread";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For thread priority inheritance we need to manually set the thread
|
|
|
|
* priority of the new thread to the priority of the current thread. We
|
|
|
|
* also have to start the thread suspended and resume it after actually
|
|
|
|
* setting the priority here.
|
|
|
|
*
|
|
|
|
* On Windows, by default all new threads are created with NORMAL thread
|
|
|
|
* priority.
|
|
|
|
*/
|
|
|
|
|
2019-12-24 14:33:30 +01:00
|
|
|
if (scheduler_settings)
|
|
|
|
{
|
|
|
|
thread_prio = scheduler_settings->thread_prio;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HANDLE current_thread = GetCurrentThread ();
|
|
|
|
thread_prio = GetThreadPriority (current_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread_prio == THREAD_PRIORITY_ERROR_RETURN)
|
2019-09-17 16:27:50 +02:00
|
|
|
{
|
|
|
|
message = "Error getting current thread priority";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-12-24 14:33:30 +01:00
|
|
|
if (SetThreadPriority (thread->handle, thread_prio) == 0)
|
2019-09-17 16:27:50 +02:00
|
|
|
{
|
|
|
|
message = "Error setting new thread priority";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:33:00 +02:00
|
|
|
if (ResumeThread (thread->handle) == (DWORD) -1)
|
2019-09-17 16:27:50 +02:00
|
|
|
{
|
|
|
|
message = "Error resuming new thread";
|
|
|
|
goto error;
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2011-10-13 04:53:52 +02:00
|
|
|
return (GRealThread *) thread;
|
2019-09-17 16:27:50 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
gchar *win_error = g_win32_error_message (GetLastError ());
|
|
|
|
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
|
|
|
|
"%s: %s", message, win_error);
|
|
|
|
g_free (win_error);
|
|
|
|
if (thread->handle)
|
|
|
|
CloseHandle (thread->handle);
|
|
|
|
g_slice_free (GThreadWin32, thread);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
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
|
2011-10-13 04:15:46 +02:00
|
|
|
g_system_thread_wait (GRealThread *thread)
|
2001-05-22 14:28:06 +02:00
|
|
|
{
|
2011-10-13 04:53:52 +02:00
|
|
|
GThreadWin32 *wt = (GThreadWin32 *) thread;
|
2001-05-22 14:28:06 +02:00
|
|
|
|
2011-10-13 05:22:31 +02:00
|
|
|
win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
|
2001-05-22 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:05:45 +02:00
|
|
|
#define EXCEPTION_SET_THREAD_NAME ((DWORD) 0x406D1388)
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
static void *SetThreadName_VEH_handle = NULL;
|
|
|
|
|
|
|
|
static LONG __stdcall
|
|
|
|
SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
|
|
|
|
{
|
|
|
|
if (ExceptionInfo->ExceptionRecord != NULL &&
|
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SET_THREAD_NAME)
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct _THREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType; /* must be 0x1000 */
|
|
|
|
LPCSTR szName; /* pointer to name (in user addr space) */
|
|
|
|
DWORD dwThreadID; /* thread ID (-1=caller thread) */
|
|
|
|
DWORD dwFlags; /* reserved for future use, must be zero */
|
|
|
|
} THREADNAME_INFO;
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetThreadName (DWORD dwThreadID,
|
|
|
|
LPCSTR szThreadName)
|
|
|
|
{
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
DWORD infosize;
|
|
|
|
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = szThreadName;
|
|
|
|
info.dwThreadID = dwThreadID;
|
|
|
|
info.dwFlags = 0;
|
|
|
|
|
|
|
|
infosize = sizeof (info) / sizeof (DWORD);
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__try
|
|
|
|
{
|
2018-03-31 22:19:12 +02:00
|
|
|
RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize,
|
|
|
|
(const ULONG_PTR *) &info);
|
2015-04-07 22:05:45 +02:00
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Without a debugger we *must* have an exception handler,
|
|
|
|
* otherwise raising an exception will crash the process.
|
|
|
|
*/
|
|
|
|
if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL))
|
|
|
|
return;
|
|
|
|
|
2021-05-09 23:23:31 +02:00
|
|
|
RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (const ULONG_PTR *) &info);
|
2015-04-07 22:05:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:21:51 +01:00
|
|
|
typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
|
|
|
|
PCWSTR lpThreadDescription);
|
|
|
|
static pSetThreadDescription SetThreadDescriptionFunc = NULL;
|
2022-01-19 14:19:05 +01:00
|
|
|
static HMODULE kernel32_module = NULL;
|
2021-01-14 17:21:51 +01:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_thread_win32_load_library (void)
|
|
|
|
{
|
|
|
|
/* FIXME: Add support for UWP app */
|
|
|
|
#if !defined(G_WINAPI_ONLY_APP)
|
2021-10-21 13:01:33 +02:00
|
|
|
static gsize _init_once = 0;
|
2021-01-14 17:21:51 +01:00
|
|
|
if (g_once_init_enter (&_init_once))
|
|
|
|
{
|
|
|
|
kernel32_module = LoadLibraryW (L"kernel32.dll");
|
|
|
|
if (kernel32_module)
|
|
|
|
{
|
|
|
|
SetThreadDescriptionFunc =
|
|
|
|
(pSetThreadDescription) GetProcAddress (kernel32_module,
|
|
|
|
"SetThreadDescription");
|
|
|
|
if (!SetThreadDescriptionFunc)
|
|
|
|
FreeLibrary (kernel32_module);
|
|
|
|
}
|
|
|
|
g_once_init_leave (&_init_once, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return !!SetThreadDescriptionFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_thread_win32_set_thread_desc (const gchar *name)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
wchar_t *namew;
|
|
|
|
|
|
|
|
if (!g_thread_win32_load_library () || !name)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
namew = g_utf8_to_utf16 (name, -1, NULL, NULL, NULL);
|
|
|
|
if (!namew)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hr = SetThreadDescriptionFunc (GetCurrentThread (), namew);
|
|
|
|
|
|
|
|
g_free (namew);
|
|
|
|
return SUCCEEDED (hr);
|
|
|
|
}
|
|
|
|
|
2011-10-02 15:27:08 +02:00
|
|
|
void
|
|
|
|
g_system_thread_set_name (const gchar *name)
|
|
|
|
{
|
2021-01-14 17:21:51 +01:00
|
|
|
/* Prefer SetThreadDescription over exception based way if available,
|
|
|
|
* since thread description set by SetThreadDescription will be preserved
|
|
|
|
* in dump file */
|
|
|
|
if (!g_thread_win32_set_thread_desc (name))
|
|
|
|
SetThreadName ((DWORD) -1, name);
|
2011-10-02 15:27:08 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:05:23 +02:00
|
|
|
/* {{{1 Epilogue */
|
|
|
|
|
2012-11-28 18:00:40 +01:00
|
|
|
void
|
2011-10-04 21:44:48 +02:00
|
|
|
g_thread_win32_init (void)
|
2011-09-17 00:05:23 +02:00
|
|
|
{
|
2011-09-30 20:22:04 +02:00
|
|
|
InitializeCriticalSection (&g_private_lock);
|
2015-04-07 22:05:45 +02:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
SetThreadName_VEH_handle = AddVectoredExceptionHandler (1, &SetThreadName_VEH);
|
|
|
|
if (SetThreadName_VEH_handle == NULL)
|
|
|
|
{
|
|
|
|
/* This is bad, but what can we do? */
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-17 00:05:23 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 18:00:40 +01:00
|
|
|
void
|
2011-10-04 22:08:27 +02:00
|
|
|
g_thread_win32_thread_detach (void)
|
|
|
|
{
|
|
|
|
gboolean dtors_called;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-11-16 17:44:29 +01:00
|
|
|
for (dtor = g_atomic_pointer_get (&g_private_destructors); dtor; dtor = dtor->next)
|
2011-10-04 22:08:27 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (dtors_called);
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:05:45 +02:00
|
|
|
void
|
|
|
|
g_thread_win32_process_detach (void)
|
|
|
|
{
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
if (SetThreadName_VEH_handle != NULL)
|
|
|
|
{
|
|
|
|
RemoveVectoredExceptionHandler (SetThreadName_VEH_handle);
|
|
|
|
SetThreadName_VEH_handle = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-04 22:08:27 +02:00
|
|
|
/* vim:set foldmethod=marker: */
|