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.

143 lines
3.7KB

  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. sigset_t fProcessSignals;
  48. static int fClientCount;
  49. static JackLibGlobals* fGlobals;
  50. JackLibGlobals()
  51. {
  52. jack_log("JackLibGlobals");
  53. if (!JackMessageBuffer::Create()) {
  54. jack_error("Cannot create message buffer");
  55. }
  56. fGraphManager = -1;
  57. fEngineControl = -1;
  58. // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
  59. #ifdef WIN32
  60. // TODO
  61. #else
  62. sigset_t signals;
  63. sigemptyset(&signals);
  64. sigaddset(&signals, SIGPIPE);
  65. sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
  66. #endif
  67. }
  68. ~JackLibGlobals()
  69. {
  70. jack_log("~JackLibGlobals");
  71. for (int i = 0; i < CLIENT_NUM; i++) {
  72. fSynchroTable[i].Disconnect();
  73. }
  74. JackMessageBuffer::Destroy();
  75. // Restore old signal mask
  76. #ifdef WIN32
  77. // TODO
  78. #else
  79. sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
  80. #endif
  81. }
  82. static void Init()
  83. {
  84. if (!JackGlobals::fServerRunning && fClientCount > 0) {
  85. // Cleanup remaining clients
  86. jack_error("Jack server was closed but clients are still allocated, cleanup...");
  87. for (int i = 0; i < CLIENT_NUM; i++) {
  88. JackClient* client = JackGlobals::fClientTable[i];
  89. if (client) {
  90. jack_error("Cleanup client ref = %d", i);
  91. client->Close();
  92. delete client;
  93. }
  94. }
  95. // Cleanup global context
  96. fClientCount = 0;
  97. delete fGlobals;
  98. fGlobals = NULL;
  99. }
  100. if (fClientCount++ == 0 && !fGlobals) {
  101. jack_log("JackLibGlobals Init %x", fGlobals);
  102. InitTime();
  103. fGlobals = new JackLibGlobals();
  104. }
  105. }
  106. static void Destroy()
  107. {
  108. if (--fClientCount == 0 && fGlobals) {
  109. jack_log("JackLibGlobals Destroy %x", fGlobals);
  110. EndTime();
  111. delete fGlobals;
  112. fGlobals = NULL;
  113. }
  114. }
  115. };
  116. } // end of namespace
  117. #endif