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.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):
  31. fThread(this)
  32. {
  33. fDriver = driver;
  34. }
  35. JackThreadedDriver::~JackThreadedDriver()
  36. {
  37. delete fDriver;
  38. }
  39. int JackThreadedDriver::Start()
  40. {
  41. jack_log("JackThreadedDriver::Start");
  42. if (fDriver->Start() < 0) {
  43. jack_error("Cannot start driver");
  44. return -1;
  45. }
  46. if (fThread.Start() < 0) {
  47. jack_error("Cannot start thread");
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. int JackThreadedDriver::Stop()
  53. {
  54. jack_log("JackThreadedDriver::Stop");
  55. switch (fThread.GetStatus()) {
  56. // Kill the thread in Init phase
  57. case JackThread::kStarting:
  58. case JackThread::kIniting:
  59. if (fThread.Kill() < 0) {
  60. jack_error("Cannot kill thread");
  61. return -1;
  62. }
  63. break;
  64. // Stop when the thread cycle is finished
  65. case JackThread::kRunning:
  66. if (fThread.Stop() < 0) {
  67. jack_error("Cannot stop thread");
  68. return -1;
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. if (fDriver->Stop() < 0) {
  75. jack_error("Cannot stop driver");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. bool JackThreadedDriver::Execute()
  81. {
  82. return (Process() == 0);
  83. }
  84. bool JackThreadedDriver::Init()
  85. {
  86. if (fDriver->Init()) {
  87. if (fDriver->IsRealTime()) {
  88. jack_log("JackThreadedDriver::Init IsRealTime");
  89. // Will do "something" on OSX only...
  90. fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
  91. if (fThread.AcquireRealTime(GetEngineControl()->fPriority) < 0)
  92. jack_error("AcquireRealTime error");
  93. }
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. bool JackRestartThreadedDriver::Execute()
  100. {
  101. try {
  102. // Keep running even in case of error
  103. while (fThread.GetStatus() == JackThread::kRunning) {
  104. Process();
  105. }
  106. } catch (JackDriverException e) {
  107. e.PrintMessage();
  108. jack_log("Driver is restarted");
  109. fThread.SetStatus(JackThread::kIniting);
  110. fThread.DropRealTime();
  111. return Init();
  112. }
  113. return false;
  114. }
  115. } // end of namespace