llcorehttprequestqueue.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @file llcorehttprequestqueue.cpp
  3. * @brief Implementation for the internal operation request queue
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llcorehttprequestqueue.h"
  28. #include "llcorehttpoperation.h"
  29. #include "llcoremutex.h"
  30. using namespace LLCoreInt;
  31. namespace LLCore
  32. {
  33. HttpRequestQueue* HttpRequestQueue::sInstance = NULL;
  34. HttpRequestQueue::HttpRequestQueue()
  35. : RefCounted(true),
  36. mQueueStopped(false)
  37. {
  38. }
  39. HttpRequestQueue::~HttpRequestQueue()
  40. {
  41. if (!mQueue.empty())
  42. {
  43. HttpScopedLock lock(mQueueMutex);
  44. llwarns << "Queue not empty on destruction. Emptying now..." << llendl;
  45. mQueue.clear();
  46. }
  47. }
  48. void HttpRequestQueue::init()
  49. {
  50. llassert_always(!sInstance);
  51. sInstance = new HttpRequestQueue();
  52. }
  53. void HttpRequestQueue::term()
  54. {
  55. if (sInstance)
  56. {
  57. sInstance->release();
  58. sInstance = NULL;
  59. }
  60. }
  61. HttpStatus HttpRequestQueue::addOp(const HttpRequestQueue::opPtr_t& op)
  62. {
  63. bool wake = false;
  64. {
  65. HttpScopedLock lock(mQueueMutex);
  66. if (mQueueStopped)
  67. {
  68. // Return op and error to caller
  69. return HttpStatus(HttpStatus::LLCORE, HE_SHUTTING_DOWN);
  70. }
  71. wake = mQueue.empty();
  72. mQueue.push_back(op);
  73. }
  74. if (wake)
  75. {
  76. mQueueCV.notify_all();
  77. }
  78. return HttpStatus();
  79. }
  80. HttpRequestQueue::opPtr_t HttpRequestQueue::fetchOp(bool wait)
  81. {
  82. HttpOperation::ptr_t result;
  83. {
  84. HttpScopedLock lock(mQueueMutex);
  85. while (mQueue.empty())
  86. {
  87. if (!wait || mQueueStopped)
  88. {
  89. return HttpOperation::ptr_t();
  90. }
  91. mQueueCV.wait(lock);
  92. }
  93. result = mQueue.front();
  94. mQueue.erase(mQueue.begin());
  95. }
  96. // Caller also acquires the reference count
  97. return result;
  98. }
  99. void HttpRequestQueue::fetchAll(bool wait, OpContainer& ops)
  100. {
  101. // Not valid putting something back on the queue...
  102. llassert_always(ops.empty());
  103. {
  104. HttpScopedLock lock(mQueueMutex);
  105. while (mQueue.empty())
  106. {
  107. if (!wait || mQueueStopped)
  108. {
  109. return;
  110. }
  111. mQueueCV.wait(lock);
  112. }
  113. mQueue.swap(ops);
  114. }
  115. }
  116. void HttpRequestQueue::wakeAll()
  117. {
  118. mQueueCV.notify_all();
  119. }
  120. bool HttpRequestQueue::stopQueue()
  121. {
  122. HttpScopedLock lock(mQueueMutex);
  123. if (!mQueueStopped)
  124. {
  125. mQueueStopped = true;
  126. wakeAll();
  127. return true;
  128. }
  129. wakeAll();
  130. return false;
  131. }
  132. } // End namespace LLCore