Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / common / JackLibGlobals.h
blob6d77e469c1f0f5aa6cbf9719967cc3bee73c90eb
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 sigset_t fProcessSignals;
60 static int fClientCount;
61 static JackLibGlobals* fGlobals;
63 JackLibGlobals()
65 jack_log("JackLibGlobals");
66 if (!JackMessageBuffer::Create()) {
67 jack_error("Cannot create message buffer");
69 fGraphManager = -1;
70 fEngineControl = -1;
72 // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
73 #ifdef WIN32
74 // TODO
75 #else
76 sigset_t signals;
77 sigemptyset(&signals);
78 sigaddset(&signals, SIGPIPE);
79 sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
80 #endif
83 ~JackLibGlobals()
85 jack_log("~JackLibGlobals");
86 for (int i = 0; i < CLIENT_NUM; i++) {
87 fSynchroTable[i].Disconnect();
89 JackMessageBuffer::Destroy();
91 // Restore old signal mask
92 #ifdef WIN32
93 // TODO
94 #else
95 sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
96 #endif
99 static void Init()
101 if (!JackGlobals::fServerRunning && fClientCount > 0) {
103 // Cleanup remaining clients
104 jack_error("Jack server was closed but clients are still allocated, cleanup...");
105 for (int i = 0; i < CLIENT_NUM; i++) {
106 JackClient* client = JackGlobals::fClientTable[i];
107 if (client) {
108 jack_error("Cleanup client ref = %d", i);
109 client->Close();
110 delete client;
111 JackGlobals::fClientTable[CLIENT_NUM] = NULL;
115 // Cleanup global context
116 fClientCount = 0;
117 delete fGlobals;
118 fGlobals = NULL;
121 if (fClientCount++ == 0 && !fGlobals) {
122 jack_log("JackLibGlobals Init %x", fGlobals);
123 InitTime();
124 fGlobals = new JackLibGlobals();
128 static void Destroy()
130 if (--fClientCount == 0 && fGlobals) {
131 jack_log("JackLibGlobals Destroy %x", fGlobals);
132 EndTime();
133 delete fGlobals;
134 fGlobals = NULL;
140 } // end of namespace
142 #endif