Merge branch 'th/prgname' into 'main'

[th/prgname] use atomic pointers for g_prgname/g_application_name and add g_set_prgname_once()

See merge request GNOME/glib!3699
This commit is contained in:
Philip Withnall 2023-11-13 11:47:44 +00:00
commit b50a8a69d9
7 changed files with 52 additions and 38 deletions

View File

@ -38,6 +38,7 @@
#include "gioenumtypes.h"
#include "gioenums.h"
#include "gfile.h"
#include "glib-private.h"
#include "glibintl.h"
#include "gmarshal-internal.h"
@ -2526,7 +2527,7 @@ g_application_run (GApplication *application,
gchar *prgname;
prgname = g_path_get_basename (argv[0]);
g_set_prgname (prgname);
GLIB_PRIVATE_CALL (g_set_prgname_once) (prgname);
g_free (prgname);
}

View File

@ -23,6 +23,7 @@
#include "glib-private.h"
#include "glib-init.h"
#include "gutilsprivate.h"
#ifdef USE_INVALID_PARAMETER_HANDLER
#include <crtdbg.h>
@ -71,6 +72,8 @@ glib__private__ (void)
g_find_program_for_path,
g_uri_get_default_scheme_port,
g_set_prgname_once,
};
return &table;

View File

@ -288,6 +288,9 @@ typedef struct {
/* See guri.c */
int (* g_uri_get_default_scheme_port) (const char *scheme);
/* See gutils.c */
gboolean (* g_set_prgname_once) (const gchar *prgname);
/* Add other private functions here, initialize them in glib-private.c */
} GLibPrivateVTable;

View File

@ -35,6 +35,7 @@
#include "gprintf.h"
#include "glibintl.h"
#include "gutilsprivate.h"
#if defined G_OS_WIN32
#include <windows.h>
@ -1813,10 +1814,7 @@ g_option_context_parse (GOptionContext *context,
else
prgname = platform_get_argv0 ();
if (prgname)
g_set_prgname (prgname);
else
g_set_prgname ("<unknown>");
g_set_prgname_once (prgname ? prgname : "<unknown");
g_free (prgname);
}

View File

@ -1657,8 +1657,8 @@ void
test_run_seedstr = seedstr;
}
if (!g_get_prgname() && !no_g_set_prgname)
g_set_prgname ((*argv)[0]);
if (!g_get_prgname () && !no_g_set_prgname)
g_set_prgname_once ((*argv)[0]);
if (g_getenv ("G_TEST_ROOT_PROCESS"))
{

View File

@ -1128,7 +1128,6 @@ g_get_host_name (void)
return hostname;
}
G_LOCK_DEFINE_STATIC (g_prgname);
static const gchar *g_prgname = NULL; /* always a quark */
/**
@ -1150,13 +1149,7 @@ static const gchar *g_prgname = NULL; /* always a quark */
const gchar*
g_get_prgname (void)
{
const gchar* retval;
G_LOCK (g_prgname);
retval = g_prgname;
G_UNLOCK (g_prgname);
return retval;
return g_atomic_pointer_get (&g_prgname);
}
/**
@ -1179,13 +1172,29 @@ g_get_prgname (void)
void
g_set_prgname (const gchar *prgname)
{
GQuark qprgname = g_quark_from_string (prgname);
G_LOCK (g_prgname);
g_prgname = g_quark_to_string (qprgname);
G_UNLOCK (g_prgname);
prgname = g_intern_string (prgname);
g_atomic_pointer_set (&g_prgname, prgname);
}
/**
* g_set_prgname_once:
* @prgname: the name of the program.
*
* If g_get_prgname() is not set, this is the same as setting
* the name via g_set_prgname() and %TRUE is returned. Otherwise,
* does nothing and returns %FALSE. This is thread-safe.
*
* Returns: whether g_prgname was initialized by the call.
*/
gboolean
g_set_prgname_once (const gchar *prgname)
{
/* if @prgname is NULL, then this has the same effect as calling
* (g_get_prgname()==NULL). */
prgname = g_intern_string (prgname);
return g_atomic_pointer_compare_and_exchange (&g_prgname, NULL, prgname);
}
G_LOCK_DEFINE_STATIC (g_application_name);
static gchar *g_application_name = NULL;
/**
@ -1207,16 +1216,14 @@ static gchar *g_application_name = NULL;
const gchar *
g_get_application_name (void)
{
gchar* retval;
const char *retval;
G_LOCK (g_application_name);
retval = g_application_name;
G_UNLOCK (g_application_name);
retval = g_atomic_pointer_get (&g_application_name);
if (retval == NULL)
return g_get_prgname ();
return retval;
if (retval)
return retval;
return g_get_prgname ();
}
/**
@ -1240,17 +1247,17 @@ g_get_application_name (void)
void
g_set_application_name (const gchar *application_name)
{
gboolean already_set = FALSE;
G_LOCK (g_application_name);
if (g_application_name)
already_set = TRUE;
else
g_application_name = g_strdup (application_name);
G_UNLOCK (g_application_name);
char *name;
if (already_set)
g_warning ("g_set_application_name() called multiple times");
g_return_if_fail (application_name);
name = g_strdup (application_name);
if (!g_atomic_pointer_compare_and_exchange (&g_application_name, NULL, name))
{
g_warning ("g_set_application_name() called multiple times");
g_free (name);
}
}
#ifdef G_OS_WIN32

View File

@ -59,6 +59,8 @@ void _g_unset_cached_tmp_dir (void);
gboolean _g_localtime (time_t timet, struct tm *tm);
gboolean g_set_prgname_once (const gchar *prgname);
G_END_DECLS
#endif /* __G_UTILS_PRIVATE_H__ */