runtime: initialise global data when the first bshell_runtime is created
This commit is contained in:
@@ -1,9 +1,23 @@
|
|||||||
#include "scope.h"
|
#include "scope.h"
|
||||||
|
|
||||||
#include <bshell/runtime/runtime.h>
|
#include <bshell/runtime/runtime.h>
|
||||||
|
#include <bshell/verb.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
static fx_once global_once = FX_ONCE_INIT;
|
||||||
|
|
||||||
|
static void cleanup_globals(void)
|
||||||
|
{
|
||||||
|
bshell_cleanup_all_verbs();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_globals(void)
|
||||||
|
{
|
||||||
|
bshell_init_all_verbs();
|
||||||
|
bshell_table_init();
|
||||||
|
}
|
||||||
|
|
||||||
struct bshell_runtime *bshell_runtime_create(void)
|
struct bshell_runtime *bshell_runtime_create(void)
|
||||||
{
|
{
|
||||||
struct bshell_runtime *out = malloc(sizeof *out);
|
struct bshell_runtime *out = malloc(sizeof *out);
|
||||||
@@ -16,6 +30,11 @@ struct bshell_runtime *bshell_runtime_create(void)
|
|||||||
out->rt_global = runtime_push_scope(out, RUNTIME_SCOPE_GLOBAL, NULL);
|
out->rt_global = runtime_push_scope(out, RUNTIME_SCOPE_GLOBAL, NULL);
|
||||||
out->rt_aliases = fx_hashtable_create();
|
out->rt_aliases = fx_hashtable_create();
|
||||||
|
|
||||||
|
if (fx_init_once(&global_once)) {
|
||||||
|
init_globals();
|
||||||
|
atexit(cleanup_globals);
|
||||||
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user