Jack2 1.9.8

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 #ifdef WIN32
00036 #ifdef __MINGW32__
00037 #include <sys/types.h>
00038 typedef _sigset_t sigset_t;
00039 #else
00040 typedef HANDLE sigset_t;
00041 #endif
00042 #endif
00043 
00044 namespace Jack
00045 {
00046 
00047 class JackClient;
00048 
00053 struct LIB_EXPORT JackLibGlobals
00054 {
00055     JackShmReadWritePtr<JackGraphManager> fGraphManager;        
00056     JackShmReadWritePtr<JackEngineControl> fEngineControl;        // transport engine has to be writable
00057     JackSynchro fSynchroTable[CLIENT_NUM];                  
00058     sigset_t fProcessSignals;
00059 
00060     static int fClientCount;
00061     static JackLibGlobals* fGlobals;
00062 
00063     JackLibGlobals()
00064     {
00065         jack_log("JackLibGlobals");
00066         JackMessageBuffer::Create();
00067         fGraphManager = -1;
00068         fEngineControl = -1;
00069 
00070         // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
00071     #ifdef WIN32
00072         // TODO
00073     #else
00074         sigset_t signals;
00075         sigemptyset(&signals);
00076         sigaddset(&signals, SIGPIPE);
00077         sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
00078     #endif
00079     }
00080 
00081     ~JackLibGlobals()
00082     {
00083         jack_log("~JackLibGlobals");
00084         for (int i = 0; i < CLIENT_NUM; i++) {
00085             fSynchroTable[i].Disconnect();
00086         }
00087         JackMessageBuffer::Destroy();
00088 
00089        // Restore old signal mask
00090     #ifdef WIN32
00091        // TODO
00092     #else
00093        sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
00094     #endif
00095     }
00096 
00097     static void Init()
00098     {
00099         if (!JackGlobals::fServerRunning && fClientCount > 0) {
00100 
00101             // Cleanup remaining clients
00102             jack_error("Jack server was closed but clients are still allocated, cleanup...");
00103             for (int i = 0; i < CLIENT_NUM; i++) {
00104                 JackClient* client = JackGlobals::fClientTable[i];
00105                 if (client) {
00106                     jack_error("Cleanup client ref = %d", i);
00107                     client->Close();
00108                     delete client;
00109                     JackGlobals::fClientTable[CLIENT_NUM] = NULL;
00110                 }
00111             }
00112 
00113             // Cleanup global context
00114             fClientCount = 0;
00115             delete fGlobals;
00116             fGlobals = NULL;
00117         }
00118 
00119         if (fClientCount++ == 0 && !fGlobals) {
00120             jack_log("JackLibGlobals Init %x", fGlobals);
00121             InitTime();
00122             fGlobals = new JackLibGlobals();
00123         }
00124     }
00125 
00126     static void Destroy()
00127     {
00128         if (--fClientCount == 0 && fGlobals) {
00129             jack_log("JackLibGlobals Destroy %x", fGlobals);
00130             delete fGlobals;
00131             fGlobals = NULL;
00132         }
00133     }
00134 
00135 };
00136 
00137 } // end of namespace
00138 
00139 #endif
00140