Jack2 1.9.6

JackLibGlobals.h

00001 /*
00002 Copyright (C) 2005 Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as published by
00006 the Free Software Foundation; either version 2.1 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public License
00015 along with this program; if not, write to the Free Software 
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #ifndef __JackLibGlobals__
00021 #define __JackLibGlobals__
00022 
00023 #include "JackShmMem.h"
00024 #include "JackEngineControl.h"
00025 #include "JackGlobals.h"
00026 #include "JackPlatformPlug.h"
00027 #include "JackGraphManager.h"
00028 #include "JackMessageBuffer.h"
00029 #include "JackTime.h"
00030 #include "JackClient.h"
00031 #include "JackError.h"
00032 #include <assert.h>
00033 #include <signal.h>
00034  
00035 
00036 namespace Jack
00037 {
00038 
00039 class JackClient;
00040 
00045 struct JackLibGlobals
00046 {
00047     JackShmReadWritePtr<JackGraphManager> fGraphManager;        
00048     JackShmReadWritePtr<JackEngineControl> fEngineControl;        // transport engine has to be writable
00049     JackSynchro fSynchroTable[CLIENT_NUM];                  
00050     sigset_t fProcessSignals;
00051  
00052     static int fClientCount;
00053     static JackLibGlobals* fGlobals;
00054 
00055     JackLibGlobals()
00056     {
00057         jack_log("JackLibGlobals");
00058         JackMessageBuffer::Create();
00059         fGraphManager = -1;
00060         fEngineControl = -1;
00061 
00062         // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
00063     #ifdef WIN32
00064         // TODO
00065     #else
00066         sigset_t signals;
00067         sigemptyset(&signals);
00068         sigaddset(&signals, SIGPIPE);
00069         sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
00070     #endif       
00071     }
00072 
00073     ~JackLibGlobals()
00074     {
00075         jack_log("~JackLibGlobals");
00076         for (int i = 0; i < CLIENT_NUM; i++) {
00077             fSynchroTable[i].Disconnect();
00078         }
00079         JackMessageBuffer::Destroy();
00080 
00081        // Restore old signal mask
00082     #ifdef WIN32
00083        // TODO
00084     #else
00085        sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
00086     #endif   
00087     }
00088 
00089     static void Init()
00090     {
00091         if (!JackGlobals::fServerRunning && fClientCount > 0) {
00092         
00093             // Cleanup remaining clients
00094             jack_error("Jack server was closed but clients are still allocated, cleanup...");
00095             for (int i = 0; i < CLIENT_NUM; i++) {
00096                 JackClient* client = JackGlobals::fClientTable[i];
00097                 if (client) {
00098                     jack_error("Cleanup client ref = %d", i);
00099                     client->Close();
00100                     delete client;
00101                     JackGlobals::fClientTable[CLIENT_NUM] = NULL;
00102                 }
00103             }
00104             
00105             // Cleanup global context
00106             fClientCount = 0;
00107             delete fGlobals;
00108             fGlobals = NULL;
00109         }
00110         
00111         if (fClientCount++ == 0 && !fGlobals) {
00112             jack_log("JackLibGlobals Init %x", fGlobals);
00113             InitTime();
00114             fGlobals = new JackLibGlobals();
00115         }
00116     }
00117 
00118     static void Destroy()
00119     {
00120         if (--fClientCount == 0 && fGlobals) {
00121             jack_log("JackLibGlobals Destroy %x", fGlobals);
00122             delete fGlobals;
00123             fGlobals = NULL;
00124         }
00125     }
00126 
00127 };
00128 
00129 } // end of namespace
00130 
00131 #endif
00132