[beast: 6/11] BSE: merge BSE startup routines before BSE thread starts



commit ac8a2cb0e82d6be42fa5b52a24f09a25a5e59165
Author: Tim Janik <timj gnu org>
Date:   Mon Sep 7 19:32:28 2015 +0200

    BSE: merge BSE startup routines before BSE thread starts

 bse/bsemain.cc |  211 +++++++++++++++++++++++++++++---------------------------
 1 files changed, 108 insertions(+), 103 deletions(-)
---
diff --git a/bse/bsemain.cc b/bse/bsemain.cc
index 299671b..5bbc979 100644
--- a/bse/bsemain.cc
+++ b/bse/bsemain.cc
@@ -22,7 +22,6 @@
 using namespace Rapicorn;
 
 /* --- prototypes --- */
-static void    bse_main_loop           (Rapicorn::AsyncBlockingQueue<int> *init_queue);
 static void    bse_async_parse_args    (int *argc_p, char **argv_p, BseMainArgs *margs, const 
Bse::StringVector &args);
 namespace Bse {
 static void     init_aida_idl ();
@@ -72,32 +71,59 @@ bse_gettext (const gchar *text)
   return dgettext (BSE_GETTEXT_DOMAIN, text);
 }
 
+static gboolean single_thread_registration_done = FALSE;
+
 static void
-bse_init_core (void)
+server_registration (SfiProxy            server,
+                     BseRegistrationType rtype,
+                     const gchar        *what,
+                     const gchar        *error,
+                     gpointer            data)
 {
-  /* global threading things */
+  // BseRegistrationType rtype = bse_registration_type_from_choice (rchoice);
+  if (rtype == BSE_REGISTER_DONE)
+    single_thread_registration_done = TRUE;
+  else
+    {
+      if (error && error[0])
+        sfi_diag ("failed to register \"%s\": %s", what, error);
+    }
+}
+
+static int initialized_for_unit_testing = -1;
+
+static void
+bse_init_intern()
+{
+  // paranoid assertions
+  if (bse_initialization_stage != 0 || ++bse_initialization_stage != 1)
+    g_error ("%s() may only be called once", "bse_init_inprocess");
+  g_assert (G_BYTE_ORDER == G_LITTLE_ENDIAN || G_BYTE_ORDER == G_BIG_ENDIAN);
+
+  // main loop and basic components
   bse_main_context = g_main_context_new ();
-  /* initialize basic components */
   bse_globals_init ();
   _bse_init_signal();
   _bse_init_categories ();
   bse_type_init ();
   bse_cxx_init ();
-  /* FIXME: global spawn dir is evil */
+  // FIXME: global spawn dir is evil
   {
     gchar *dir = g_get_current_dir ();
     sfi_com_set_spawn_dir (dir);
     g_free (dir);
   }
-  /* initialize GSL components */
+  // initialize GSL components
   gsl_init ();
-  /* remaining BSE components */
+  // remaining BSE components
   bse_plugin_init_builtins ();
-  /* initialize C wrappers around C++ generated types */
+  // initialize C wrappers around C++ generated types
   _bse_init_c_wrappers ();
-  /* make sure the server is alive */
+
+  // make sure the server object is alive
   bse_server_get ();
-  /* load drivers early */
+
+  // load drivers
   if (bse_main_args->load_drivers_early)
     {
       SfiRing *ring = bse_plugin_path_list_files (TRUE, FALSE);
@@ -111,7 +137,7 @@ bse_init_core (void)
         }
     }
 
-  /* dump device list */
+  // dump device list
   if (bse_main_args->dump_driver_list)
     {
       g_printerr ("%s", _("\nAvailable PCM drivers:\n"));
@@ -119,58 +145,8 @@ bse_init_core (void)
       g_printerr ("%s", _("\nAvailable MIDI drivers:\n"));
       bse_device_dump_list (BSE_TYPE_MIDI_DEVICE, "  ", TRUE, NULL, NULL);
     }
-}
-
-static gboolean single_thread_registration_done = FALSE;
-
-static void
-server_registration (SfiProxy            server,
-                     BseRegistrationType rtype,
-                     const gchar        *what,
-                     const gchar        *error,
-                     gpointer            data)
-{
-  // BseRegistrationType rtype = bse_registration_type_from_choice (rchoice);
-  if (rtype == BSE_REGISTER_DONE)
-    single_thread_registration_done = TRUE;
-  else
-    {
-      if (error && error[0])
-        sfi_diag ("failed to register \"%s\": %s", what, error);
-    }
-}
-
-static int initialized_for_unit_testing = -1;
-
-void
-bse_init_inprocess (int *argc, char **argv, const char *app_name, const Bse::StringVector &args)
-{
-  if (!bindtextdomain_initialized)
-    bse_bindtextdomain();
-
-  if (bse_initialization_stage != 0 || ++bse_initialization_stage != 1)
-    g_error ("%s() may only be called once", "bse_init_inprocess");
-
-  /* paranoid assertions */
-  g_assert (G_BYTE_ORDER == G_LITTLE_ENDIAN || G_BYTE_ORDER == G_BIG_ENDIAN);
-
-  /* initialize submodules */
-  if (initialized_for_unit_testing > 0)
-    sfi_init_test (argc, argv);
-  else
-    sfi_init (argc, argv, app_name);
-  bse_main_args = &default_main_args;
-  /* early argument handling */
-  if (argc && argv)
-    {
-      if (*argc && !g_get_prgname ())
-       g_set_prgname (*argv);
-      bse_async_parse_args (argc, argv, bse_main_args, args);
-    }
-
-  bse_init_core ();
 
-  /* initialize core plugins & scripts */
+  // initialize core plugins & scripts
   if (bse_main_args->load_core_plugins || bse_main_args->load_core_scripts)
       g_object_connect (bse_server_get(), "signal::registration", server_registration, NULL, NULL);
   if (bse_main_args->load_core_plugins)
@@ -195,49 +171,101 @@ bse_init_inprocess (int *argc, char **argv, const char *app_name, const Bse::Str
           // sfi_glue_gc_run ();
         }
     }
+
+  // unit testing message
   if (initialized_for_unit_testing > 0)
     {
       StringVector sv = Rapicorn::string_split (Rapicorn::cpu_info(), " ");
       String machine = sv.size() >= 2 ? sv[1] : "Unknown";
       TMSG ("  NOTE   Running on: %s+%s", machine.c_str(), bse_block_impl_name());
     }
-  // sfi_glue_gc_run ();
 }
 
 static std::thread async_bse_thread;
 
 void
-_bse_init_async (int *argc, char **argv, const char *app_name, const Bse::StringVector &args)
+bse_init_inprocess (int *argc, char **argv, const char *app_name, const Bse::StringVector &args)
 {
   assert (async_bse_thread.get_id() == std::thread::id());      // no async_bse_thread started
+  assert (bse_main_context == NULL);
+
+  // ensure textdomain for error messages
   if (!bindtextdomain_initialized)
     bse_bindtextdomain();
-  if (bse_initialization_stage != 0)
-    g_error ("%s() may only be called once", "bse_init_async");
-  bse_initialization_stage++;
-  if (bse_initialization_stage != 1)
-    g_error ("%s() may only be called once", "bse_init_async");
-  /* this function is running in the user program and needs to start the main BSE thread */
-  /* paranoid assertions */
-  g_assert (G_BYTE_ORDER == G_LITTLE_ENDIAN || G_BYTE_ORDER == G_BIG_ENDIAN);
-  /* initialize submodules */
-  sfi_init (argc, argv, app_name);
+  // setup GLib's prgname for error messages
+  if (argc && argv && *argc && !g_get_prgname ())
+    g_set_prgname (*argv);
+
+  // argument handling
   bse_main_args = &default_main_args;
-  /* handle argument early*/
   if (argc && argv)
+    bse_async_parse_args (argc, argv, bse_main_args, args);
+
+  // initialize SFI
+  if (initialized_for_unit_testing > 0)
+    sfi_init_test (argc, argv);
+  else
+    sfi_init (argc, argv, app_name);
+
+  // initialize globals, signals, types, builtins, etc
+  bse_init_intern ();
+}
+
+static void
+bse_main_loop_thread (Rapicorn::AsyncBlockingQueue<int> *init_queue)
+{
+  Bse::TaskRegistry::add ("BSE Core", Rapicorn::ThisThread::process_pid(), 
Rapicorn::ThisThread::thread_pid());
+  bse_init_intern ();
+  // start other threads
+  struct Internal : Bse::Sequencer { using Bse::Sequencer::_init_threaded; };
+  Internal::_init_threaded();
+  // allow aida IDL remoting
+  Bse::init_aida_idl();
+  // complete initialization
+  bse_initialization_stage++;   // = 2
+  init_queue->push ('B');       // signal completion to caller
+  init_queue = NULL;            // completion invalidates init_queue
+  // Bse Core Event Loop
+  while (true)                  // FIXME: missing exit handler
     {
-      if (*argc && !g_get_prgname ())
-       g_set_prgname (*argv);
-      bse_async_parse_args (argc, argv, bse_main_args, args);
+      g_main_context_pending (bse_main_context);
+      g_main_context_iteration (bse_main_context, TRUE);
     }
+  Bse::TaskRegistry::remove (Rapicorn::ThisThread::thread_pid());
+}
+
+void
+_bse_init_async (int *argc, char **argv, const char *app_name, const Bse::StringVector &args)
+{
+  assert (async_bse_thread.get_id() == std::thread::id());      // no async_bse_thread started
+  assert (bse_main_context == NULL);
+
+  // ensure textdomain for error messages
+  if (!bindtextdomain_initialized)
+    bse_bindtextdomain();
+  // setup GLib's prgname for error messages
+  if (argc && argv && *argc && !g_get_prgname ())
+    g_set_prgname (*argv);
+
+  // argument handling
+  bse_main_args = &default_main_args;
+  if (argc && argv)
+    bse_async_parse_args (argc, argv, bse_main_args, args);
+
+  // initialize SFI
+  if (initialized_for_unit_testing > 0)
+    sfi_init_test (argc, argv);
+  else
+    sfi_init (argc, argv, app_name);
+
   // start main BSE thread
   auto *init_queue = new Rapicorn::AsyncBlockingQueue<int>();
-  async_bse_thread = std::thread (bse_main_loop, init_queue);
+  async_bse_thread = std::thread (bse_main_loop_thread, init_queue); // calls bse_init_intern
   // wait for initialization completion of the core thread
   int msg = init_queue->pop();
   assert (msg == 'B');
   delete init_queue;
-  async_bse_thread.detach();    // FIXME: rather join on exit
+  async_bse_thread.detach();    // FIXME: join BSE thread on exit()
 }
 
 struct AsyncData {
@@ -296,29 +324,6 @@ bse_init_test (int *argc, char **argv, const Bse::StringVector &args)
   bse_init_inprocess (argc, argv, NULL, args);
 }
 
-static void
-bse_main_loop (Rapicorn::AsyncBlockingQueue<int> *init_queue)
-{
-  Bse::TaskRegistry::add ("BSE Core", Rapicorn::ThisThread::process_pid(), 
Rapicorn::ThisThread::thread_pid());
-  bse_init_core ();
-  // start other threads
-  struct Internal : Bse::Sequencer { using Bse::Sequencer::_init_threaded; };
-  Internal::_init_threaded();
-  // allow aida IDL remoting
-  Bse::init_aida_idl();
-  // complete initialization
-  bse_initialization_stage++;   // = 2
-  init_queue->push ('B');       // signal completion to caller
-  init_queue = NULL;            // completion invalidates init_queue
-  // Bse Core Event Loop
-  while (true)                  // FIXME: missing exit handler
-    {
-      g_main_context_pending (bse_main_context);
-      g_main_context_iteration (bse_main_context, TRUE);
-    }
-  Bse::TaskRegistry::remove (Rapicorn::ThisThread::thread_pid());
-}
-
 static guint
 get_n_processors (void)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]