Merge branch '715-setenv-warnings' into 'master'

genviron: Message if g_setenv()/g_unsetenv() are used after threads spawned

See merge request GNOME/glib!1337
This commit is contained in:
Philip Withnall 2020-02-03 12:34:47 +00:00
commit e5658c608d
3 changed files with 27 additions and 0 deletions

View File

@ -42,6 +42,7 @@
#include "gunicode.h"
#include "gconvert.h"
#include "gquark.h"
#include "gthreadprivate.h"
/* Environ array functions {{{1 */
static gboolean
@ -299,6 +300,13 @@ g_setenv (const gchar *variable,
g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE);
#ifndef G_DISABLE_CHECKS
/* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
* See https://gitlab.gnome.org/GNOME/glib/issues/715 */
if (g_thread_n_created () > 0)
g_debug ("setenv()/putenv() are not thread-safe and should not be used after threads are created");
#endif
#ifdef HAVE_SETENV
result = setenv (variable, value, overwrite);
#else
@ -355,6 +363,13 @@ g_unsetenv (const gchar *variable)
g_return_if_fail (variable != NULL);
g_return_if_fail (strchr (variable, '=') == NULL);
#ifndef G_DISABLE_CHECKS
/* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
* See https://gitlab.gnome.org/GNOME/glib/issues/715 */
if (g_thread_n_created () > 0)
g_debug ("unsetenv() is not thread-safe and should not be used after threads are created");
#endif
#ifdef HAVE_UNSETENV
unsetenv (variable);
#else /* !HAVE_UNSETENV */

View File

@ -512,6 +512,8 @@ static GMutex g_once_mutex;
static GCond g_once_cond;
static GSList *g_once_init_list = NULL;
static volatile guint g_thread_n_created_counter = 0;
static void g_thread_cleanup (gpointer data);
static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
@ -807,6 +809,12 @@ g_thread_proxy (gpointer data)
return NULL;
}
guint
g_thread_n_created (void)
{
return g_atomic_int_get (&g_thread_n_created_counter);
}
/**
* g_thread_new:
* @name: (nullable): an (optional) name for the new thread
@ -898,6 +906,8 @@ g_thread_new_internal (const gchar *name,
{
g_return_val_if_fail (func != NULL, NULL);
g_atomic_int_inc (&g_thread_n_created_counter);
return (GThread *) g_system_thread_new (proxy, stack_size, scheduler_settings,
name, func, data, error);
}

View File

@ -81,6 +81,8 @@ gboolean g_thread_get_scheduler_settings (GThreadSchedulerSettings *scheduler_se
gpointer g_thread_proxy (gpointer thread);
guint g_thread_n_created (void);
gpointer g_private_set_alloc0 (GPrivate *key,
gsize size);