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

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = 10;
  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 fallback = { { '\0' }, 0 };
  69. const MyData& my(postRtEvents.data.getFirst(fallback, 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. }
  87. int main()
  88. {
  89. MyData m1; m1.id = 1; std::strcpy(m1.str, "1");
  90. MyData m2; m2.id = 2; std::strcpy(m2.str, "2");
  91. MyData m3; m3.id = 3; std::strcpy(m3.str, "3");
  92. MyData m4; m4.id = 4; std::strcpy(m4.str, "4");
  93. MyData m5; m5.id = 5; std::strcpy(m5.str, "5");
  94. // start
  95. assert(postRtEvents.data.count() == 0);
  96. assert(postRtEvents.data.isEmpty());
  97. assert(postRtEvents.dataPendingRT.count() == 0);
  98. assert(postRtEvents.dataPendingRT.isEmpty());
  99. // single append
  100. postRtEvents.appendRT(m1);
  101. assert(postRtEvents.data.count() == 0);
  102. assert(postRtEvents.dataPendingRT.count() == 1);
  103. postRtEvents.trySplice();
  104. assert(postRtEvents.data.count() == 1);
  105. assert(postRtEvents.dataPendingRT.count() == 0);
  106. // +3 appends
  107. postRtEvents.appendRT(m2);
  108. postRtEvents.appendRT(m4);
  109. postRtEvents.appendRT(m3);
  110. assert(postRtEvents.data.count() == 1);
  111. assert(postRtEvents.dataPendingRT.count() == 3);
  112. postRtEvents.trySplice();
  113. assert(postRtEvents.data.count() == 4);
  114. assert(postRtEvents.dataPendingRT.count() == 0);
  115. for (RtLinkedList<MyData>::Itenerator it = postRtEvents.data.begin2(); it.valid(); it.next())
  116. {
  117. const MyData& my(it.getValue());
  118. carla_stdout("FOR DATA!!!: %i %s", my.id, my.str);
  119. if (my.id == 2)
  120. {
  121. // +1 append
  122. postRtEvents.dataPendingRT.insertAt(m5, it);
  123. assert(postRtEvents.data.count() == 4);
  124. assert(postRtEvents.dataPendingRT.count() == 1);
  125. }
  126. }
  127. for (const MyData& my : postRtEvents.data)
  128. carla_stdout("FOR DATA!!! NEW AUTO Itenerator!!!: %i %s", my.id, my.str);
  129. postRtEvents.trySplice();
  130. assert(postRtEvents.data.count() == 5);
  131. assert(postRtEvents.dataPendingRT.count() == 0);
  132. run5Tests();
  133. // reset
  134. postRtEvents.clear();
  135. assert(postRtEvents.data.count() == 0);
  136. assert(postRtEvents.data.isEmpty());
  137. assert(postRtEvents.dataPendingRT.count() == 0);
  138. assert(postRtEvents.dataPendingRT.isEmpty());
  139. // test non-rt
  140. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  141. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  142. LinkedList<uint32_t> evIns, evOuts;
  143. evIns.append(CARLA_EVENT_DATA_ATOM);
  144. evOuts.append(CARLA_EVENT_DATA_ATOM);
  145. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  146. if (evIns.count() > 0)
  147. {
  148. for (size_t j=0, count=evIns.count(); j < count; ++j)
  149. {
  150. const uint32_t& type(evIns.getAt(j, 0));
  151. if (type == CARLA_EVENT_DATA_ATOM)
  152. pass();
  153. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  154. pass();
  155. }
  156. }
  157. evIns.clear();
  158. evOuts.clear();
  159. return 0;
  160. }