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.

140 lines
3.4KB

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