Commit 47444dac authored by Allison Karlitskaya's avatar Allison Karlitskaya

Deprecate g_thread_init()

Move the last few things that needed thread-safe initialisation to a
global ctor.

https://bugzilla.gnome.org/show_bug.cgi?id=660744
parent 310c3ed4
......@@ -150,6 +150,8 @@ libglib_2_0_la_SOURCES = \
glibintl.h \
glib_trace.h \
glib-ctor.h \
glib-init.h \
glib-init.c \
glib-private.h \
glib-private.c \
glist.c \
......
......@@ -109,6 +109,55 @@ gettime (void)
guint64 (*g_thread_gettime) (void) = gettime;
/* Initialisation {{{1 ---------------------------------------------------- */
gboolean g_threads_got_initialized = TRUE;
GSystemThread zero_thread; /* This is initialized to all zero */
/**
* g_thread_init:
* @vtable: a function table of type #GThreadFunctions, that provides
* the entry points to the thread system to be used. Since 2.32,
* this parameter is ignored and should always be %NULL
*
* If you use GLib from more than one thread, you must initialize the
* thread system by calling g_thread_init().
*
* Since version 2.24, calling g_thread_init() multiple times is allowed,
* but nothing happens except for the first call.
*
* Since version 2.32, GLib does not support custom thread implementations
* anymore and the @vtable parameter is ignored and you should pass %NULL.
*
* <note><para>g_thread_init() must not be called directly or indirectly
* in a callback from GLib. Also no mutexes may be currently locked while
* calling g_thread_init().</para></note>
*
* <note><para>To use g_thread_init() in your program, you have to link
* with the libraries that the command <command>pkg-config --libs
* gthread-2.0</command> outputs. This is not the case for all the
* other thread-related functions of GLib. Those can be used without
* having to link with the thread libraries.</para></note>
*/
/**
* g_thread_get_initialized:
*
* Indicates if g_thread_init() has been called.
*
* Returns: %TRUE if threads have been initialized.
*
* Since: 2.20
*/
gboolean
g_thread_get_initialized (void)
{
return g_thread_supported ();
}
/* We need this for ABI compatibility */
void g_thread_init_glib (void) { }
/* Internal variables {{{1 */
static GRealThread *g_thread_all_threads = NULL;
......
......@@ -200,6 +200,21 @@ void g_static_private_free (GStaticPrivate *private_key);
gboolean g_once_init_enter_impl (volatile gsize *location);
void g_thread_init (gpointer vtable);
gboolean g_thread_get_initialized (void);
GLIB_VAR gboolean g_threads_got_initialized;
#if defined(G_THREADS_MANDATORY)
#define g_thread_supported() 1
#else
#define g_thread_supported() (g_threads_got_initialized)
#endif
GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
G_END_DECLS
#endif /* __G_DEPRECATED_THREAD_H__ */
/*
* Copyright © 2011 Canonical Limited
*
* 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 version 2 of the
* licence, or (at your option) any later version.
*
* 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include "glib-init.h"
#include "gutils.h" /* for GDebugKey */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/**
* g_mem_gc_friendly:
*
* This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
* includes the key <link linkend="G_DEBUG">gc-friendly</link>.
*/
#ifdef ENABLE_GC_FRIENDLY_DEFAULT
gboolean g_mem_gc_friendly = TRUE;
#else
gboolean g_mem_gc_friendly = FALSE;
#endif
GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
static gboolean
debug_key_matches (const gchar *key,
const gchar *token,
guint length)
{
/* may not call GLib functions: see note in g_parse_debug_string() */
for (; length; length--, key++, token++)
{
char k = (*key == '_') ? '-' : tolower (*key );
char t = (*token == '_') ? '-' : tolower (*token);
if (k != t)
return FALSE;
}
return *key == '\0';
}
/**
* g_parse_debug_string:
* @string: (allow-none): a list of debug options separated by colons, spaces, or
* commas, or %NULL.
* @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
* strings with bit flags.
* @nkeys: the number of #GDebugKey<!-- -->s in the array.
*
* Parses a string containing debugging options
* into a %guint containing bit flags. This is used
* within GDK and GTK+ to parse the debug options passed on the
* command line or through environment variables.
*
* If @string is equal to "all", all flags are set. If @string
* is equal to "help", all the available keys in @keys are printed
* out to standard error.
*
* Returns: the combined set of bit flags.
*/
guint
g_parse_debug_string (const gchar *string,
const GDebugKey *keys,
guint nkeys)
{
guint i;
guint result = 0;
if (string == NULL)
return 0;
/* this function is used during the initialisation of gmessages, gmem
* and gslice, so it may not do anything that causes memory to be
* allocated or risks messages being emitted.
*
* this means, more or less, that this code may not call anything
* inside GLib.
*/
if (!strcasecmp (string, "all"))
{
for (i = 0; i < nkeys; i++)
result |= keys[i].value;
}
else if (!strcasecmp (string, "help"))
{
/* using stdio directly for the reason stated above */
fprintf (stderr, "Supported debug values: ");
for (i = 0; i < nkeys; i++)
fprintf (stderr, " %s", keys[i].key);
fprintf (stderr, "\n");
}
else
{
const gchar *p = string;
const gchar *q;
while (*p)
{
q = strpbrk (p, ":;, \t");
if (!q)
q = p + strlen(p);
for (i = 0; i < nkeys; i++)
if (debug_key_matches (keys[i].key, p, q - p))
result |= keys[i].value;
p = q;
if (*p)
p++;
}
}
return result;
}
static guint
g_parse_debug_envvar (const gchar *envvar,
const GDebugKey *keys,
gint n_keys)
{
const gchar *value;
#ifdef OS_WIN32
/* "fatal-warnings,fatal-criticals,all,help" is pretty short */
gchar buffer[80];
if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
value = buffer;
else
return 0;
#else
value = getenv (envvar);
#endif
return g_parse_debug_string (value, keys, n_keys);
}
static void
g_messages_prefixed_init (void)
{
const GDebugKey keys[] = {
{ "error", G_LOG_LEVEL_ERROR },
{ "critical", G_LOG_LEVEL_CRITICAL },
{ "warning", G_LOG_LEVEL_WARNING },
{ "message", G_LOG_LEVEL_MESSAGE },
{ "info", G_LOG_LEVEL_INFO },
{ "debug", G_LOG_LEVEL_DEBUG }
};
g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys));
}
static void
g_debug_init (void)
{
const GDebugKey keys[] = {
{"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
{"fatal-criticals", G_LOG_LEVEL_CRITICAL }
};
GLogLevelFlags flags;
flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
g_log_always_fatal |= flags;
}
static void
g_mem_init (void)
{
const GDebugKey keys[] = {
{ "gc-friendly", 1 },
};
if (g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys)))
g_mem_gc_friendly = TRUE;
}
static void
glib_init (void)
{
g_messages_prefixed_init ();
g_debug_init ();
g_mem_init ();
}
#if defined (G_OS_WIN32)
HMODULE glib_dll;
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
glib_dll = hinstDLL;
g_thread_win32_init ();
glib_init ();
break;
default:
/* do nothing */
;
}
return TRUE;
}
#elif defined (__GNUC__)
__attribute__ ((constructor)) static void
glib_init_ctor (void)
{
glib_init ();
}
#else
# error Your platform/compiler is missing constructor support
#endif
/*
* Copyright © 2011 Canonical Limited
*
* 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 version 2 of the
* licence, or (at your option) any later version.
*
* 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#ifndef __GLIB_INIT_H__
#define __GLIB_INIT_H__
#include "gmessages.h"
G_GNUC_INTERNAL extern GLogLevelFlags g_log_always_fatal;
G_GNUC_INTERNAL extern GLogLevelFlags g_log_msg_prefix;
GLIB_VAR gboolean g_mem_gc_friendly;
#ifdef G_OS_WIN32
#include <windows.h>
G_GNUC_INTERNAL void g_thread_win32_init (void);
G_GNUC_INTERNAL extern HMODULE glib_dll;
#endif
#endif /* __GLIB_INIT_H__ */
......@@ -522,8 +522,6 @@ g_main_context_new (void)
static gsize initialised;
GMainContext *context;
g_thread_init_glib ();
if (g_once_init_enter (&initialised))
{
#ifdef G_MAIN_POLL_DEBUG
......@@ -4742,8 +4740,6 @@ g_get_worker_context (void)
{
static gsize initialised;
g_thread_init_glib ();
if (g_once_init_enter (&initialised))
{
GError *error = NULL;
......
......@@ -36,6 +36,8 @@
#include <string.h>
#include <signal.h>
#include "glib-init.h"
#include "gslice.h"
#include "gbacktrace.h"
#include "gtestutils.h"
......@@ -56,36 +58,6 @@
* g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
*/
#ifdef ENABLE_GC_FRIENDLY_DEFAULT
gboolean g_mem_gc_friendly = TRUE;
#else
/**
* g_mem_gc_friendly:
*
* This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
* includes the key <link linkend="G_DEBUG">gc-friendly</link>.
*/
gboolean g_mem_gc_friendly = FALSE;
#endif
GLIB_CTOR (g_mem_init_nomessage)
{
gchar buffer[1024];
const gchar *val;
const GDebugKey keys[] = {
{ "gc-friendly", 1 },
};
gint flags;
/* don't use g_malloc/g_message here */
val = _g_getenv_nomalloc ("G_DEBUG", buffer);
flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
if (flags & 1) /* gc-friendly */
{
g_mem_gc_friendly = TRUE;
}
}
/* --- malloc wrappers --- */
#ifndef REALLOC_0_WORKS
static gpointer
......@@ -182,8 +154,6 @@ static GMemVTable glib_mem_vtable = {
gpointer
g_malloc (gsize n_bytes)
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
{
gpointer mem;
......@@ -214,8 +184,6 @@ g_malloc (gsize n_bytes)
gpointer
g_malloc0 (gsize n_bytes)
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
{
gpointer mem;
......@@ -253,8 +221,6 @@ g_realloc (gpointer mem,
{
gpointer newmem;
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
{
newmem = glib_mem_vtable.realloc (mem, n_bytes);
......@@ -284,8 +250,6 @@ g_realloc (gpointer mem,
void
g_free (gpointer mem)
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (mem))
glib_mem_vtable.free (mem);
TRACE(GLIB_MEM_FREE((void*) mem));
......@@ -305,8 +269,6 @@ g_try_malloc (gsize n_bytes)
{
gpointer mem;
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
mem = glib_mem_vtable.try_malloc (n_bytes);
else
......@@ -332,8 +294,6 @@ g_try_malloc0 (gsize n_bytes)
{
gpointer mem;
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
mem = glib_mem_vtable.try_malloc (n_bytes);
else
......@@ -362,8 +322,6 @@ g_try_realloc (gpointer mem,
{
gpointer newmem;
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
if (G_LIKELY (n_bytes))
newmem = glib_mem_vtable.try_realloc (mem, n_bytes);
else
......@@ -398,8 +356,6 @@ g_malloc_n (gsize n_blocks,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
}
......@@ -424,8 +380,6 @@ g_malloc0_n (gsize n_blocks,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
}
......@@ -452,8 +406,6 @@ g_realloc_n (gpointer mem,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
}
......@@ -731,8 +683,6 @@ g_mem_profile (void)
gsize local_zinit;
gsize local_frees;
GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_mutex_lock (&gmem_profile_mutex);
local_allocs = profile_allocs;
......
......@@ -60,6 +60,7 @@
#include <errno.h>
#include "gmessages.h"
#include "glib-init.h"
#include "gbacktrace.h"
#include "gconvert.h"
......@@ -103,17 +104,13 @@ struct _GLogHandler
/* --- variables --- */
static GMutex g_messages_lock;
static GLogDomain *g_log_domains = NULL;
static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
static GPrintFunc glib_print_func = NULL;
static GPrintFunc glib_printerr_func = NULL;
static GPrivate g_log_depth;
static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
static GLogFunc default_log_func = g_log_default_handler;
static gpointer default_log_data = NULL;
static GTestLogFatalFunc fatal_log_func = NULL;
static gpointer fatal_log_data;
static gboolean g_debug_initialized = FALSE;
/* --- functions --- */
#ifdef G_OS_WIN32
......@@ -158,74 +155,6 @@ write_string (int fd,
write (fd, string, strlen (string));
}
static void
g_messages_prefixed_init (void)
{
static gboolean initialized = FALSE;
if (!initialized)
{
const gchar *val;
initialized = TRUE;
val = g_getenv ("G_MESSAGES_PREFIXED");
if (val)
{
const GDebugKey keys[] = {
{ "error", G_LOG_LEVEL_ERROR },
{ "critical", G_LOG_LEVEL_CRITICAL },
{ "warning", G_LOG_LEVEL_WARNING },
{ "message", G_LOG_LEVEL_MESSAGE },
{ "info", G_LOG_LEVEL_INFO },
{ "debug", G_LOG_LEVEL_DEBUG }
};
g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
}
}
}
static guint
g_parse_debug_envvar (const gchar *envvar,
const GDebugKey *keys,
gint n_keys)
{
const gchar *value;
#ifdef OS_WIN32
/* "fatal-warnings,fatal-criticals,all,help" is pretty short */
gchar buffer[80];
if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
value = buffer;
else
return 0;
#else
value = getenv (envvar);
#endif
return g_parse_debug_string (value, keys, n_keys);
}
static void
g_debug_init (void)
{
const GDebugKey keys[] = {
{"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
{"fatal-criticals", G_LOG_LEVEL_CRITICAL }
};
GLogLevelFlags flags;
flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
g_mutex_lock (&g_messages_lock);
g_log_always_fatal |= flags;
g_mutex_unlock (&g_messages_lock);
g_debug_initialized = TRUE;
}
static GLogDomain*
g_log_find_domain_L (const gchar *log_domain)
{
......@@ -498,9 +427,6 @@ g_logv (const gchar *log_domain,
gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
gint i;
if (!g_debug_initialized)
g_debug_init ();
log_level &= G_LOG_LEVEL_MASK;
if (!log_level)
return;
......@@ -973,8 +899,6 @@ g_log_default_handler (const gchar *log_domain,
return;
}
g_messages_prefixed_init ();
fd = mklevel_prefix (level_prefix, log_level);
gstring = g_string_new (NULL);
......@@ -1203,12 +1127,3 @@ g_printf_string_upper_bound (const gchar *format,
gchar c;
return _g_vsnprintf (&c, 1, format, args) + 1;
}
void
_g_messages_thread_init_nomessage (void)
{
g_messages_prefixed_init ();
g_debug_init ();
}
......@@ -133,8 +133,6 @@ get_random_version (void)
static gsize initialized = FALSE;
static guint random_version;
g_thread_init_glib ();
if (g_once_init_enter (&initialized))
{
const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
......
......@@ -356,9 +356,6 @@ GLIB_CTOR (g_slice_init_nomessage)
allocator->max_slab_chunk_size_for_magazine_cache = MAX_SLAB_CHUNK_SIZE (allocator);
if (allocator->config.always_malloc || allocator->config.bypass_magazines)
allocator->max_slab_chunk_size_for_magazine_cache = 0; /* non-optimized cases */
/* at this point, g_mem_gc_friendly() should be initialized, this
* should have been accomplished by the above g_malloc/g_new calls
*/
}
static inline guint
......
......@@ -323,8 +323,6 @@ get_C_locale (void)
static gsize initialized = FALSE;
static locale_t C_locale = NULL;
g_thread_init_glib ();
if (g_once_init_enter (&initialized))
{
C_locale = newlocale (LC_ALL_MASK, "C", NULL);
......
......@@ -538,9 +538,6 @@ g_thread_error_quark (void)
/* Local Data {{{1 -------------------------------------------------------- */
gboolean g_threads_got_initialized = FALSE;
GSystemThread zero_thread; /* This is initialized to all zero */
GMutex g_once_mutex;
static GCond g_once_cond;
static GSList *g_once_init_list = NULL;
......@@ -550,74 +547,6 @@ static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup
G_LOCK_DEFINE_STATIC (g_thread_new);
/* Initialisation {{{1 ---------------------------------------------------- */
/**
* g_thread_init:
* @vtable: a function table of type #GThreadFunctions, that provides
* the entry points to the thread system to be used. Since 2.32,
* this parameter is ignored and should always be %NULL
*
* If you use GLib from more than one thread, you must initialize the
* thread system by calling g_thread_init().
*
* Since version 2.24, calling g_thread_init() multiple times is allowed,
* but nothing happens except for the first call.
*
* Since version 2.32, GLib does not support custom thread implementations
* anymore and the @vtable parameter is ignored and you should pass %NULL.
*
* <note><para>g_thread_init() must not be called directly or indirectly
* in a callback from GLib. Also no mutexes may be currently locked while
* calling g_thread_init().</para></note>
*
* <note><para>To use g_thread_init() in your program, you have to link
* with the libraries that the command <command>pkg-config --libs
* gthread-2.0</command> outputs. This is not the case for all the
* other thread-related functions of GLib. Those can be used without
* having to link with the thread libraries.</para></note>
*/
void
g_thread_init_glib (void)
{
static gboolean already_done;
GRealThread *main_thread;
if (already_done)
return;
already_done = TRUE;
/* We let the main thread (the one that calls g_thread_init) inherit
* the static_private data set before calling g_thread_init
*/
main_thread = (GRealThread*) g_thread_self ();
/* setup the basic threading system */
g_threads_got_initialized = TRUE;
g_private_set (&g_thread_specific_private, main_thread);
g_system_thread_self (&main_thread->system_thread);
/* accomplish log system initialization to enable messaging */
_g_messages_thread_init_nomessage ();
}
/**
* g_thread_get_initialized:
*
* Indicates if g_thread_init() has been called.
*
* Returns: %TRUE if threads have been initialized.
*
* Since: 2.20
<