Channel.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 love
  24. {
  25. namespace thread
  26. {
  27. static std::map<std::string, Channel *> namedChannels;
  28. static Mutex *namedChannelMutex;
  29. Channel *Channel::getChannel(const std::string &name)
  30. {
  31. if (!namedChannelMutex)
  32. namedChannelMutex = newMutex();
  33. Lock lock(namedChannelMutex);
  34. auto it = namedChannels.find(name);
  35. if (it != namedChannels.end())
  36. {
  37. it->second->retain();
  38. return it->second;
  39. }
  40. namedChannels[name] = new Channel(name);
  41. return namedChannels[name];
  42. }
  43. Channel::Channel()
  44. : named(false)
  45. , sent(0)
  46. , received(0)
  47. {
  48. }
  49. Channel::Channel(const std::string &name)
  50. : named(true)
  51. , name(name)
  52. , sent(0)
  53. , received(0)
  54. {
  55. }
  56. Channel::~Channel()
  57. {
  58. if (named)
  59. {
  60. Lock l(namedChannelMutex);
  61. namedChannels.erase(name);
  62. }
  63. }
  64. uint64 Channel::push(const Variant &var)
  65. {
  66. Lock l(mutex);
  67. // Keep a reference to ourselves
  68. // if we're non-empty and named.
  69. if (named && queue.empty())
  70. retain();
  71. queue.push(var);
  72. cond->broadcast();
  73. return ++sent;
  74. }
  75. void Channel::supply(const Variant &var)
  76. {
  77. Lock l(mutex);
  78. uint64 id = push(var);
  79. while (received < id)
  80. cond->wait(mutex);
  81. }
  82. bool Channel::pop(Variant *var)
  83. {
  84. Lock l(mutex);
  85. if (queue.empty())
  86. return false;
  87. *var = queue.front();
  88. queue.pop();
  89. received++;
  90. cond->broadcast();
  91. // Release our reference to ourselves
  92. // if we're empty and named.
  93. if (named && queue.empty())
  94. release();
  95. return true;
  96. }
  97. void Channel::demand(Variant *var)
  98. {
  99. Lock l(mutex);
  100. while (!pop(var))
  101. cond->wait(mutex);
  102. }
  103. bool Channel::peek(Variant *var)
  104. {
  105. Lock l(mutex);
  106. if (queue.empty())
  107. return false;
  108. *var = queue.front();
  109. return true;
  110. }
  111. int Channel::getCount() const
  112. {
  113. Lock l(mutex);
  114. return (int) queue.size();
  115. }
  116. bool Channel::hasRead(uint64 id) const
  117. {
  118. Lock l(mutex);
  119. return received >= id;
  120. }
  121. void Channel::clear()
  122. {
  123. Lock l(mutex);
  124. // We're already empty.
  125. if (queue.empty())
  126. return;
  127. while (!queue.empty())
  128. queue.pop();
  129. // Finish all the supply waits
  130. received = sent;
  131. cond->broadcast();
  132. // Once again, release our own
  133. // reference if we're named.
  134. if (named)
  135. release();
  136. }
  137. void Channel::lockMutex()
  138. {
  139. mutex->lock();
  140. }
  141. void Channel::unlockMutex()
  142. {
  143. mutex->unlock();
  144. }
  145. } // thread
  146. } // love