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.

RtLinkedList.cpp 6.9KB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013-2014 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 doc/GPL.txt file.
  16. */
  17. #include "RtLinkedList.hpp"
  18. #include "CarlaString.hpp"
  19. #include "CarlaMutex.hpp"
  20. const unsigned short MIN_RT_EVENTS = 5;
  21. const unsigned short MAX_RT_EVENTS = 12;
  22. struct MyData {
  23. char str[234];
  24. int id;
  25. };
  26. struct PostRtEvents {
  27. CarlaMutex mutex;
  28. RtLinkedList<MyData>::Pool dataPool;
  29. RtLinkedList<MyData> data;
  30. RtLinkedList<MyData> dataPendingRT;
  31. PostRtEvents() noexcept
  32. : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS),
  33. data(dataPool),
  34. dataPendingRT(dataPool) {}
  35. ~PostRtEvents() noexcept
  36. {
  37. clear();
  38. }
  39. void appendRT(const MyData& event) noexcept
  40. {
  41. dataPendingRT.append(event);
  42. }
  43. void clear() noexcept
  44. {
  45. mutex.lock();
  46. data.clear();
  47. dataPendingRT.clear();
  48. mutex.unlock();
  49. }
  50. void trySplice() noexcept
  51. {
  52. if (mutex.tryLock())
  53. {
  54. dataPendingRT.moveTo(data, true);
  55. mutex.unlock();
  56. }
  57. }
  58. } postRtEvents;
  59. void run5Tests();
  60. void run5Tests()
  61. {
  62. ushort k = 0;
  63. MyData allMyData[MAX_RT_EVENTS];
  64. // Make a safe copy of events while clearing them
  65. postRtEvents.mutex.lock();
  66. while (! postRtEvents.data.isEmpty())
  67. {
  68. static MyData kFallback = { { '\0' }, 0 };
  69. const MyData& my(postRtEvents.data.getFirst(kFallback, true));
  70. allMyData[k++] = my;
  71. }
  72. postRtEvents.mutex.unlock();
  73. carla_stdout("Post-Rt Event Count: %i", k);
  74. assert(k == 5);
  75. // data should be empty now
  76. assert(postRtEvents.data.count() == 0);
  77. assert(postRtEvents.data.isEmpty());
  78. assert(postRtEvents.dataPendingRT.count() == 0);
  79. assert(postRtEvents.dataPendingRT.isEmpty());
  80. // Handle events now
  81. for (ushort i=0; i < k; ++i)
  82. {
  83. const MyData& my(allMyData[i]);
  84. carla_stdout("Got data: %i %s", my.id, my.str);
  85. }
  86. // append events past minimum size
  87. MyData dummyData = { { '\0' }, 0 };
  88. // 5 initial go ok
  89. assert(postRtEvents.dataPendingRT.append(dummyData));
  90. assert(postRtEvents.dataPendingRT.append(dummyData));
  91. assert(postRtEvents.dataPendingRT.append(dummyData));
  92. assert(postRtEvents.dataPendingRT.append(dummyData));
  93. assert(postRtEvents.dataPendingRT.append(dummyData));
  94. // afterwards it fails
  95. assert(! postRtEvents.dataPendingRT.append(dummyData));
  96. assert(! postRtEvents.dataPendingRT.append(dummyData));
  97. carla_stdout("here %i", __LINE__);
  98. // adding sleepy works
  99. assert(postRtEvents.dataPendingRT.append_sleepy(dummyData));
  100. carla_stdout("here %i", __LINE__);
  101. // now atomic works too, size was increased
  102. assert(postRtEvents.dataPendingRT.append(dummyData));
  103. assert(postRtEvents.dataPendingRT.append(dummyData));
  104. assert(postRtEvents.dataPendingRT.append(dummyData));
  105. assert(postRtEvents.dataPendingRT.append(dummyData));
  106. carla_stdout("here %i", __LINE__);
  107. // fails here now
  108. assert(! postRtEvents.dataPendingRT.append(dummyData));
  109. carla_stdout("here %i", __LINE__);
  110. // adding sleepy still works
  111. assert(postRtEvents.dataPendingRT.append_sleepy(dummyData));
  112. carla_stdout("here %i", __LINE__);
  113. // now atomic works for 1 more
  114. assert(postRtEvents.dataPendingRT.append(dummyData));
  115. assert(! postRtEvents.dataPendingRT.append(dummyData));
  116. carla_stdout("here %i", __LINE__);
  117. // and adding sleepy no longer works
  118. assert(! postRtEvents.dataPendingRT.append_sleepy(dummyData));
  119. carla_stdout("here %i", __LINE__);
  120. // cleanup
  121. postRtEvents.trySplice();
  122. postRtEvents.clear();
  123. }
  124. int main()
  125. {
  126. MyData m1; m1.id = 1; std::strcpy(m1.str, "1");
  127. MyData m2; m2.id = 2; std::strcpy(m2.str, "2");
  128. MyData m3; m3.id = 3; std::strcpy(m3.str, "3");
  129. MyData m4; m4.id = 4; std::strcpy(m4.str, "4");
  130. MyData m5; m5.id = 5; std::strcpy(m5.str, "5");
  131. // start
  132. assert(postRtEvents.data.count() == 0);
  133. assert(postRtEvents.data.isEmpty());
  134. assert(postRtEvents.dataPendingRT.count() == 0);
  135. assert(postRtEvents.dataPendingRT.isEmpty());
  136. // single append
  137. postRtEvents.appendRT(m1);
  138. assert(postRtEvents.data.count() == 0);
  139. assert(postRtEvents.dataPendingRT.count() == 1);
  140. postRtEvents.trySplice();
  141. assert(postRtEvents.data.count() == 1);
  142. assert(postRtEvents.dataPendingRT.count() == 0);
  143. // +3 appends
  144. postRtEvents.appendRT(m2);
  145. postRtEvents.appendRT(m4);
  146. postRtEvents.appendRT(m3);
  147. assert(postRtEvents.data.count() == 1);
  148. assert(postRtEvents.dataPendingRT.count() == 3);
  149. postRtEvents.trySplice();
  150. assert(postRtEvents.data.count() == 4);
  151. assert(postRtEvents.dataPendingRT.count() == 0);
  152. for (RtLinkedList<MyData>::Itenerator it = postRtEvents.data.begin2(); it.valid(); it.next())
  153. {
  154. static const MyData kFallback = { { '\0' }, 0 };
  155. const MyData& my(it.getValue(kFallback));
  156. carla_stdout("FOR DATA!!!: %i %s", my.id, my.str);
  157. if (my.id == 2)
  158. {
  159. // +1 append
  160. postRtEvents.dataPendingRT.insertAt(m5, it);
  161. assert(postRtEvents.data.count() == 4);
  162. assert(postRtEvents.dataPendingRT.count() == 1);
  163. }
  164. }
  165. #if 0
  166. for (const MyData& my : postRtEvents.data)
  167. carla_stdout("FOR DATA!!! NEW AUTO Itenerator!!!: %i %s", my.id, my.str);
  168. #endif
  169. postRtEvents.trySplice();
  170. assert(postRtEvents.data.count() == 5);
  171. assert(postRtEvents.dataPendingRT.count() == 0);
  172. run5Tests();
  173. // reset
  174. postRtEvents.clear();
  175. assert(postRtEvents.data.count() == 0);
  176. assert(postRtEvents.data.isEmpty());
  177. assert(postRtEvents.dataPendingRT.count() == 0);
  178. assert(postRtEvents.dataPendingRT.isEmpty());
  179. // test non-rt
  180. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  181. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  182. LinkedList<uint32_t> evIns, evOuts;
  183. evIns.append(CARLA_EVENT_DATA_ATOM);
  184. evOuts.append(CARLA_EVENT_DATA_ATOM);
  185. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  186. if (evIns.count() > 0)
  187. {
  188. for (size_t j=0, count=evIns.count(); j < count; ++j)
  189. {
  190. const uint32_t& type(evIns.getAt(j, 0));
  191. if (type == CARLA_EVENT_DATA_ATOM)
  192. pass();
  193. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  194. pass();
  195. }
  196. }
  197. evIns.clear();
  198. evOuts.clear();
  199. return 0;
  200. }