Channel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Copyright (c) 2006-2016 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Channel.h"
  21. #include <map>
  22. #include <string>
  23. namespace
  24. {
  25. union uslong
  26. {
  27. unsigned long u;
  28. long i;
  29. };
  30. // target <= current, but semi-wrapsafe, one wrap, anyway
  31. inline bool past(unsigned int target, unsigned int current)
  32. {
  33. if (target > current)
  34. return false;
  35. if (target == current)
  36. return true;
  37. uslong t, c;
  38. t.u = target;
  39. c.u = current;
  40. return !(t.i < 0 && c.i > 0);
  41. }
  42. }
  43. namespace love
  44. {
  45. namespace thread
  46. {
  47. static std::map<std::string, Channel *> namedChannels;
  48. static Mutex *namedChannelMutex;
  49. Channel *Channel::getChannel(const std::string &name)
  50. {
  51. if (!namedChannelMutex)
  52. namedChannelMutex = newMutex();
  53. Lock lock(namedChannelMutex);
  54. auto it = namedChannels.find(name);
  55. if (it != namedChannels.end())
  56. {
  57. it->second->retain();
  58. return it->second;
  59. }
  60. namedChannels[name] = new Channel(name);
  61. return namedChannels[name];
  62. }
  63. Channel::Channel()
  64. : named(false)
  65. , sent(0)
  66. , received(0)
  67. {
  68. mutex = newMutex();
  69. cond = newConditional();
  70. }
  71. Channel::Channel(const std::string &name)
  72. : named(true)
  73. , name(name)
  74. , sent(0)
  75. , received(0)
  76. {
  77. mutex = newMutex();
  78. cond = newConditional();
  79. }
  80. Channel::~Channel()
  81. {
  82. delete mutex;
  83. delete cond;
  84. if (named)
  85. {
  86. Lock l(namedChannelMutex);
  87. namedChannels.erase(name);
  88. }
  89. }
  90. unsigned long Channel::push(const Variant &var)
  91. {
  92. Lock l(mutex);
  93. // Keep a reference to ourselves
  94. // if we're non-empty and named.
  95. if (named && queue.empty())
  96. retain();
  97. queue.push(var);
  98. cond->broadcast();
  99. return ++sent;
  100. }
  101. void Channel::supply(const Variant &var)
  102. {
  103. Lock l(mutex);
  104. unsigned long id = push(var);
  105. while (!past(id, received))
  106. cond->wait(mutex);
  107. }
  108. bool Channel::pop(Variant *var)
  109. {
  110. Lock l(mutex);
  111. if (queue.empty())
  112. return false;
  113. *var = queue.front();
  114. queue.pop();
  115. received++;
  116. cond->broadcast();
  117. // Release our reference to ourselves
  118. // if we're empty and named.
  119. if (named && queue.empty())
  120. release();
  121. return true;
  122. }
  123. void Channel::demand(Variant *var)
  124. {
  125. Lock l(mutex);
  126. while (!pop(var))
  127. cond->wait(mutex);
  128. }
  129. bool Channel::peek(Variant *var)
  130. {
  131. Lock l(mutex);
  132. if (queue.empty())
  133. return false;
  134. *var = queue.front();
  135. return true;
  136. }
  137. int Channel::getCount()
  138. {
  139. Lock l(mutex);
  140. return (int) queue.size();
  141. }
  142. void Channel::clear()
  143. {
  144. Lock l(mutex);
  145. // We're already empty.
  146. if (queue.empty())
  147. return;
  148. while (!queue.empty())
  149. queue.pop();
  150. // Finish all the supply waits
  151. received = sent;
  152. cond->broadcast();
  153. // Once again, release our own
  154. // reference if we're named.
  155. if (named)
  156. release();
  157. }
  158. void Channel::lockMutex()
  159. {
  160. mutex->lock();
  161. }
  162. void Channel::unlockMutex()
  163. {
  164. mutex->unlock();
  165. }
  166. } // thread
  167. } // love