/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
- * gmutex.c: MT safety related functions
+ * gthread.c: MT safety related functions
* Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
* Owen Taylor
*
*/
#include "config.h"
-#include "glib.h"
-
-#ifdef G_THREAD_USE_PID_SURROGATE
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <errno.h>
-#endif /* G_THREAD_USE_PID_SURROGATE */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <string.h>
+#include "glib.h"
+#include "gthreadinit.h"
+
#if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
-# define g_system_thread_equal(thread1, thread2) \
- (thread1.dummy_pointer == thread2.dummy_pointer)
+# define g_system_thread_equal_simple(thread1, thread2) \
+ ((thread1).dummy_pointer == (thread2).dummy_pointer)
# define g_system_thread_assign(dest, src) \
- (dest.dummy_pointer = src.dummy_pointer)
+ ((dest).dummy_pointer = (src).dummy_pointer)
#else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
-# define g_system_thread_equal(thread1, thread2) \
- (memcmp (&thread1, &thread2, GLIB_SIZEOF_SYSTEM_THREAD) == 0)
+# define g_system_thread_equal_simple(thread1, thread2) \
+ (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
# define g_system_thread_assign(dest, src) \
- (memcpy (&dest, &src, GLIB_SIZEOF_SYSTEM_THREAD))
+ (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
#endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
+#define g_system_thread_equal(thread1, thread2) \
+ (g_thread_functions_for_glib_use.thread_equal ? \
+ g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
+ g_system_thread_equal_simple((thread1), (thread2)))
+
GQuark
g_thread_error_quark (void)
{
struct _GRealThread
{
GThread thread;
- GMainContext *context;
gpointer private_data;
gpointer retval;
GSystemThread system_thread;
-#ifdef G_THREAD_USE_PID_SURROGATE
- pid_t pid;
-#endif /* G_THREAD_USE_PID_SURROGATE */
};
-#ifdef G_THREAD_USE_PID_SURROGATE
-static gint priority_map[] = { 15, 0, -15, -20 };
-static gboolean prio_warned = FALSE;
-# define SET_PRIO(pid, prio) G_STMT_START{ \
- gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
- if (error == -1 && errno == EACCES && !prio_warned) \
- { \
- prio_warned = TRUE; \
- g_warning ("Priorities can only be increased by root."); \
- } \
- }G_STMT_END
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
typedef struct _GStaticPrivateNode GStaticPrivateNode;
struct _GStaticPrivateNode
{
gboolean g_thread_use_default_impl = TRUE;
gboolean g_threads_got_initialized = FALSE;
-#if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
-__declspec(dllexport)
-#endif
GThreadFunctions g_thread_functions_for_glib_use = {
(GMutex*(*)())g_thread_fail, /* mutex_new */
NULL, /* mutex_lock */
/* Local data */
-static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
+static GMutex *g_once_mutex = NULL;
+static GCond *g_once_cond = NULL;
static GPrivate *g_thread_specific_private = NULL;
static GSList *g_thread_all_threads = NULL;
static GSList *g_thread_free_indeces = NULL;
G_LOCK_DEFINE_STATIC (g_thread);
+#ifdef G_THREADS_ENABLED
/* This must be called only once, before any threads are created.
* It will only be called from g_thread_init() in -lgthread.
*/
-void
-g_mutex_init (void)
+void
+g_thread_init_glib (void)
{
- GRealThread* main_thread;
-
/* We let the main thread (the one that calls g_thread_init) inherit
- * the data, that it set before calling g_thread_init
+ * the static_private data set before calling g_thread_init
*/
- main_thread = (GRealThread*) g_thread_self ();
+ GRealThread* main_thread = (GRealThread*) g_thread_self ();
+
+ g_once_mutex = g_mutex_new ();
+ g_once_cond = g_cond_new ();
+
+ _g_convert_thread_init ();
+ _g_rand_thread_init ();
+ _g_main_thread_init ();
+ _g_mem_thread_init ();
+ _g_messages_thread_init ();
+ _g_atomic_thread_init ();
+
+ g_threads_got_initialized = TRUE;
g_thread_specific_private = g_private_new (g_thread_cleanup);
- G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
+ g_private_set (g_thread_specific_private, main_thread);
G_THREAD_UF (thread_self, (&main_thread->system_thread));
- g_mutex_protect_static_mutex_allocation = g_mutex_new ();
+ _g_mem_thread_private_init ();
+ _g_messages_thread_private_init ();
+
+}
+#endif /* G_THREADS_ENABLED */
+
+gpointer
+g_once_impl (GOnce *once,
+ GThreadFunc func,
+ gpointer arg)
+{
+ g_mutex_lock (g_once_mutex);
+
+ while (once->status == G_ONCE_STATUS_PROGRESS)
+ g_cond_wait (g_once_cond, g_once_mutex);
+
+ if (once->status != G_ONCE_STATUS_READY)
+ {
+ once->status = G_ONCE_STATUS_PROGRESS;
+ g_mutex_unlock (g_once_mutex);
+
+ once->retval = func (arg);
+
+ g_mutex_lock (g_once_mutex);
+ once->status = G_ONCE_STATUS_READY;
+ g_cond_broadcast (g_once_cond);
+ }
+
+ g_mutex_unlock (g_once_mutex);
+
+ return once->retval;
}
void
g_return_if_fail (mutex);
- memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
+ *mutex = init_mutex;
}
GMutex *
if (!g_thread_supported ())
return NULL;
- g_assert (g_mutex_protect_static_mutex_allocation);
+ g_assert (g_once_mutex);
- g_mutex_lock (g_mutex_protect_static_mutex_allocation);
+ g_mutex_lock (g_once_mutex);
if (!(*mutex))
- *mutex = g_mutex_new ();
+ {
+ GMutex *new_mutex = g_mutex_new ();
+
+ /* The following is a memory barrier to avoid the write
+ * to *new_mutex being reordered to after writing *mutex */
+ g_mutex_lock (new_mutex);
+ g_mutex_unlock (new_mutex);
+
+ *mutex = new_mutex;
+ }
- g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
+ g_mutex_unlock (g_once_mutex);
return *mutex;
}
g_return_if_fail (mutex);
- memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
+ *mutex = init_mutex;
}
void
guint
g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
{
- gint depth;
+ guint depth;
g_return_val_if_fail (mutex, 0);
G_UNLOCK (g_thread);
}
-void g_main_context_destroy (GMainContext *context);
-
static void
g_thread_cleanup (gpointer data)
{
}
g_array_free (array, TRUE);
}
- if (thread->context)
- g_main_context_destroy (thread->context);
/* We only free the thread structure, if it isn't joinable. If
it is, the structure is freed in g_thread_join */
g_assert (data);
-#ifdef G_THREAD_USE_PID_SURROGATE
- thread->pid = getpid ();
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
/* This has to happen before G_LOCK, as that might call g_thread_self */
g_private_set (g_thread_specific_private, data);
G_LOCK (g_thread);
G_UNLOCK (g_thread);
-#ifdef G_THREAD_USE_PID_SURROGATE
- if (g_thread_use_default_impl)
- SET_PRIO (thread->pid, thread->thread.priority);
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
- thread->retval = thread->thread.func (thread->thread.arg);
+ thread->retval = thread->thread.func (thread->thread.data);
return NULL;
}
GThread*
-g_thread_create (GThreadFunc thread_func,
- gpointer arg,
- gulong stack_size,
- gboolean joinable,
- gboolean bound,
- GThreadPriority priority,
- GError **error)
-{
- GRealThread* result = g_new (GRealThread, 1);
+g_thread_create_full (GThreadFunc func,
+ gpointer data,
+ gulong stack_size,
+ gboolean joinable,
+ gboolean bound,
+ GThreadPriority priority,
+ GError **error)
+{
+ GRealThread* result;
GError *local_error = NULL;
- g_return_val_if_fail (thread_func, NULL);
+ g_return_val_if_fail (func, NULL);
g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
+ result = g_new (GRealThread, 1);
+
result->thread.joinable = joinable;
- result->thread.bound = bound;
result->thread.priority = priority;
- result->thread.func = thread_func;
- result->thread.arg = arg;
+ result->thread.func = func;
+ result->thread.data = data;
result->private_data = NULL;
- result->context = NULL;
G_LOCK (g_thread);
G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
stack_size, joinable, bound, priority,
thread->priority = priority;
-#ifdef G_THREAD_USE_PID_SURROGATE
- if (g_thread_use_default_impl)
- SET_PRIO (real->pid, priority);
- else
-#endif /* G_THREAD_USE_PID_SURROGATE */
- G_THREAD_CF (thread_set_priority, (void)0,
- (&real->system_thread, priority));
+ G_THREAD_CF (thread_set_priority, (void)0,
+ (&real->system_thread, priority));
}
GThread*
created by GLib. */
thread = g_new (GRealThread, 1);
thread->thread.joinable = FALSE; /* This is a save guess */
- thread->thread.bound = TRUE; /* This isn't important at all */
thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
just a guess */
thread->thread.func = NULL;
- thread->thread.arg = NULL;
+ thread->thread.data = NULL;
thread->private_data = NULL;
- thread->context = NULL;
if (g_thread_supported ())
G_THREAD_UF (thread_self, (&thread->system_thread));
-#ifdef G_THREAD_USE_PID_SURROGATE
- thread->pid = getpid ();
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
g_private_set (g_thread_specific_private, thread);
G_LOCK (g_thread);
g_return_if_fail (lock);
- memcpy (lock, &init_lock, sizeof (GStaticRWLock));
+ *lock = init_lock;
}
static void inline
{
if (lock->want_to_write && lock->write_cond)
g_cond_signal (lock->write_cond);
- else if (lock->read_cond)
+ else if (lock->want_to_read && lock->read_cond)
g_cond_broadcast (lock->read_cond);
}
return;
g_static_mutex_lock (&lock->mutex);
- while (lock->write || lock->want_to_write)
+ lock->want_to_read++;
+ while (lock->have_writer || lock->want_to_write)
g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
+ lock->want_to_read--;
lock->read_counter++;
g_static_mutex_unlock (&lock->mutex);
}
return TRUE;
g_static_mutex_lock (&lock->mutex);
- if (!lock->write && !lock->want_to_write)
+ if (!lock->have_writer && !lock->want_to_write)
{
lock->read_counter++;
ret_val = TRUE;
g_static_mutex_lock (&lock->mutex);
lock->want_to_write++;
- while (lock->write || lock->read_counter)
+ while (lock->have_writer || lock->read_counter)
g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
lock->want_to_write--;
- lock->write = TRUE;
+ lock->have_writer = TRUE;
g_static_mutex_unlock (&lock->mutex);
}
return TRUE;
g_static_mutex_lock (&lock->mutex);
- if (!lock->write && !lock->read_counter)
+ if (!lock->have_writer && !lock->read_counter)
{
- lock->write = TRUE;
+ lock->have_writer = TRUE;
ret_val = TRUE;
}
g_static_mutex_unlock (&lock->mutex);
return;
g_static_mutex_lock (&lock->mutex);
- lock->write = FALSE;
+ lock->have_writer = FALSE;
g_static_rw_lock_signal (lock);
g_static_mutex_unlock (&lock->mutex);
}