Introduce our own shutdown callback mechanism.

Instead of adding stuff to `_shutdown()`, we can now register a shutdown
routine from the respective `init()` function of our modules.

This also has the advantage, that we're sure they're called in reverse
order from how the initialization happened.

I didn't simply use `atexit()` because POSIX says that the number of
possible callbacks is limited (min 32) and I was not sure whether
we will maybe extend this number at one point.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-07 11:44:31 +01:00
parent 662a0be633
commit c5a131ee46
39 changed files with 321 additions and 327 deletions

View File

@@ -65,9 +65,49 @@
static GHashTable* plugins;
static void
_plugins_shutdown(void)
{
GList* values = g_hash_table_get_values(plugins);
GList *curr = values, *next;
while (curr) {
next = g_list_next(curr);
#ifdef HAVE_PYTHON
if (curr && ((ProfPlugin*)curr->data)->lang == LANG_PYTHON) {
python_plugin_destroy(curr->data);
curr = NULL;
}
#endif
#ifdef HAVE_C
if (curr && ((ProfPlugin*)curr->data)->lang == LANG_C) {
c_plugin_destroy(curr->data);
curr = NULL;
}
#endif
curr = next;
}
g_list_free(values);
#ifdef HAVE_PYTHON
python_shutdown();
#endif
#ifdef HAVE_C
c_shutdown();
#endif
autocompleters_destroy();
plugin_themes_close();
plugin_settings_close();
callbacks_close();
disco_close();
g_hash_table_destroy(plugins);
plugins = NULL;
}
void
plugins_init(void)
{
prof_add_shutdown_routine(_plugins_shutdown);
plugins = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
callbacks_init();
autocompleters_init();
@@ -421,21 +461,8 @@ plugins_close_win(const char* const plugin_name, const char* const tag)
callbacks_remove_win(plugin_name, tag);
}
void
plugins_on_start(void)
{
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
plugin->on_start_func(plugin);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_shutdown(void)
static void
_plugins_on_shutdown(void)
{
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
@@ -447,6 +474,20 @@ plugins_on_shutdown(void)
g_list_free(values);
}
void
plugins_on_start(void)
{
prof_add_shutdown_routine(_plugins_on_shutdown);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
plugin->on_start_func(plugin);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_connect(const char* const account_name, const char* const fulljid)
{
@@ -924,42 +965,3 @@ plugins_get_disco_features(void)
{
return disco_get_features();
}
void
plugins_shutdown(void)
{
GList* values = g_hash_table_get_values(plugins);
GList *curr = values, *next;
while (curr) {
next = g_list_next(curr);
#ifdef HAVE_PYTHON
if (curr && ((ProfPlugin*)curr->data)->lang == LANG_PYTHON) {
python_plugin_destroy(curr->data);
curr = NULL;
}
#endif
#ifdef HAVE_C
if (curr && ((ProfPlugin*)curr->data)->lang == LANG_C) {
c_plugin_destroy(curr->data);
curr = NULL;
}
#endif
curr = next;
}
g_list_free(values);
#ifdef HAVE_PYTHON
python_shutdown();
#endif
#ifdef HAVE_C
c_shutdown();
#endif
autocompleters_destroy();
plugin_themes_close();
plugin_settings_close();
callbacks_close();
disco_close();
g_hash_table_destroy(plugins);
plugins = NULL;
}