tests/constructor: Support systems where dlclose is a no-op

POSIX allows dlclose() implementations that are no-ops,
and in such case library destructors run at application
exit rather than dlclose().

That's the case, for example, of UNIX systems with the
Musl LibC.
This commit is contained in:
Luca Bacci 2023-09-27 16:36:42 +02:00
parent 85e21ff757
commit c4c39ea52a
2 changed files with 31 additions and 4 deletions

View File

@ -61,6 +61,9 @@ void string_add_exclusive (const char *string);
MODULE_EXPORT
int string_remove (const char *string);
MODULE_EXPORT
int string_find (const char *string);
MODULE_EXPORT
void string_check (const char *string);
@ -204,6 +207,19 @@ string_remove (const char *string)
return 1;
}
/**< private >
* string_find:
*
* @string: NULL-terminated string. Must not be empty
*
* Returns 1 if the string is present, 0 otherwise
*/
MODULE_EXPORT
int string_find (const char *string)
{
return string_find_index_ (string) < sizeof (buffer);
}
/**< private >
* string_check:
*

View File

@ -40,6 +40,8 @@ void string_add_exclusive (const char *string);
MODULE_IMPORT
void string_check (const char *string);
MODULE_IMPORT
int string_find (const char *string);
#if G_HAS_CONSTRUCTORS
@ -66,9 +68,13 @@ dtor (void)
{
string_add_exclusive (G_STRINGIFY (PREFIX) "_" "dtor");
#ifdef BUILD_TEST_EXECUTABLE
_Exit (EXIT_SUCCESS);
#endif
if (string_find ("app_dtor") && string_find ("lib_dtor"))
{
/* All destructors were invoked, this is the last.
* Call _Exit (EXIT_SUCCESS) to exit immediately
* with a success code */
_Exit (EXIT_SUCCESS);
}
}
#endif /* G_HAS_CONSTRUCTORS */
@ -210,7 +216,12 @@ test_lib (gconstpointer data)
unload_library ();
#if G_HAS_DESTRUCTORS
string_check ("lib_" "dtor");
/* Destructors in dynamically-loaded libraries do not
* necessarily run on dlclose. On some systems dlclose
* is effectively a no-op (e.g with the Musl LibC) and
* destructors run at program exit */
g_test_message ("Destructors run on module unload: %s\n",
string_find ("lib_" "dtor") ? "yes" : "no");
#endif
#if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS)
string_check ("lib_" "tlscb_process_detach");