Channel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2006-2012 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 l(namedChannelMutex);
  34. if (!namedChannels.count(name))
  35. namedChannels[name] = new Channel(name);
  36. else
  37. namedChannels[name]->retain();
  38. return namedChannels[name];
  39. }
  40. Channel::Channel()
  41. : named(false)
  42. {
  43. mutex = newMutex();
  44. cond = newConditional();
  45. }
  46. Channel::Channel(const std::string &name)
  47. : named(true), name(name)
  48. {
  49. mutex = newMutex();
  50. cond = newConditional();
  51. }
  52. Channel::~Channel()
  53. {
  54. while (!queue.empty())
  55. {
  56. queue.front()->release();
  57. queue.pop();
  58. }
  59. delete mutex;
  60. delete cond;
  61. if (named)
  62. {
  63. Lock l(namedChannelMutex);
  64. namedChannels.erase(name);
  65. }
  66. }
  67. void Channel::push(Variant *var)
  68. {
  69. if (!var)
  70. return;
  71. Lock l(mutex);
  72. var->retain();
  73. // Keep a reference to ourselves
  74. // if we're non-empty and named.
  75. if (named && queue.empty())
  76. retain();
  77. queue.push(var);
  78. cond->signal();
  79. }
  80. Variant *Channel::pop()
  81. {
  82. Lock l(mutex);
  83. if (queue.empty())
  84. return 0;
  85. Variant *var = queue.front();
  86. queue.pop();
  87. // Release our reference to ourselves
  88. // if we're empty and named.
  89. if (named && queue.empty())
  90. release();
  91. return var;
  92. } // NOTE: Returns a retained Variant
  93. Variant *Channel::demand()
  94. {
  95. Variant *var;
  96. while (!(var = pop()))
  97. {
  98. mutex->lock();
  99. cond->wait(mutex);
  100. mutex->unlock();
  101. }
  102. return var;
  103. }
  104. Variant *Channel::peek()
  105. {
  106. Lock l(mutex);
  107. if (queue.empty())
  108. return 0;
  109. Variant *var = queue.front();
  110. var->retain();
  111. return var;
  112. }
  113. int Channel::count()
  114. {
  115. Lock l(mutex);
  116. return queue.size();
  117. }
  118. void Channel::clear()
  119. {
  120. Lock l(mutex);
  121. while (!queue.empty())
  122. {
  123. queue.front()->release();
  124. queue.pop();
  125. }
  126. // Once again, release our own
  127. // reference if we're named.
  128. if (named)
  129. release();
  130. }
  131. } // thread
  132. } // love