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.

139 lines
3.8KB

  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. namespace Jack
  29. {
  30. class JackClient;
  31. /*!
  32. \brief Global library static structure: singleton kind of pattern.
  33. */
  34. struct JackLibGlobals
  35. {
  36. JackShmReadWritePtr<JackGraphManager> fGraphManager; /*! Shared memory Port manager */
  37. JackShmReadWritePtr<JackEngineControl> fEngineControl; /*! Shared engine control */ // transport engine has to be writable
  38. JackSynchro fSynchroTable[CLIENT_NUM]; /*! Shared synchro table */
  39. sigset_t fProcessSignals;
  40. static int fClientCount;
  41. static JackLibGlobals* fGlobals;
  42. JackLibGlobals()
  43. {
  44. jack_log("JackLibGlobals");
  45. JackMessageBuffer::Create();
  46. fGraphManager = -1;
  47. fEngineControl = -1;
  48. // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
  49. #ifdef WIN32
  50. // TODO
  51. #else
  52. sigset_t signals;
  53. sigemptyset(&signals);
  54. sigaddset(&signals, SIGPIPE);
  55. sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
  56. #endif
  57. }
  58. ~JackLibGlobals()
  59. {
  60. jack_log("~JackLibGlobals");
  61. for (int i = 0; i < CLIENT_NUM; i++) {
  62. fSynchroTable[i].Disconnect();
  63. }
  64. JackMessageBuffer::Destroy();
  65. // Restore old signal mask
  66. #ifdef WIN32
  67. // TODO
  68. #else
  69. sigprocmask(SIG_BLOCK, &fProcessSignals, 0);
  70. #endif
  71. }
  72. static void Init()
  73. {
  74. if (!JackGlobals::fServerRunning && fClientCount > 0) {
  75. // Cleanup remaining clients
  76. jack_error("Jack server was closed but clients are still allocated, cleanup...");
  77. for (int i = 0; i < CLIENT_NUM; i++) {
  78. JackClient* client = JackGlobals::fClientTable[i];
  79. if (client) {
  80. jack_error("Cleanup client ref = %d", i);
  81. client->Close();
  82. delete client;
  83. JackGlobals::fClientTable[CLIENT_NUM] = NULL;
  84. }
  85. }
  86. // Cleanup global context
  87. fClientCount = 0;
  88. delete fGlobals;
  89. fGlobals = NULL;
  90. }
  91. if (fClientCount++ == 0 && !fGlobals) {
  92. jack_log("JackLibGlobals Init %x", fGlobals);
  93. InitTime();
  94. fGlobals = new JackLibGlobals();
  95. }
  96. }
  97. static void Destroy()
  98. {
  99. if (--fClientCount == 0 && fGlobals) {
  100. jack_log("JackLibGlobals Destroy %x", fGlobals);
  101. delete fGlobals;
  102. fGlobals = NULL;
  103. }
  104. }
  105. static void CheckContext()
  106. {
  107. if (!(fClientCount > 0 && fGlobals)) {
  108. jack_error("Error !!! : client accessing an already desallocated library context");
  109. }
  110. }
  111. };
  112. } // end of namespace
  113. #endif