Merge branch 'master' into develop
[jack2.git] / common / JackLibGlobals.h
blobbca775a37516d6440c508ae13730093764057ac1
1 /*
2 Copyright (C) 2005 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef __JackLibGlobals__
21 #define __JackLibGlobals__
23 #include "JackShmMem.h"
24 #include "JackEngineControl.h"
25 #include "JackGlobals.h"
26 #include "JackPlatformPlug.h"
27 #include "JackGraphManager.h"
28 #include "JackMessageBuffer.h"
29 #include "JackTime.h"
30 #include "JackClient.h"
31 #include "JackError.h"
32 #include <assert.h>
33 #include <signal.h>
35 #ifdef WIN32
36 #ifdef __MINGW32__
37 #include <sys/types.h>
38 typedef _sigset_t sigset_t;
39 #else
40 typedef HANDLE sigset_t;
41 #endif
42 #endif
44 namespace Jack
47 class JackClient;
49 /*!
50 \brief Global library static structure: singleton kind of pattern.
53 struct JackLibGlobals
55 JackShmReadWritePtr<JackGraphManager> fGraphManager; /*! Shared memory Port manager */
56 JackShmReadWritePtr<JackEngineControl> fEngineControl; /*! Shared engine control */ // transport engine has to be writable
57 JackSynchro fSynchroTable[CLIENT_NUM]; /*! Shared synchro table */
58 JackMetadata *fMetadata; /*! Shared metadata base */
59 sigset_t fProcessSignals;
61 static int fClientCount;
62 static JackLibGlobals* fGlobals;
64 JackLibGlobals()
66 jack_log("JackLibGlobals");
67 if (!JackMessageBuffer::Create()) {
68 jack_error("Cannot create message buffer");
70 fGraphManager = -1;
71 fEngineControl = -1;
73 fMetadata = new JackMetadata(false);
75 // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
76 #ifdef WIN32
77 // TODO
78 #else
79 sigset_t signals;
80 sigemptyset(&signals);
81 sigaddset(&signals, SIGPIPE);
82 sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
83 #endif
86 ~JackLibGlobals()
88 jack_log("~JackLibGlobals");
89 for (int i = 0; i < CLIENT_NUM; i++) {
90 fSynchroTable[i].Disconnect();
92 JackMessageBuffer::Destroy();
94 delete fMetadata;
95 fMetadata = NULL;
97 // Restore old signal mask
98 #ifdef WIN32
99 // TODO
100 #else
101 sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
102 #endif
105 static void Init()
107 if (!JackGlobals::fServerRunning && fClientCount > 0) {
109 // Cleanup remaining clients
110 jack_error("Jack server was closed but clients are still allocated, cleanup...");
111 for (int i = 0; i < CLIENT_NUM; i++) {
112 JackClient* client = JackGlobals::fClientTable[i];
113 if (client) {
114 jack_error("Cleanup client ref = %d", i);
115 client->Close();
116 delete client;
120 // Cleanup global context
121 fClientCount = 0;
122 delete fGlobals;
123 fGlobals = NULL;
126 if (fClientCount++ == 0 && !fGlobals) {
127 jack_log("JackLibGlobals Init %x", fGlobals);
128 InitTime();
129 fGlobals = new JackLibGlobals();
133 static void Destroy()
135 if (--fClientCount == 0 && fGlobals) {
136 jack_log("JackLibGlobals Destroy %x", fGlobals);
137 EndTime();
138 delete fGlobals;
139 fGlobals = NULL;
145 } // end of namespace
147 #endif