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.

179 lines
5.0KB

  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. class JackLibGlobals : public JackGlobals
  43. {
  44. /* This object is managed by JackGlobalsManager */
  45. friend class JackGlobalsManager;
  46. private:
  47. /* is called when first context for specific server is created */
  48. JackLibGlobals(const std::string &server_name)
  49. : JackGlobals(server_name)
  50. , fMetadata(server_name.c_str())
  51. {
  52. fGraphManager = -1;
  53. fEngineControl = -1;
  54. fClientCount = 0;
  55. InitTime();
  56. }
  57. /* is called when last context for specific server is removed */
  58. ~JackLibGlobals() override
  59. {
  60. EndTime();
  61. for (int i = 0; i < CLIENT_NUM; i++) {
  62. fSynchroTable[i].Disconnect();
  63. }
  64. }
  65. public:
  66. JackShmReadWritePtr<JackGraphManager> fGraphManager; /*! Shared memory Port manager */
  67. JackShmReadWritePtr<JackEngineControl> fEngineControl; /*! Shared engine control */ // transport engine has to be writable
  68. JackSynchro fSynchroTable[CLIENT_NUM]; /*! Shared synchro table */
  69. JackMetadata fMetadata; /*! Shared metadata base */
  70. sigset_t fProcessSignals;
  71. int fClientCount;
  72. /* is called each time a context for specific server is added */
  73. bool AddContext(const uint32_t &context_num, const std::string &server_name) override
  74. {
  75. if (!fServerRunning && fClientCount > 0) {
  76. // Cleanup remaining clients
  77. jack_error("Jack server was closed but clients are still allocated, cleanup...");
  78. for (int i = 0; i < CLIENT_NUM; i++) {
  79. JackClient* client = fClientTable[i];
  80. if (client) {
  81. jack_error("Cleanup client ref = %d", i);
  82. client->Close();
  83. delete client;
  84. }
  85. }
  86. // run destructors for all included objects, keeping the behaviour to previous revisions of JackGlobals source
  87. this->~JackLibGlobals();
  88. // construct in place
  89. new(this) JackLibGlobals(server_name);
  90. }
  91. fClientCount++;
  92. if (context_num == 0 && !JackMessageBuffer::Create()) {
  93. jack_error("Cannot create message buffer");
  94. }
  95. // Filter SIGPIPE to avoid having client get a SIGPIPE when trying to access a died server.
  96. #ifdef WIN32
  97. // TODO
  98. #else
  99. sigset_t signals;
  100. sigemptyset(&signals);
  101. sigaddset(&signals, SIGPIPE);
  102. sigprocmask(SIG_BLOCK, &signals, &fProcessSignals);
  103. #endif
  104. return true;
  105. }
  106. /* is called each time a context for specific server is removed */
  107. bool DelContext(const uint32_t &context_num) override
  108. {
  109. #ifdef WIN32
  110. // TODO
  111. #else
  112. sigprocmask(SIG_BLOCK, &fProcessSignals, nullptr);
  113. #endif
  114. if (context_num == 0 && !JackMessageBuffer::Destroy()) {
  115. jack_error("Cannot destroy message buffer");
  116. }
  117. return --fClientCount == 0;
  118. }
  119. JackGraphManager *GetGraphManager() override
  120. {
  121. return fGraphManager;
  122. }
  123. JackEngineControl *GetEngineControl() override
  124. {
  125. return fEngineControl;
  126. }
  127. JackSynchro *GetSynchroTable() override
  128. {
  129. return fSynchroTable;
  130. }
  131. /* must not be used in JackLibGlobals */
  132. JackServer *GetServer() override
  133. {
  134. assert(false);
  135. return nullptr;
  136. }
  137. JackMetadata* GetMetadata()
  138. {
  139. return &fMetadata;
  140. }
  141. };
  142. } // end of namespace
  143. #endif