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.

138 lines
3.3KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #ifdef WIN32
  20. #pragma warning (disable : 4786)
  21. #endif
  22. #include "JackThreadedDriver.h"
  23. #include "JackError.h"
  24. #include "JackGlobals.h"
  25. #include "JackClient.h"
  26. #include "JackEngineControl.h"
  27. #include "JackException.h"
  28. namespace Jack
  29. {
  30. JackThreadedDriver::JackThreadedDriver(JackDriverClient* driver):fThread(this)
  31. {
  32. fDriver = driver;
  33. }
  34. JackThreadedDriver::~JackThreadedDriver()
  35. {
  36. delete fDriver;
  37. }
  38. int JackThreadedDriver::Start()
  39. {
  40. jack_log("JackThreadedDriver::Start");
  41. if (fDriver->Start() < 0) {
  42. jack_error("Cannot start driver");
  43. return -1;
  44. }
  45. if (fThread.Start() < 0) {
  46. jack_error("Cannot start thread");
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. int JackThreadedDriver::Stop()
  52. {
  53. jack_log("JackThreadedDriver::Stop");
  54. switch (fThread.GetStatus()) {
  55. // Kill the thread in Init phase
  56. case JackThread::kStarting:
  57. case JackThread::kIniting:
  58. if (fThread.Kill() < 0) {
  59. jack_error("Cannot kill thread");
  60. return -1;
  61. }
  62. break;
  63. // Stop when the thread cycle is finished
  64. case JackThread::kRunning:
  65. if (fThread.Stop() < 0) {
  66. jack_error("Cannot stop thread");
  67. return -1;
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. if (fDriver->Stop() < 0) {
  74. jack_error("Cannot stop driver");
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. bool JackThreadedDriver::Execute()
  80. {
  81. return (Process() == 0);
  82. }
  83. bool JackThreadedDriver::Init()
  84. {
  85. if (fDriver->Init()) {
  86. if (fDriver->IsRealTime()) {
  87. jack_log("JackThreadedDriver::Init IsRealTime");
  88. // Will do "something" on OSX only...
  89. fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
  90. if (fThread.AcquireRealTime(GetEngineControl()->fPriority) < 0)
  91. jack_error("AcquireRealTime error");
  92. }
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. bool JackRestartThreadedDriver::Execute()
  99. {
  100. try {
  101. // Keep running even in case of error
  102. while (fThread.GetStatus() == JackThread::kRunning) {
  103. Process();
  104. }
  105. } catch (JackDriverException e) {
  106. e.PrintMessage();
  107. jack_log("Driver is restarted");
  108. fThread.SetStatus(JackThread::kIniting);
  109. fThread.DropRealTime();
  110. return Init();
  111. }
  112. return false;
  113. }
  114. } // end of namespace