mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-11-27 20:49:51 +01:00
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
This commit is contained in:
246
glib/glib-init.c
Normal file
246
glib/glib-init.c
Normal file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* 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
|
||||
Reference in New Issue
Block a user