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 5.0KB

11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifdef PROPER_CPP11_SUPPORT
  31. MyData(MyData&) = delete;
  32. MyData(const MyData&) = delete;
  33. #endif
  34. };
  35. struct PostRtEvents {
  36. CarlaMutex mutex;
  37. RtList<MyData>::Pool dataPool;
  38. RtList<MyData> data;
  39. RtList<MyData> dataPendingRT;
  40. PostRtEvents()
  41. : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS),
  42. data(dataPool),
  43. dataPendingRT(dataPool) {}
  44. ~PostRtEvents()
  45. {
  46. clear();
  47. }
  48. void appendRT(const MyData& event)
  49. {
  50. dataPendingRT.append(event);
  51. }
  52. void clear()
  53. {
  54. mutex.lock();
  55. data.clear();
  56. dataPendingRT.clear();
  57. mutex.unlock();
  58. }
  59. void trySplice()
  60. {
  61. if (mutex.tryLock())
  62. {
  63. dataPendingRT.spliceAppend(data);
  64. mutex.unlock();
  65. }
  66. }
  67. //void appendNonRT(const PluginPostRtEvent& event)
  68. //{
  69. // data.append_sleepy(event);
  70. //}
  71. } postRtEvents;
  72. void run5Tests()
  73. {
  74. unsigned short k = 0;
  75. MyData allMyData[MAX_RT_EVENTS];
  76. // Make a safe copy of events while clearing them
  77. postRtEvents.mutex.lock();
  78. while (! postRtEvents.data.isEmpty())
  79. {
  80. MyData& my = postRtEvents.data.getFirst(true);
  81. allMyData[k++] = my;
  82. //std::memcpy(&allMyData[i++], &my, sizeof(MyData));
  83. }
  84. postRtEvents.mutex.unlock();
  85. printf("Post-Rt Event Count: %i\n", k);
  86. assert(k == 5);
  87. // data should be empty now
  88. assert(postRtEvents.data.count() == 0);
  89. assert(postRtEvents.data.isEmpty());
  90. assert(postRtEvents.dataPendingRT.count() == 0);
  91. assert(postRtEvents.dataPendingRT.isEmpty());
  92. // Handle events now
  93. for (unsigned short i=0; i < k; ++i)
  94. {
  95. const MyData& my = allMyData[i];
  96. printf("Got data: %i %s\n", my.idStr, (const char*)my.str);
  97. }
  98. }
  99. int main()
  100. {
  101. MyData m1(1);
  102. MyData m2(2);
  103. MyData m3(3);
  104. MyData m4(4);
  105. MyData m5(5);
  106. // start
  107. assert(postRtEvents.data.count() == 0);
  108. assert(postRtEvents.data.isEmpty());
  109. assert(postRtEvents.dataPendingRT.count() == 0);
  110. assert(postRtEvents.dataPendingRT.isEmpty());
  111. // single append
  112. postRtEvents.appendRT(m1);
  113. postRtEvents.trySplice();
  114. assert(postRtEvents.data.count() == 1);
  115. assert(postRtEvents.dataPendingRT.count() == 0);
  116. // +3 appends
  117. postRtEvents.appendRT(m2);
  118. postRtEvents.appendRT(m4);
  119. postRtEvents.appendRT(m3);
  120. assert(postRtEvents.data.count() == 1);
  121. assert(postRtEvents.dataPendingRT.count() == 3);
  122. postRtEvents.trySplice();
  123. assert(postRtEvents.data.count() == 4);
  124. assert(postRtEvents.dataPendingRT.count() == 0);
  125. for (auto it = postRtEvents.data.begin(); it.valid(); it.next())
  126. {
  127. MyData& my(*it);
  128. printf("FOR DATA!!!: %i %s\n", my.idStr, (const char*)my.str);
  129. if (my.idStr == 1)
  130. {
  131. // +1 append at
  132. postRtEvents.dataPendingRT.insertAt(m5, it);
  133. assert(postRtEvents.data.count() == 4);
  134. assert(postRtEvents.dataPendingRT.count() == 1);
  135. postRtEvents.trySplice();
  136. assert(postRtEvents.data.count() == 5);
  137. assert(postRtEvents.dataPendingRT.count() == 0);
  138. }
  139. }
  140. run5Tests();
  141. // reset
  142. postRtEvents.clear();
  143. assert(postRtEvents.data.count() == 0);
  144. assert(postRtEvents.data.isEmpty());
  145. assert(postRtEvents.dataPendingRT.count() == 0);
  146. assert(postRtEvents.dataPendingRT.isEmpty());
  147. // test non-rt
  148. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  149. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  150. NonRtList<uint32_t> evIns, evOuts;
  151. evIns.append(CARLA_EVENT_DATA_ATOM);
  152. evOuts.append(CARLA_EVENT_DATA_ATOM);
  153. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  154. if (evIns.count() > 0)
  155. {
  156. const size_t count(evIns.count());
  157. for (uint32_t j=0; j < count; ++j)
  158. {
  159. const uint32_t& type(evIns.getAt(j));
  160. if (type == CARLA_EVENT_DATA_ATOM)
  161. pass();
  162. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  163. pass();
  164. }
  165. }
  166. evIns.clear();
  167. evOuts.clear();
  168. return 0;
  169. }