summaryrefslogtreecommitdiff
path: root/common/JackLibGlobals.h
blob: f45f40a7198be02c5af9e2f4ac6518f20628b840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright (C) 2005 Grame  

This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __JackLibGlobals__
#define __JackLibGlobals__

#include "JackShmMem.h"
#include "JackEngineControl.h"
#ifdef __APPLE__
#include "JackMachPort.h"
#include <map>
#endif
#include "JackGlobals.h"
#include "JackGraphManager.h"
#include "JackTime.h"
#include <assert.h>

namespace Jack
{

class JackClient;

/*!
\brief Global library static structure: singleton kind of pattern.
*/

struct JackLibGlobals
{
    JackShmReadWritePtr<JackGraphManager> fGraphManager;	/*! Shared memory Port manager */
    JackShmReadWritePtr<JackEngineControl> fEngineControl;	/*! Shared engine control */  // transport engine has to be writable
    JackSynchro* fSynchroTable[CLIENT_NUM];					/*! Shared synchro table */
#ifdef __APPLE__
    std::map<mach_port_t, JackClient*> fClientTable;        /*! Client table */
#endif

    static long fClientCount;
    static JackLibGlobals* fGlobals;

    JackLibGlobals()
    {
        JackLog("JackLibGlobals\n");
        for (int i = 0; i < CLIENT_NUM; i++)
            fSynchroTable[i] = JackGlobals::MakeSynchro();
        fGraphManager = -1;
        fEngineControl = -1;
    }

    virtual ~JackLibGlobals()
    {
        JackLog("~JackLibGlobals\n");
        for (int i = 0; i < CLIENT_NUM; i++) {
            fSynchroTable[i]->Disconnect();
            delete fSynchroTable[i];
        }
    }

    static void Init()
    {
        if (fClientCount++ == 0 && !fGlobals) {
            JackLog("JackLibGlobals Init %x\n", fGlobals);
            JackGlobals::InitClient();
            InitTime();
            fGlobals = new JackLibGlobals();
	   }
    }

    static void Destroy()
    {
        if (--fClientCount == 0 && fGlobals) {
            JackLog("JackLibGlobals Destroy %x\n", fGlobals);
            delete fGlobals;
            fGlobals = NULL;
            JackGlobals::Destroy();
        }
    }
	
	static void CheckContext()
	{
		if (!(fClientCount > 0 && fGlobals)) {
			jack_error("Error !!! : client accessing an already desallocated library context");
		}
	}

};

} // end of namespace

#endif