PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  26. #include <apr_pools.h>
  27. #include <apr_queue.h>
  28. #include "llthreadsafequeue.h"
  29. // LLThreadSafeQueueImplementation
  30. //-----------------------------------------------------------------------------
  31. LLThreadSafeQueueImplementation::LLThreadSafeQueueImplementation(apr_pool_t * pool, unsigned int capacity):
  32. mOwnsPool(pool == 0),
  33. mPool(pool),
  34. mQueue(0)
  35. {
  36. if(mOwnsPool) {
  37. apr_status_t status = apr_pool_create(&mPool, 0);
  38. if(status != APR_SUCCESS) throw LLThreadSafeQueueError("failed to allocate pool");
  39. } else {
  40. ; // No op.
  41. }
  42. apr_status_t status = apr_queue_create(&mQueue, capacity, mPool);
  43. if(status != APR_SUCCESS) throw LLThreadSafeQueueError("failed to allocate queue");
  44. }
  45. LLThreadSafeQueueImplementation::~LLThreadSafeQueueImplementation()
  46. {
  47. if(mQueue != 0) {
  48. if(apr_queue_size(mQueue) != 0) llwarns <<
  49. "terminating queue which still contains " << apr_queue_size(mQueue) <<
  50. " elements;" << "memory will be leaked" << LL_ENDL;
  51. apr_queue_term(mQueue);
  52. }
  53. if(mOwnsPool && (mPool != 0)) apr_pool_destroy(mPool);
  54. }
  55. void LLThreadSafeQueueImplementation::pushFront(void * element)
  56. {
  57. apr_status_t status = apr_queue_push(mQueue, element);
  58. if(status == APR_EINTR) {
  59. throw LLThreadSafeQueueInterrupt();
  60. } else if(status != APR_SUCCESS) {
  61. throw LLThreadSafeQueueError("push failed");
  62. } else {
  63. ; // Success.
  64. }
  65. }
  66. bool LLThreadSafeQueueImplementation::tryPushFront(void * element){
  67. return apr_queue_trypush(mQueue, element) == APR_SUCCESS;
  68. }
  69. void * LLThreadSafeQueueImplementation::popBack(void)
  70. {
  71. void * element;
  72. apr_status_t status = apr_queue_pop(mQueue, &element);
  73. if(status == APR_EINTR) {
  74. throw LLThreadSafeQueueInterrupt();
  75. } else if(status != APR_SUCCESS) {
  76. throw LLThreadSafeQueueError("pop failed");
  77. } else {
  78. return element;
  79. }
  80. }
  81. bool LLThreadSafeQueueImplementation::tryPopBack(void *& element)
  82. {
  83. return apr_queue_trypop(mQueue, &element) == APR_SUCCESS;
  84. }
  85. size_t LLThreadSafeQueueImplementation::size()
  86. {
  87. return apr_queue_size(mQueue);
  88. }