Jack2  1.9.11-RC1
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 
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  if (!JackMessageBuffer::Create()) {
67  jack_error("Cannot create message buffer");
68  }
69  fGraphManager = -1;
70  fEngineControl = -1;
71 
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
81  }
82 
84  {
85  jack_log("~JackLibGlobals");
86  for (int i = 0; i < CLIENT_NUM; i++) {
87  fSynchroTable[i].Disconnect();
88  }
89  JackMessageBuffer::Destroy();
90 
91  // Restore old signal mask
92  #ifdef WIN32
93  // TODO
94  #else
95  sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
96  #endif
97  }
98 
99  static void Init()
100  {
101  if (!JackGlobals::fServerRunning && fClientCount > 0) {
102 
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  }
112  }
113 
114  // Cleanup global context
115  fClientCount = 0;
116  delete fGlobals;
117  fGlobals = NULL;
118  }
119 
120  if (fClientCount++ == 0 && !fGlobals) {
121  jack_log("JackLibGlobals Init %x", fGlobals);
122  InitTime();
123  fGlobals = new JackLibGlobals();
124  }
125  }
126 
127  static void Destroy()
128  {
129  if (--fClientCount == 0 && fGlobals) {
130  jack_log("JackLibGlobals Destroy %x", fGlobals);
131  EndTime();
132  delete fGlobals;
133  fGlobals = NULL;
134  }
135  }
136 
137 };
138 
139 } // end of namespace
140 
141 #endif
142 
Inter process synchronization using POSIX semaphore.
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
Global library static structure: singleton kind of pattern.
Pointer on shared memory segment in the client side.
Definition: JackShmMem.h:148
JackShmReadWritePtr< JackEngineControl > fEngineControl
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
JackSynchro fSynchroTable[CLIENT_NUM]
The base class for clients: share part of the implementation for JackInternalClient and JackLibClient...
Definition: JackClient.h:47