* 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.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
#include "config.h"
+#include "glib.h"
+#include "glib-init.h"
#include "gthread.h"
#include "gthreadprivate.h"
#include "gslice.h"
* 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;
+ * zero. That means that they are completely ABI compatible with our
+ * GMutex and GCond APIs.
*
* Unfortunately, Windows XP lacks these facilities and GLib still
* needs to support Windows XP. Our approach here is as follows:
static CRITICAL_SECTION *
g_rec_mutex_get_impl (GRecMutex *mutex)
{
- CRITICAL_SECTION *impl = mutex->impl;
+ CRITICAL_SECTION *impl = mutex->p;
- if G_UNLIKELY (mutex->impl == NULL)
+ if G_UNLIKELY (mutex->p == NULL)
{
impl = g_rec_mutex_impl_new ();
- if (InterlockedCompareExchangePointer (&mutex->impl, impl, NULL) != NULL)
+ if (InterlockedCompareExchangePointer (&mutex->p, impl, NULL) != NULL)
g_rec_mutex_impl_free (impl);
- impl = mutex->impl;
+ impl = mutex->p;
}
return impl;
void
g_rec_mutex_init (GRecMutex *mutex)
{
- mutex->impl = g_rec_mutex_impl_new ();
+ mutex->p = g_rec_mutex_impl_new ();
}
void
g_rec_mutex_clear (GRecMutex *mutex)
{
- if (mutex->impl)
- g_rec_mutex_impl_free (mutex->impl);
+ g_rec_mutex_impl_free (mutex->p);
}
void
void
g_rec_mutex_unlock (GRecMutex *mutex)
{
- LeaveCriticalSection (mutex->impl);
+ LeaveCriticalSection (mutex->p);
}
gboolean
}
gboolean
-g_cond_timedwait (GCond *cond,
- GMutex *entered_mutex,
- gint64 abs_time)
+g_cond_wait_until (GCond *cond,
+ GMutex *entered_mutex,
+ gint64 end_time)
{
gint64 span;
- FILETIME ft;
- gint64 now;
-
- GetSystemTimeAsFileTime (&ft);
- memmove (&now, &ft, sizeof (FILETIME));
- now -= G_GINT64_CONSTANT (116444736000000000);
- now /= 10;
-
- span = abs_time - now;
+ span = end_time - g_get_monotonic_time ();
if G_UNLIKELY (span < 0)
span = 0;
return g_thread_impl_vtable.SleepConditionVariableSRW (cond, entered_mutex, span / 1000, 0);
}
-gboolean
-g_cond_timed_wait (GCond *cond,
- GMutex *entered_mutex,
- GTimeVal *abs_time)
-{
- if (abs_time)
- {
- gint64 micros;
-
- micros = abs_time->tv_sec;
- micros *= 1000000;
- micros += abs_time->tv_usec;
-
- return g_cond_timedwait (cond, entered_mutex, micros);
- }
- else
- {
- g_cond_wait (cond, entered_mutex);
- return TRUE;
- }
-}
-
/* {{{1 GPrivate */
typedef struct _GPrivateDestructor GPrivateDestructor;
};
static GPrivateDestructor * volatile g_private_destructors;
+static CRITICAL_SECTION g_private_lock;
-void
-g_private_init (GPrivate *key,
- GDestroyNotify notify)
+static DWORD
+g_private_get_impl (GPrivate *key)
{
- GPrivateDestructor *destructor;
+ DWORD impl = (DWORD) key->p;
- key->index = TlsAlloc ();
+ if G_UNLIKELY (impl == 0)
+ {
+ EnterCriticalSection (&g_private_lock);
+ impl = (DWORD) key->p;
+ if (impl == 0)
+ {
+ GPrivateDestructor *destructor;
- destructor = malloc (sizeof (GPrivateDestructor));
- if G_UNLIKELY (destructor == NULL)
- g_thread_abort (errno, "malloc");
- destructor->index = key->index;
- destructor->notify = notify;
+ impl = TlsAlloc ();
- do
- destructor->next = g_private_destructors;
- while (InterlockedCompareExchangePointer (&g_private_destructors, destructor->next, destructor) != destructor->next);
+ if (impl == TLS_OUT_OF_INDEXES)
+ g_thread_abort (0, "TlsAlloc");
- key->ready = TRUE;
+ 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);
+ }
+
+ return impl;
}
gpointer
g_private_get (GPrivate *key)
{
- if (!key->ready)
- return key->single_value;
-
- return TlsGetValue (key->index);
+ return TlsGetValue (g_private_get_impl (key));
}
void
g_private_set (GPrivate *key,
gpointer value)
{
- if (!key->ready)
- {
- key->single_value = value;
- return;
- }
+ TlsSetValue (g_private_get_impl (key), value);
+}
- TlsSetValue (key->index, value);
+void
+g_private_replace (GPrivate *key,
+ gpointer value)
+{
+ DWORD impl = g_private_get_impl (key);
+ gpointer old;
+
+ old = TlsGetValue (impl);
+ if (old && key->notify)
+ key->notify (old);
+ TlsSetValue (impl, value);
}
/* {{{1 GThread */
-#include "glib.h"
-#include "gthreadprivate.h"
-
#define win32_check_for_error(what) G_STMT_START{ \
if (!(what)) \
g_error ("file %s: line %d (%s): error %s during %s", \
#define G_MUTEX_SIZE (sizeof (gpointer))
-static DWORD g_thread_self_tls;
-static DWORD g_private_tls;
-
typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
-typedef struct _GThreadData GThreadData;
-struct _GThreadData
+typedef struct
{
- GThreadFunc func;
- gpointer data;
- HANDLE thread;
- gboolean joinable;
-};
+ GRealThread thread;
+
+ GThreadFunc proxy;
+ HANDLE handle;
+} GThreadWin32;
void
-g_system_thread_self (gpointer thread)
+g_system_thread_free (GRealThread *thread)
{
- GThreadData *self = TlsGetValue (g_thread_self_tls);
+ GThreadWin32 *wt = (GThreadWin32 *) thread;
- if (!self)
- {
- /* This should only happen for the main thread! */
- HANDLE handle = GetCurrentThread ();
- HANDLE process = GetCurrentProcess ();
- self = g_new (GThreadData, 1);
- win32_check_for_error (DuplicateHandle (process, handle, process,
- &self->thread, 0, FALSE,
- DUPLICATE_SAME_ACCESS));
- win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
- self->func = NULL;
- self->data = NULL;
- self->joinable = FALSE;
- }
-
- *(GThreadData **)thread = self;
+ win32_check_for_error (CloseHandle (wt->handle));
+ g_slice_free (GThreadWin32, wt);
}
void
g_system_thread_exit (void)
{
- GThreadData *self = TlsGetValue (g_thread_self_tls);
- 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;
-
- 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;
- }
- }
- }
- while (dtors_called);
-
- if (self)
- {
- if (!self->joinable)
- {
- win32_check_for_error (CloseHandle (self->thread));
- g_free (self);
- }
- win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
- }
-
- if (g_thread_impl_vtable.CallThisOnThreadExit)
- g_thread_impl_vtable.CallThisOnThreadExit ();
-
_endthreadex (0);
}
static guint __stdcall
-g_thread_proxy (gpointer data)
+g_thread_win32_proxy (gpointer data)
{
- GThreadData *self = (GThreadData*) data;
-
- win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
+ GThreadWin32 *self = data;
- self->func (self->data);
+ self->proxy (self);
g_system_thread_exit ();
return 0;
}
-void
-g_system_thread_create (GThreadFunc func,
- gpointer data,
- gulong stack_size,
- gboolean joinable,
- gpointer thread,
- GError **error)
+GRealThread *
+g_system_thread_new (GThreadFunc func,
+ gulong stack_size,
+ GError **error)
{
+ GThreadWin32 *thread;
guint ignore;
- GThreadData *retval;
-
- g_return_if_fail (func);
-
- retval = g_new(GThreadData, 1);
- retval->func = func;
- retval->data = data;
- retval->joinable = joinable;
+ thread = g_slice_new0 (GThreadWin32);
+ thread->proxy = func;
- retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy,
- retval, 0, &ignore);
+ thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread, 0, &ignore);
- if (retval->thread == NULL)
+ if (thread->handle == NULL)
{
gchar *win_error = g_win32_error_message (GetLastError ());
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
"Error creating thread: %s", win_error);
- g_free (retval);
g_free (win_error);
- return;
+ g_slice_free (GThreadWin32, thread);
+ return NULL;
}
- *(GThreadData **)thread = retval;
+ return (GRealThread *) thread;
}
void
}
void
-g_system_thread_join (gpointer thread)
+g_system_thread_wait (GRealThread *thread)
{
- GThreadData *target = *(GThreadData **)thread;
+ GThreadWin32 *wt = (GThreadWin32 *) thread;
- g_return_if_fail (target->joinable);
-
- win32_check_for_error (WAIT_FAILED !=
- WaitForSingleObject (target->thread, INFINITE));
-
- win32_check_for_error (CloseHandle (target->thread));
- g_free (target);
+ win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
}
-gboolean
-g_system_thread_equal (gpointer thread1,
- gpointer thread2)
+void
+g_system_thread_set_name (const gchar *name)
{
- return ((GSystemThread*)thread1)->dummy_pointer == ((GSystemThread*)thread2)->dummy_pointer;
+ /* FIXME: implement */
}
/* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
typedef struct _GThreadXpWaiter GThreadXpWaiter;
struct _GThreadXpWaiter
{
- HANDLE event;
- volatile GThreadXpWaiter *next;
+ HANDLE event;
+ volatile GThreadXpWaiter *next;
+ volatile GThreadXpWaiter **my_owner;
};
static GThreadXpWaiter *
waiter->event = CreateEvent (0, FALSE, FALSE, NULL);
if (waiter->event == NULL)
g_thread_abort (GetLastError (), "CreateEvent");
+ waiter->my_owner = NULL;
TlsSetValue (g_thread_xp_waiter_tls, waiter);
}
{
CRITICAL_SECTION writer_lock;
gboolean ever_shared; /* protected by writer_lock */
+ gboolean writer_locked; /* protected by writer_lock */
/* below is only ever touched if ever_shared becomes true */
CRITICAL_SECTION atomicity;
{
EnterCriticalSection (&g_thread_xp_lock);
- result = malloc (sizeof (GThreadSRWLock));
-
+ /* Check again */
+ result = *lock;
if (result == NULL)
- g_thread_abort (errno, "malloc");
+ {
+ result = malloc (sizeof (GThreadSRWLock));
- InitializeCriticalSection (&result->writer_lock);
- result->ever_shared = FALSE;
- *lock = result;
+ if (result == NULL)
+ g_thread_abort (errno, "malloc");
+
+ InitializeCriticalSection (&result->writer_lock);
+ result->writer_locked = FALSE;
+ result->ever_shared = FALSE;
+ *lock = result;
+ }
LeaveCriticalSection (&g_thread_xp_lock);
}
EnterCriticalSection (&lock->writer_lock);
+ /* 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;
+
if (lock->ever_shared)
{
GThreadXpWaiter *waiter = NULL;
if (!TryEnterCriticalSection (&lock->writer_lock))
return FALSE;
+ /* 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;
+
if (lock->ever_shared)
{
gboolean available;
{
GThreadSRWLock *lock = *(GThreadSRWLock * volatile *) mutex;
+ lock->writer_locked = FALSE;
+
/* We need this until we fix some weird parts of GLib that try to
* unlock freshly-allocated mutexes.
*/
EnterCriticalSection (&lock->writer_lock);
+ /* See g_thread_xp_AcquireSRWLockExclusive */
+ g_assert (!lock->writer_locked);
+
g_thread_xp_srwlock_become_reader (lock);
LeaveCriticalSection (&lock->writer_lock);
if (!TryEnterCriticalSection (&lock->writer_lock))
return FALSE;
+ /* See g_thread_xp_AcquireSRWLockExclusive */
+ if G_UNLIKELY (lock->writer_locked)
+ {
+ LeaveCriticalSection (&lock->writer_lock);
+ return FALSE;
+ }
+
g_thread_xp_srwlock_become_reader (lock);
LeaveCriticalSection (&lock->writer_lock);
waiter->next = NULL;
EnterCriticalSection (&g_thread_xp_lock);
+ waiter->my_owner = cv->last_ptr;
*cv->last_ptr = waiter;
cv->last_ptr = &waiter->next;
LeaveCriticalSection (&g_thread_xp_lock);
if (status != WAIT_TIMEOUT && status != WAIT_OBJECT_0)
g_thread_abort (GetLastError (), "WaitForSingleObject");
-
g_mutex_lock (mutex);
+ if (status == WAIT_TIMEOUT)
+ {
+ EnterCriticalSection (&g_thread_xp_lock);
+ if (waiter->my_owner)
+ {
+ if (waiter->next)
+ waiter->next->my_owner = waiter->my_owner;
+ else
+ cv->last_ptr = waiter->my_owner;
+ *waiter->my_owner = waiter->next;
+ waiter->my_owner = NULL;
+ }
+ LeaveCriticalSection (&g_thread_xp_lock);
+ }
+
return status == WAIT_OBJECT_0;
}
volatile GThreadXpWaiter *waiter;
EnterCriticalSection (&g_thread_xp_lock);
+
waiter = cv->first;
if (waiter != NULL)
{
+ waiter->my_owner = NULL;
cv->first = waiter->next;
- if (cv->first == NULL)
+ if (cv->first != NULL)
+ cv->first->my_owner = &cv->first;
+ else
cv->last_ptr = &cv->first;
}
- LeaveCriticalSection (&g_thread_xp_lock);
if (waiter != NULL)
SetEvent (waiter->event);
+
+ LeaveCriticalSection (&g_thread_xp_lock);
}
static void __stdcall
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)
{
next = waiter->next;
SetEvent (waiter->event);
+ waiter->my_owner = NULL;
waiter = next;
}
+
+ LeaveCriticalSection (&g_thread_xp_lock);
}
/* {{{2 XP Setup */
return TRUE;
}
-G_GNUC_INTERNAL void
-g_thread_DllMain (void)
+void
+g_thread_win32_init (void)
+{
+ if (!g_thread_lookup_native_funcs ())
+ g_thread_xp_init ();
+
+ InitializeCriticalSection (&g_private_lock);
+}
+
+void
+g_thread_win32_thread_detach (void)
{
- if (g_thread_lookup_native_funcs ())
- fprintf (stderr, "(debug) GThread using native mode\n");
- else
+ gboolean dtors_called;
+
+ do
{
- fprintf (stderr, "(debug) GThread using Windows XP mode\n");
- g_thread_xp_init ();
+ 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;
+ }
+ }
}
+ while (dtors_called);
- win32_check_for_error (TLS_OUT_OF_INDEXES != (g_thread_self_tls = TlsAlloc ()));
- win32_check_for_error (TLS_OUT_OF_INDEXES != (g_private_tls = TlsAlloc ()));
+ if (g_thread_impl_vtable.CallThisOnThreadExit)
+ g_thread_impl_vtable.CallThisOnThreadExit ();
}
/* vim:set foldmethod=marker: */
-