123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /**
- * @file llcorehttprequestqueue.cpp
- * @brief Implementation for the internal operation request queue
- *
- * $LicenseInfo:firstyear=2012&license=viewerlgpl$
- * Second Life Viewer Source Code
- * Copyright (C) 2012, Linden Research, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation;
- * version 2.1 of the License only.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "llcorehttprequestqueue.h"
- #include "llcorehttpoperation.h"
- #include "llcoremutex.h"
- using namespace LLCoreInt;
- namespace LLCore
- {
- HttpRequestQueue* HttpRequestQueue::sInstance = NULL;
- HttpRequestQueue::HttpRequestQueue()
- : RefCounted(true),
- mQueueStopped(false)
- {
- }
- HttpRequestQueue::~HttpRequestQueue()
- {
- if (!mQueue.empty())
- {
- HttpScopedLock lock(mQueueMutex);
- llwarns << "Queue not empty on destruction. Emptying now..." << llendl;
- mQueue.clear();
- }
- }
- void HttpRequestQueue::init()
- {
- llassert_always(!sInstance);
- sInstance = new HttpRequestQueue();
- }
- void HttpRequestQueue::term()
- {
- if (sInstance)
- {
- sInstance->release();
- sInstance = NULL;
- }
- }
- HttpStatus HttpRequestQueue::addOp(const HttpRequestQueue::opPtr_t& op)
- {
- bool wake = false;
- {
- HttpScopedLock lock(mQueueMutex);
- if (mQueueStopped)
- {
- // Return op and error to caller
- return HttpStatus(HttpStatus::LLCORE, HE_SHUTTING_DOWN);
- }
- wake = mQueue.empty();
- mQueue.push_back(op);
- }
- if (wake)
- {
- mQueueCV.notify_all();
- }
- return HttpStatus();
- }
- HttpRequestQueue::opPtr_t HttpRequestQueue::fetchOp(bool wait)
- {
- HttpOperation::ptr_t result;
- {
- HttpScopedLock lock(mQueueMutex);
- while (mQueue.empty())
- {
- if (!wait || mQueueStopped)
- {
- return HttpOperation::ptr_t();
- }
- mQueueCV.wait(lock);
- }
- result = mQueue.front();
- mQueue.erase(mQueue.begin());
- }
- // Caller also acquires the reference count
- return result;
- }
- void HttpRequestQueue::fetchAll(bool wait, OpContainer& ops)
- {
- // Not valid putting something back on the queue...
- llassert_always(ops.empty());
- {
- HttpScopedLock lock(mQueueMutex);
- while (mQueue.empty())
- {
- if (!wait || mQueueStopped)
- {
- return;
- }
- mQueueCV.wait(lock);
- }
- mQueue.swap(ops);
- }
- }
- void HttpRequestQueue::wakeAll()
- {
- mQueueCV.notify_all();
- }
- bool HttpRequestQueue::stopQueue()
- {
- HttpScopedLock lock(mQueueMutex);
- if (!mQueueStopped)
- {
- mQueueStopped = true;
- wakeAll();
- return true;
- }
- wakeAll();
- return false;
- }
- } // End namespace LLCore
|