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.

RtList.cpp 4.3KB

11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.spliceAppend(data, true);
  62. mutex.unlock();
  63. }
  64. }
  65. //void appendNonRT(const PluginPostRtEvent& event)
  66. //{
  67. // data.append_sleepy(event);
  68. //}
  69. } postRtEvents;
  70. void run5Tests()
  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 == 5);
  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. MyData m5(5);
  104. // start
  105. assert(postRtEvents.data.count() == 0);
  106. assert(postRtEvents.data.isEmpty());
  107. assert(postRtEvents.dataPendingRT.count() == 0);
  108. assert(postRtEvents.dataPendingRT.isEmpty());
  109. // single append
  110. postRtEvents.appendRT(m1);
  111. postRtEvents.trySplice();
  112. assert(postRtEvents.data.count() == 1);
  113. assert(postRtEvents.dataPendingRT.count() == 0);
  114. // +3 appends
  115. postRtEvents.appendRT(m2);
  116. postRtEvents.appendRT(m4);
  117. postRtEvents.appendRT(m3);
  118. assert(postRtEvents.data.count() == 1);
  119. assert(postRtEvents.dataPendingRT.count() == 3);
  120. postRtEvents.trySplice();
  121. assert(postRtEvents.data.count() == 4);
  122. assert(postRtEvents.dataPendingRT.count() == 0);
  123. for (auto it = postRtEvents.data.begin(); it.valid(); it.next())
  124. {
  125. MyData& my = *it;
  126. printf("FOR DATA!!!: %i %s\n", my.idStr, (const char*)my.str);
  127. if (my.idStr == 1)
  128. {
  129. // +1 append at
  130. postRtEvents.dataPendingRT.insertAt(m5, it);
  131. assert(postRtEvents.data.count() == 4);
  132. assert(postRtEvents.dataPendingRT.count() == 1);
  133. postRtEvents.trySplice();
  134. assert(postRtEvents.data.count() == 5);
  135. assert(postRtEvents.dataPendingRT.count() == 0);
  136. }
  137. }
  138. run5Tests();
  139. // reset
  140. postRtEvents.clear();
  141. assert(postRtEvents.data.count() == 0);
  142. assert(postRtEvents.data.isEmpty());
  143. assert(postRtEvents.dataPendingRT.count() == 0);
  144. assert(postRtEvents.dataPendingRT.isEmpty());
  145. return 0;
  146. }