diff --git a/ChangeLog b/ChangeLog index c403217d..91b7049e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,10 @@ Michael Voigt Jackdmp changes log --------------------------- +2009-01-14 Stephane Letz + + * Cleanup server starting code for clients directly linked with libjackserver.so. + 2009-01-07 Stephane Letz * Use up to BUFFER_SIZE_MAX frames in midi ports, fix for ticket #117. diff --git a/common/JackInternalClient.cpp b/common/JackInternalClient.cpp index 13087448..b0784e36 100644 --- a/common/JackInternalClient.cpp +++ b/common/JackInternalClient.cpp @@ -19,6 +19,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "JackSystemDeps.h" +#include "JackServerGlobals.h" #include "JackGraphManager.h" #include "JackConstants.h" #include "JackInternalClient.h" @@ -87,17 +88,17 @@ JackEngineControl* JackInternalClient::fEngineControl = NULL; // Used for external C API (JackAPI.cpp) JackGraphManager* GetGraphManager() { - return JackServer::fInstance->GetGraphManager(); + return JackServerGlobals::fInstance->GetGraphManager(); } JackEngineControl* GetEngineControl() { - return JackServer::fInstance->GetEngineControl(); + return JackServerGlobals::fInstance->GetEngineControl(); } JackSynchro* GetSynchroTable() { - return JackServer::fInstance->GetSynchroTable(); + return JackServerGlobals::fInstance->GetSynchroTable(); } JackInternalClient::JackInternalClient(JackServer* server, JackSynchro* table): JackClient(table) diff --git a/common/JackNetAdapter.cpp b/common/JackNetAdapter.cpp index 6de39b11..7b375b3b 100644 --- a/common/JackNetAdapter.cpp +++ b/common/JackNetAdapter.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #include "JackNetAdapter.h" #include "JackException.h" -#include "JackServer.h" +#include "JackServerGlobals.h" #include "JackEngineControl.h" namespace Jack @@ -216,7 +216,7 @@ namespace Jack SetAdaptedBufferSize ( fParams.fPeriodSize ); SetAdaptedSampleRate ( fParams.fSampleRate ); - if (fThread.AcquireRealTime ( JackServer::fInstance->GetEngineControl()->fClientPriority ) < 0) { + if (fThread.AcquireRealTime ( JackServerGlobals::fInstance->GetEngineControl()->fClientPriority ) < 0) { jack_error("AcquireRealTime error"); } else { set_threaded_log_function(); diff --git a/common/JackServer.cpp b/common/JackServer.cpp index 216e0a3a..25160945 100644 --- a/common/JackServer.cpp +++ b/common/JackServer.cpp @@ -19,7 +19,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "JackSystemDeps.h" -#include "JackServer.h" +#include "JackServerGlobals.h" #include "JackTime.h" #include "JackFreewheelDriver.h" #include "JackLoopbackDriver.h" @@ -38,8 +38,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { -JackServer* JackServer::fInstance = NULL; - JackServer::JackServer(bool sync, bool temporary, long timeout, bool rt, long priority, long loopback, bool verbose, const char* server_name) { if (rt) { @@ -56,7 +54,8 @@ JackServer::JackServer(bool sync, bool temporary, long timeout, bool rt, long pr fAudioDriver = NULL; fFreewheel = false; fLoopback = loopback; - fInstance = this; // Unique instance + JackServerGlobals::fInstance = this; // Unique instance + JackServerGlobals::fUserCount = 1; // One user jack_verbose = verbose; } @@ -159,14 +158,14 @@ int JackServer::Close() int JackServer::InternalClientLoad(const char* client_name, const char* so_name, const char* objet_data, int options, int* int_ref, int* status) { - JackLoadableInternalClient* client = new JackLoadableInternalClient1(fInstance, GetSynchroTable(), so_name, objet_data); + JackLoadableInternalClient* client = new JackLoadableInternalClient1(JackServerGlobals::fInstance, GetSynchroTable(), so_name, objet_data); assert(client); return InternalClientLoadAux(client, so_name, client_name, options, int_ref, status); } int JackServer::InternalClientLoad(const char* client_name, const char* so_name, const JSList * parameters, int options, int* int_ref, int* status) { - JackLoadableInternalClient* client = new JackLoadableInternalClient2(fInstance, GetSynchroTable(), so_name, parameters); + JackLoadableInternalClient* client = new JackLoadableInternalClient2(JackServerGlobals::fInstance, GetSynchroTable(), so_name, parameters); assert(client); return InternalClientLoadAux(client, so_name, client_name, options, int_ref, status); } diff --git a/common/JackServer.h b/common/JackServer.h index aaac22f4..d802965a 100644 --- a/common/JackServer.h +++ b/common/JackServer.h @@ -93,7 +93,6 @@ class SERVER_EXPORT JackServer JackSynchro* GetSynchroTable(); JackGraphManager* GetGraphManager(); - static JackServer* fInstance; // Unique instance }; } // end of namespace diff --git a/common/JackServerAPI.cpp b/common/JackServerAPI.cpp index 61300939..ca702287 100644 --- a/common/JackServerAPI.cpp +++ b/common/JackServerAPI.cpp @@ -48,9 +48,6 @@ extern "C" using namespace Jack; -// beware!!! things can go nasty if one client is started with JackNoStartServer and another without it -static bool g_nostart; - EXPORT jack_client_t* jack_client_open_aux(const char* ext_client_name, jack_options_t options, jack_status_t* status, va_list ap) { jack_varargs_t va; /* variable arguments */ @@ -84,27 +81,22 @@ EXPORT jack_client_t* jack_client_open_aux(const char* ext_client_name, jack_opt jack_varargs_init(&va); } - g_nostart = (options & JackNoStartServer) != 0; - if (!g_nostart) { - if (!JackServerGlobals::Init()) { // jack server initialisation - int my_status1 = (JackFailure | JackServerError); - *status = (jack_status_t)my_status1; - return NULL; - } + if (!JackServerGlobals::Init()) { // jack server initialisation + int my_status1 = (JackFailure | JackServerError); + *status = (jack_status_t)my_status1; + return NULL; } - + if (JACK_DEBUG) { - client = new JackDebugClient(new JackInternalClient(JackServer::fInstance, GetSynchroTable())); // Debug mode + client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode } else { - client = new JackInternalClient(JackServer::fInstance, GetSynchroTable()); + client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable()); } int res = client->Open(va.server_name, client_name, options, status); if (res < 0) { delete client; - if (!g_nostart) { - JackServerGlobals::Destroy(); // jack server destruction - } + JackServerGlobals::Destroy(); // jack server destruction int my_status1 = (JackFailure | JackServerError); *status = (jack_status_t)my_status1; return NULL; @@ -137,9 +129,7 @@ EXPORT int jack_client_close(jack_client_t* ext_client) } else { res = client->Close(); delete client; - if (!g_nostart) { - JackServerGlobals::Destroy(); // jack server destruction - } + JackServerGlobals::Destroy(); // jack server destruction jack_log("jack_client_close res = %d", res); } JackGlobals::fOpenMutex->Unlock(); @@ -148,8 +138,8 @@ EXPORT int jack_client_close(jack_client_t* ext_client) EXPORT int jack_get_client_pid(const char *name) { - return (JackServer::fInstance != NULL) - ? JackServer::fInstance->GetEngine()->GetClientPID(name) + return (JackServerGlobals::fInstance != NULL) + ? JackServerGlobals::fInstance->GetEngine()->GetClientPID(name) : 0; } diff --git a/common/JackServerGlobals.cpp b/common/JackServerGlobals.cpp index ea1d7d20..612e3dc5 100644 --- a/common/JackServerGlobals.cpp +++ b/common/JackServerGlobals.cpp @@ -28,8 +28,8 @@ static char* server_name = NULL; namespace Jack { -unsigned int JackServerGlobals::fClientCount = 0; -JackServer* JackServerGlobals::fServer = NULL; +JackServer* JackServerGlobals::fInstance; +unsigned int JackServerGlobals::fUserCount; int JackServerGlobals::Start(const char* server_name, jack_driver_desc_t* driver_desc, @@ -43,34 +43,27 @@ int JackServerGlobals::Start(const char* server_name, int verbose) { jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose); - fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name); - int res = fServer->Open(driver_desc, driver_params); - return (res < 0) ? res : fServer->Start(); + new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name); // Will setup fInstance and fUserCount globals + int res = fInstance->Open(driver_desc, driver_params); + return (res < 0) ? res : fInstance->Start(); } -int JackServerGlobals::Stop() +void JackServerGlobals::Stop() { - fServer->Stop(); - fServer->Close(); jack_log("Jackdmp: server close"); - delete fServer; - jack_log("Jackdmp: delete server"); - return 0; + fInstance->Stop(); + fInstance->Close(); } -int JackServerGlobals::Delete() +void JackServerGlobals::Delete() { - delete fServer; jack_log("Jackdmp: delete server"); - return 0; + delete fInstance; + fInstance = NULL; } bool JackServerGlobals::Init() { - // Server already started - if (JackServer::fInstance != NULL) - return true; - int realtime = 0; int client_timeout = 0; /* msecs; if zero, use period size. */ int realtime_priority = 10; @@ -86,7 +79,7 @@ bool JackServerGlobals::Init() int seen_driver = 0; char *driver_name = NULL; char **driver_args = NULL; - JSList* driver_params; + JSList* driver_params = NULL; int driver_nargs = 1; JSList* drivers = NULL; int show_version = 0; @@ -99,17 +92,16 @@ bool JackServerGlobals::Init() char buffer[255]; int argc = 0; char* argv[32]; - - // Otherwise first client starts the server - if (fClientCount++ == 0) { + + // First user starts the server + if (fUserCount++ == 0) { jack_log("JackServerGlobals Init"); jack_driver_desc_t* driver_desc; const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:"; static struct option long_options[] = { - { "driver", 1, 0, 'd' - }, + { "driver", 1, 0, 'd' }, { "verbose", 0, 0, 'v' }, { "help", 0, 0, 'h' }, { "port-max", 1, 0, 'p' }, @@ -307,15 +299,16 @@ bool JackServerGlobals::Init() error: if (driver_params) jack_free_driver_params(driver_params); - fClientCount--; + fUserCount--; return false; } void JackServerGlobals::Destroy() { - if (--fClientCount == 0) { + if (--fUserCount == 0) { jack_log("JackServerGlobals Destroy"); Stop(); + Delete(); jack_cleanup_shm(); JackTools::CleanupFiles(server_name); jack_unregister_server(server_name); diff --git a/common/JackServerGlobals.h b/common/JackServerGlobals.h index baea169b..736f9608 100644 --- a/common/JackServerGlobals.h +++ b/common/JackServerGlobals.h @@ -34,11 +34,11 @@ class JackClient; \brief Global server static structure: singleton kind of pattern. */ -struct JackServerGlobals +struct SERVER_EXPORT JackServerGlobals { - static unsigned int fClientCount; - static JackServer* fServer; - + static JackServer* fInstance; + static unsigned int fUserCount; + JackServerGlobals(); ~JackServerGlobals(); @@ -54,8 +54,8 @@ struct JackServerGlobals int priority, int loopback, int verbose); - static int Stop(); - static int Delete(); + static void Stop(); + static void Delete(); }; } // end of namespace