PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llthreadsafequeue.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 109 lines | 62 code | 21 blank | 26 comment | 28 complexity | aa1c5e8c35dc7e244fcba1006e299432 MD5 | raw file
  1/** 
  2 * @file llthread.cpp
  3 *
  4 * $LicenseInfo:firstyear=2004&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2010, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26#include "linden_common.h"
 27#include <apr_pools.h>
 28#include <apr_queue.h>
 29#include "llthreadsafequeue.h"
 30
 31
 32
 33// LLThreadSafeQueueImplementation
 34//-----------------------------------------------------------------------------
 35
 36
 37LLThreadSafeQueueImplementation::LLThreadSafeQueueImplementation(apr_pool_t * pool, unsigned int capacity):
 38	mOwnsPool(pool == 0),
 39	mPool(pool),
 40	mQueue(0)
 41{
 42	if(mOwnsPool) {
 43		apr_status_t status = apr_pool_create(&mPool, 0);
 44		if(status != APR_SUCCESS) throw LLThreadSafeQueueError("failed to allocate pool");
 45	} else {
 46		; // No op.
 47	}
 48	
 49	apr_status_t status = apr_queue_create(&mQueue, capacity, mPool);
 50	if(status != APR_SUCCESS) throw LLThreadSafeQueueError("failed to allocate queue");
 51}
 52
 53
 54LLThreadSafeQueueImplementation::~LLThreadSafeQueueImplementation()
 55{
 56	if(mQueue != 0) {
 57		if(apr_queue_size(mQueue) != 0) llwarns << 
 58			"terminating queue which still contains " << apr_queue_size(mQueue) <<
 59			" elements;" << "memory will be leaked" << LL_ENDL;
 60		apr_queue_term(mQueue);
 61	}
 62	if(mOwnsPool && (mPool != 0)) apr_pool_destroy(mPool);
 63}
 64
 65
 66void LLThreadSafeQueueImplementation::pushFront(void * element)
 67{
 68	apr_status_t status = apr_queue_push(mQueue, element);
 69	
 70	if(status == APR_EINTR) {
 71		throw LLThreadSafeQueueInterrupt();
 72	} else if(status != APR_SUCCESS) {
 73		throw LLThreadSafeQueueError("push failed");
 74	} else {
 75		; // Success.
 76	}
 77}
 78
 79
 80bool LLThreadSafeQueueImplementation::tryPushFront(void * element){
 81	return apr_queue_trypush(mQueue, element) == APR_SUCCESS;
 82}
 83
 84
 85void * LLThreadSafeQueueImplementation::popBack(void)
 86{
 87	void * element;
 88	apr_status_t status = apr_queue_pop(mQueue, &element);
 89
 90	if(status == APR_EINTR) {
 91		throw LLThreadSafeQueueInterrupt();
 92	} else if(status != APR_SUCCESS) {
 93		throw LLThreadSafeQueueError("pop failed");
 94	} else {
 95		return element;
 96	}
 97}
 98
 99
100bool LLThreadSafeQueueImplementation::tryPopBack(void *& element)
101{
102	return apr_queue_trypop(mQueue, &element) == APR_SUCCESS;
103}
104
105
106size_t LLThreadSafeQueueImplementation::size()
107{
108	return apr_queue_size(mQueue);
109}