#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>
#endif
+#include <string.h>
+
#if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
# define g_system_thread_equal(thread1, thread2) \
(thread1.dummy_pointer == thread2.dummy_pointer)
return quark;
}
+/* Keep this in sync with GRealThread in gmain.c! */
typedef struct _GRealThread GRealThread;
struct _GRealThread
{
GThreadFunc func;
gpointer arg;
gpointer private_data;
+ GMainContext *context;
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_OS_WIN32) && defined(__GNUC__)
+#if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
__declspec(dllexport)
#endif
GThreadFunctions g_thread_functions_for_glib_use = {
g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
guint depth)
{
+ GSystemThread self;
g_return_if_fail (mutex);
if (!g_thread_supported ())
return;
+ G_THREAD_UF (thread_self, (&self));
+
+ if (g_system_thread_equal (self, mutex->owner))
+ {
+ mutex->depth += depth;
+ return;
+ }
g_static_mutex_lock (&mutex->mutex);
- G_THREAD_UF (thread_self, (&mutex->owner));
+ g_system_thread_assign (mutex->owner, self);
mutex->depth = depth;
}
gpointer
g_static_private_get (GStaticPrivate *private_key)
{
- return g_static_private_get_for_thread (private_key, g_thread_self ());
-}
-
-gpointer
-g_static_private_get_for_thread (GStaticPrivate *private_key,
- GThread *thread)
-{
+ GRealThread *self = (GRealThread*) g_thread_self ();
GArray *array;
- GRealThread *self = (GRealThread*) thread;
-
- g_return_val_if_fail (thread, NULL);
array = self->private_data;
if (!array)
if (!private_key->index)
return NULL;
else if (private_key->index <= array->len)
- return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
+ return g_array_index (array, GStaticPrivateNode,
+ private_key->index - 1).data;
else
return NULL;
}
gpointer data,
GDestroyNotify notify)
{
- g_static_private_set_for_thread (private_key, g_thread_self (),
- data, notify);
-}
-
-void
-g_static_private_set_for_thread (GStaticPrivate *private_key,
- GThread *thread,
- gpointer data,
- GDestroyNotify notify)
-{
+ GRealThread *self = (GRealThread*) g_thread_self ();
GArray *array;
- GRealThread *self =(GRealThread*) thread;
static guint next_index = 0;
GStaticPrivateNode *node;
- g_return_if_fail (thread);
-
array = self->private_data;
if (!array)
{
void
g_static_private_free (GStaticPrivate *private_key)
{
- GStaticPrivate copied_key;
+ guint index = private_key->index;
GSList *list;
- copied_key.index = private_key->index;
- private_key->index = 0;
-
- if (!copied_key.index)
+ if (!index)
return;
+
+ private_key->index = 0;
G_LOCK (g_thread);
list = g_thread_all_threads;
while (list)
{
- GThread *thread = list->data;
+ GRealThread *thread = list->data;
+ GArray *array = thread->private_data;
list = list->next;
-
- G_UNLOCK (g_thread);
- g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
- G_LOCK (g_thread);
+
+ if (array && index <= array->len)
+ {
+ GStaticPrivateNode *node = &g_array_index (array,
+ GStaticPrivateNode,
+ index - 1);
+ gpointer ddata = node->data;
+ GDestroyNotify ddestroy = node->destroy;
+
+ node->data = NULL;
+ node->destroy = NULL;
+
+ if (ddestroy)
+ {
+ G_UNLOCK (g_thread);
+ ddestroy (ddata);
+ G_LOCK (g_thread);
+ }
+ }
}
- g_thread_free_indeces =
- g_slist_prepend (g_thread_free_indeces,
- GUINT_TO_POINTER (copied_key.index));
+ g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
+ GUINT_TO_POINTER (index));
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 */
if (!thread->thread.joinable)
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);
before thread->func is called. See g_thread_create. */
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->func (thread->arg);
}
result->func = thread_func;
result->arg = arg;
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,
g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
thread->priority = priority;
- G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, 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));
}
GThread*
thread->func = NULL;
thread->arg = 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);