AESMQueue.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef AESM_QUEUE_H
  32. #define AESM_QUEUE_H
  33. #include <queue>
  34. #include <pthread.h>
  35. #include "RequestData.h"
  36. #include "IAERequest.h"
  37. #include "IAESMQueue.h"
  38. #include <oal/error_report.h>
  39. template <typename T>
  40. class AESMQueue : public IAESMQueue<T> {
  41. public:
  42. AESMQueue();
  43. ~AESMQueue();
  44. void push(T*);
  45. T* blockingPop();
  46. void close();
  47. private:
  48. std::queue<T*> m_queue;
  49. pthread_cond_t m_queueCond;
  50. pthread_mutex_t m_queueMutex;
  51. volatile uint8_t m_events;
  52. private:
  53. AESMQueue& operator=(const AESMQueue&);
  54. AESMQueue(const AESMQueue&);
  55. };
  56. #define QUEUE_EVENT_CLOSE 1
  57. template<typename T>
  58. AESMQueue<T>::AESMQueue() :
  59. m_queue()
  60. {
  61. int rc;
  62. rc = pthread_mutex_init(&m_queueMutex, NULL);
  63. if (rc != 0)
  64. {
  65. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to initialize mutex");
  66. exit(-1);
  67. }
  68. rc = pthread_cond_init(&m_queueCond, NULL);
  69. if (rc != 0)
  70. {
  71. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to initialize a condition variable");
  72. exit(-1);
  73. }
  74. m_events = 0;
  75. }
  76. template<typename T>
  77. AESMQueue<T>::~AESMQueue()
  78. {
  79. int rc;
  80. rc = pthread_mutex_destroy(&m_queueMutex);
  81. if (rc != 0)
  82. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to destroy mutex");
  83. rc = pthread_cond_destroy(&m_queueCond);
  84. if (rc != 0)
  85. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to destory a condition variable");
  86. }
  87. template<typename T>
  88. void AESMQueue<T>::push(T* value)
  89. {
  90. int rc;
  91. rc = pthread_mutex_lock(&m_queueMutex);
  92. if (rc != 0)
  93. {
  94. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to acquire mutex");
  95. exit(-1);
  96. }
  97. m_queue.push(value);
  98. // m_events |= QUEUE_EVENT_INSERT;
  99. rc = pthread_cond_signal(&m_queueCond);
  100. if (rc != 0)
  101. {
  102. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to signal condition");
  103. exit(-1);
  104. }
  105. rc = pthread_mutex_unlock(&m_queueMutex);
  106. if (rc != 0)
  107. {
  108. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to unlock mutex");
  109. exit(-1);
  110. }
  111. }
  112. template<typename T>
  113. T* AESMQueue<T>::blockingPop()
  114. {
  115. int rc;
  116. T* value = NULL;
  117. rc = pthread_mutex_lock(&m_queueMutex);
  118. if (rc != 0)
  119. {
  120. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to acquire mutex");
  121. exit(-1);
  122. }
  123. while(1) {
  124. // check CLOSE event
  125. if (m_events & QUEUE_EVENT_CLOSE) {
  126. //epmty the queue
  127. while (!m_queue.empty())
  128. {
  129. value = m_queue.front();
  130. m_queue.pop();
  131. delete value;
  132. }
  133. value = NULL;
  134. break;
  135. }
  136. if (!m_queue.empty()) {
  137. // queue is not empty
  138. value = m_queue.front();
  139. m_queue.pop();
  140. break;
  141. }
  142. else {
  143. rc = pthread_cond_wait(&m_queueCond, &m_queueMutex);
  144. if (rc != 0)
  145. {
  146. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed wait on a condition");
  147. exit(-1);
  148. }
  149. }
  150. }
  151. rc = pthread_mutex_unlock(&m_queueMutex);
  152. if (rc != 0)
  153. {
  154. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to unlock mutex");
  155. exit(-1);
  156. }
  157. return value;
  158. }
  159. template<typename T>
  160. void AESMQueue<T>::close()
  161. {
  162. int rc;
  163. rc = pthread_mutex_lock(&m_queueMutex);
  164. if (rc != 0)
  165. {
  166. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to acquire mutex");
  167. exit(-1);
  168. }
  169. m_events |= QUEUE_EVENT_CLOSE;
  170. rc = pthread_cond_signal(&m_queueCond);
  171. if (rc != 0)
  172. {
  173. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to signal a condition");
  174. exit(-1);
  175. }
  176. rc = pthread_mutex_unlock(&m_queueMutex);
  177. if (rc != 0)
  178. {
  179. aesm_log_report(AESM_LOG_REPORT_ERROR, "Failed to unlock mutex");
  180. exit(-1);
  181. }
  182. }
  183. #endif