jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.9KB

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