Jack2  1.9.8
JackLibGlobals.h
1 /*
2 Copyright (C) 2005 Grame
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #ifndef __JackLibGlobals__
21 #define __JackLibGlobals__
22 
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>
34 
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
43 
44 namespace Jack
45 {
46 
47 class JackClient;
48 
53 struct LIB_EXPORT JackLibGlobals
54 {
56  JackShmReadWritePtr<JackEngineControl> fEngineControl; // transport engine has to be writable
57  JackSynchro fSynchroTable[CLIENT_NUM];
58  sigset_t fProcessSignals;
59 
60  static int fClientCount;
61  static JackLibGlobals* fGlobals;
62 
64  {
65  jack_log("JackLibGlobals");
66  JackMessageBuffer::Create();
67  fGraphManager = -1;
68  fEngineControl = -1;
69 
70  // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
71  #ifdef WIN32
72  // TODO
73  #else
74  sigset_t signals;
75  sigemptyset(&signals);
76  sigaddset(&signals, SIGPIPE);
77  sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
78  #endif
79  }
80 
82  {
83  jack_log("~JackLibGlobals");
84  for (int i = 0; i < CLIENT_NUM; i++) {
85  fSynchroTable[i].Disconnect();
86  }
87  JackMessageBuffer::Destroy();
88 
89  // Restore old signal mask
90  #ifdef WIN32
91  // TODO
92  #else
93  sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
94  #endif
95  }
96 
97  static void Init()
98  {
99  if (!JackGlobals::fServerRunning && fClientCount > 0) {
100 
101  // Cleanup remaining clients
102  jack_error("Jack server was closed but clients are still allocated, cleanup...");
103  for (int i = 0; i < CLIENT_NUM; i++) {
104  JackClient* client = JackGlobals::fClientTable[i];
105  if (client) {
106  jack_error("Cleanup client ref = %d", i);
107  client->Close();
108  delete client;
109  JackGlobals::fClientTable[CLIENT_NUM] = NULL;
110  }
111  }
112 
113  // Cleanup global context
114  fClientCount = 0;
115  delete fGlobals;
116  fGlobals = NULL;
117  }
118 
119  if (fClientCount++ == 0 && !fGlobals) {
120  jack_log("JackLibGlobals Init %x", fGlobals);
121  InitTime();
122  fGlobals = new JackLibGlobals();
123  }
124  }
125 
126  static void Destroy()
127  {
128  if (--fClientCount == 0 && fGlobals) {
129  jack_log("JackLibGlobals Destroy %x", fGlobals);
130  delete fGlobals;
131  fGlobals = NULL;
132  }
133  }
134 
135 };
136 
137 } // end of namespace
138 
139 #endif
140