Jack2 1.9.10

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 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         if (!JackMessageBuffer::Create()) {
00067             jack_error("Cannot create message buffer");
00068         }
00069         fGraphManager = -1;
00070         fEngineControl = -1;
00071 
00072         // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
00073     #ifdef WIN32
00074         // TODO
00075     #else
00076         sigset_t signals;
00077         sigemptyset(&signals);
00078         sigaddset(&signals, SIGPIPE);
00079         sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
00080     #endif
00081     }
00082 
00083     ~JackLibGlobals()
00084     {
00085         jack_log("~JackLibGlobals");
00086         for (int i = 0; i < CLIENT_NUM; i++) {
00087             fSynchroTable[i].Disconnect();
00088         }
00089         JackMessageBuffer::Destroy();
00090 
00091        // Restore old signal mask
00092     #ifdef WIN32
00093        // TODO
00094     #else
00095        sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
00096     #endif
00097     }
00098 
00099     static void Init()
00100     {
00101         if (!JackGlobals::fServerRunning && fClientCount > 0) {
00102 
00103             // Cleanup remaining clients
00104             jack_error("Jack server was closed but clients are still allocated, cleanup...");
00105             for (int i = 0; i < CLIENT_NUM; i++) {
00106                 JackClient* client = JackGlobals::fClientTable[i];
00107                 if (client) {
00108                     jack_error("Cleanup client ref = %d", i);
00109                     client->Close();
00110                     delete client;
00111                 }
00112             }
00113 
00114             // Cleanup global context
00115             fClientCount = 0;
00116             delete fGlobals;
00117             fGlobals = NULL;
00118         }
00119 
00120         if (fClientCount++ == 0 && !fGlobals) {
00121             jack_log("JackLibGlobals Init %x", fGlobals);
00122             InitTime();
00123             fGlobals = new JackLibGlobals();
00124         }
00125     }
00126 
00127     static void Destroy()
00128     {
00129         if (--fClientCount == 0 && fGlobals) {
00130             jack_log("JackLibGlobals Destroy %x", fGlobals);
00131             EndTime();
00132             delete fGlobals;
00133             fGlobals = NULL;
00134         }
00135     }
00136 
00137 };
00138 
00139 } // end of namespace
00140 
00141 #endif
00142