/sparrowhawk/foundation/ESFSharedEmbeddedQueue.cpp

http://github.com/jtblatt/duderino · C++ · 138 lines · 85 code · 37 blank · 16 comment · 28 complexity · 422918a3043a01ed74a3f18fa015d3bf MD5 · raw file

  1. /** @file ESFSharedEmbeddedQueue.cpp
  2. * @brief A producer/consumer queue of ESFEmbeddedListElements
  3. *
  4. * Copyright (c) 2009 Yahoo! Inc.
  5. * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
  6. * under the BSD (revised) open source license.
  7. *
  8. * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
  9. * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
  10. *
  11. *
  12. * $Author: blattj $
  13. * $Date: 2009/05/25 21:51:08 $
  14. * $Name: $
  15. * $Revision: 1.3 $
  16. */
  17. #ifndef ESF_SHARED_EMBEDDED_QUEUE_H
  18. #include <ESFSharedEmbeddedQueue.h>
  19. #endif
  20. ESFSharedEmbeddedQueue::ESFSharedEmbeddedQueue() :
  21. _isStopped(false), _list() {
  22. #if defined HAVE_PTHREAD_MUTEX_INIT && defined HAVE_PTHREAD_COND_INIT && \
  23. defined HAVE_PTHREAD_MUTEX_DESTROY && defined HAVE_PTHREAD_COND_DESTROY
  24. pthread_mutex_init(&_mutex, 0);
  25. pthread_cond_init(&_signal, 0);
  26. #else
  27. #error "Platform has no mutex or signal initializer"
  28. #endif
  29. }
  30. ESFSharedEmbeddedQueue::~ESFSharedEmbeddedQueue() {
  31. #if defined HAVE_PTHREAD_MUTEX_DESTROY && defined HAVE_PTHREAD_COND_DESTROY
  32. pthread_mutex_destroy(&_mutex);
  33. pthread_cond_destroy(&_signal);
  34. #else
  35. #error "Platform has no mutex or signal destructor"
  36. #endif
  37. }
  38. ESFError ESFSharedEmbeddedQueue::push(ESFEmbeddedListElement *element) {
  39. #if defined HAVE_PTHREAD_MUTEX_LOCK && defined HAVE_PTHREAD_MUTEX_UNLOCK && defined HAVE_PTHREAD_COND_SIGNAL
  40. ESFError error = ESFConvertError(pthread_mutex_lock(&_mutex));
  41. if (ESF_SUCCESS != error) {
  42. return error;
  43. }
  44. if (_isStopped) {
  45. pthread_mutex_unlock(&_mutex);
  46. return ESF_SHUTDOWN;
  47. }
  48. _list.addLast(element);
  49. pthread_mutex_unlock(&_mutex);
  50. pthread_cond_signal(&_signal);
  51. #else
  52. #error "Platform has no mutex lock, mutex unlock, or cond signal"
  53. #endif
  54. return ESF_SUCCESS;
  55. }
  56. ESFEmbeddedListElement *ESFSharedEmbeddedQueue::pop(ESFError *result) {
  57. ESFEmbeddedListElement *tmp = 0;
  58. if (result) *result = ESF_SUCCESS;
  59. #if defined HAVE_PTHREAD_MUTEX_LOCK && defined HAVE_PTHREAD_MUTEX_UNLOCK && defined HAVE_PTHREAD_COND_WAIT
  60. ESFError error = ESFConvertError(pthread_mutex_lock(&_mutex));
  61. if (ESF_SUCCESS != error) {
  62. if (result) *result = error;
  63. return 0;
  64. }
  65. do {
  66. if (_isStopped) {
  67. break;
  68. }
  69. tmp = _list.removeFirst();
  70. if (tmp) {
  71. break;
  72. }
  73. error = ESFConvertError(pthread_cond_wait(&_signal, &_mutex));
  74. if (ESF_SUCCESS != error && ESF_INTR != error) {
  75. if (result) *result = error;
  76. return 0;
  77. }
  78. } while (0 == tmp);
  79. pthread_mutex_unlock(&_mutex);
  80. #else
  81. #error "Platform has no mutex lock, mutex unlock, or cond wait"
  82. #endif
  83. if (tmp) {
  84. return tmp;
  85. }
  86. if (result) {
  87. *result = ESF_SHUTDOWN;
  88. }
  89. return 0;
  90. }
  91. void ESFSharedEmbeddedQueue::stop() {
  92. #if defined HAVE_PTHREAD_MUTEX_LOCK && defined HAVE_PTHREAD_MUTEX_UNLOCK && defined HAVE_PTHREAD_COND_BROADCAST
  93. pthread_mutex_lock(&_mutex);
  94. _isStopped = true;
  95. _list.clear(true);
  96. pthread_mutex_unlock(&_mutex);
  97. pthread_cond_broadcast(&_signal);
  98. #else
  99. #error "Platform has no mutex lock, mutex unlock, or cond broadcast"
  100. #endif
  101. }