Audio plugin host https://kx.studio/carla
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.

159 lines
3.6KB

  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "RtList.hpp"
  18. #include "CarlaString.hpp"
  19. #include "CarlaMutex.hpp"
  20. const unsigned short MIN_RT_EVENTS = 152;
  21. const unsigned short MAX_RT_EVENTS = 512;
  22. struct MyData {
  23. CarlaString str;
  24. int idStr;
  25. MyData()
  26. : idStr(-1) {}
  27. MyData(int id)
  28. : str(id),
  29. idStr(id) {}
  30. MyData(MyData&) = delete;
  31. MyData(const MyData&) = delete;
  32. };
  33. struct PostRtEvents {
  34. CarlaMutex mutex;
  35. RtList<MyData>::Pool dataPool;
  36. RtList<MyData> data;
  37. RtList<MyData> dataPendingRT;
  38. PostRtEvents()
  39. : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS),
  40. data(&dataPool),
  41. dataPendingRT(&dataPool) {}
  42. ~PostRtEvents()
  43. {
  44. clear();
  45. }
  46. void appendRT(const MyData& event)
  47. {
  48. dataPendingRT.append(event);
  49. }
  50. void clear()
  51. {
  52. mutex.lock();
  53. data.clear();
  54. dataPendingRT.clear();
  55. mutex.unlock();
  56. }
  57. void trySplice()
  58. {
  59. if (mutex.tryLock())
  60. {
  61. dataPendingRT.splice(data, true);
  62. mutex.unlock();
  63. }
  64. }
  65. //void appendNonRT(const PluginPostRtEvent& event)
  66. //{
  67. // data.append_sleepy(event);
  68. //}
  69. } postRtEvents;
  70. void run4Tests()
  71. {
  72. unsigned short k = 0;
  73. MyData allMyData[MAX_RT_EVENTS];
  74. // Make a safe copy of events while clearing them
  75. postRtEvents.mutex.lock();
  76. while (! postRtEvents.data.isEmpty())
  77. {
  78. MyData& my = postRtEvents.data.getFirst(true);
  79. allMyData[k++] = my;
  80. //std::memcpy(&allMyData[i++], &my, sizeof(MyData));
  81. }
  82. postRtEvents.mutex.unlock();
  83. printf("Post-Rt Event Count: %i\n", k);
  84. assert(k == 4);
  85. // data should be empty now
  86. assert(postRtEvents.data.count() == 0);
  87. assert(postRtEvents.data.isEmpty());
  88. assert(postRtEvents.dataPendingRT.count() == 0);
  89. assert(postRtEvents.dataPendingRT.isEmpty());
  90. // Handle events now
  91. for (unsigned short i=0; i < k; i++)
  92. {
  93. const MyData& my = allMyData[i];
  94. printf("Got data: %i %s\n", my.idStr, (const char*)my.str);
  95. }
  96. }
  97. int main()
  98. {
  99. MyData m1(1);
  100. MyData m2(2);
  101. MyData m3(3);
  102. MyData m4(4);
  103. // start
  104. assert(postRtEvents.data.count() == 0);
  105. assert(postRtEvents.data.isEmpty());
  106. assert(postRtEvents.dataPendingRT.count() == 0);
  107. assert(postRtEvents.dataPendingRT.isEmpty());
  108. // single append
  109. postRtEvents.appendRT(m1);
  110. postRtEvents.trySplice();
  111. assert(postRtEvents.data.count() == 1);
  112. assert(postRtEvents.dataPendingRT.count() == 0);
  113. // +3 appends
  114. postRtEvents.appendRT(m2);
  115. postRtEvents.appendRT(m4);
  116. postRtEvents.appendRT(m3);
  117. postRtEvents.trySplice();
  118. assert(postRtEvents.data.count() == 4);
  119. assert(postRtEvents.dataPendingRT.count() == 0);
  120. run4Tests();
  121. // reset
  122. postRtEvents.clear();
  123. assert(postRtEvents.data.count() == 0);
  124. assert(postRtEvents.data.isEmpty());
  125. assert(postRtEvents.dataPendingRT.count() == 0);
  126. assert(postRtEvents.dataPendingRT.isEmpty());
  127. return 0;
  128. }