msg_q.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* Copyright (c) 2011-2012, 2014, 2017 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. // Uncomment to log verbose logs
  29. #define LOG_NDEBUG 1
  30. #define LOG_TAG "LocSvc_utils_q"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <pthread.h>
  34. #include <loc_pla.h>
  35. #include <log_util.h>
  36. #include "linked_list.h"
  37. #include "msg_q.h"
  38. typedef struct msg_q {
  39. void* msg_list; /* Linked list to store information */
  40. pthread_cond_t list_cond; /* Condition variable for waiting on msg queue */
  41. pthread_mutex_t list_mutex; /* Mutex for exclusive access to message queue */
  42. int unblocked; /* Has this message queue been unblocked? */
  43. } msg_q;
  44. /*===========================================================================
  45. FUNCTION convert_linked_list_err_type
  46. DESCRIPTION
  47. Converts from one set of enum values to another.
  48. linked_list_val: Value to convert to msg_q_enum_type
  49. DEPENDENCIES
  50. N/A
  51. RETURN VALUE
  52. Corresponding linked_list_enum_type in msg_q_enum_type
  53. SIDE EFFECTS
  54. N/A
  55. ===========================================================================*/
  56. static msq_q_err_type convert_linked_list_err_type(linked_list_err_type linked_list_val)
  57. {
  58. switch( linked_list_val )
  59. {
  60. case eLINKED_LIST_SUCCESS:
  61. return eMSG_Q_SUCCESS;
  62. case eLINKED_LIST_INVALID_PARAMETER:
  63. return eMSG_Q_INVALID_PARAMETER;
  64. case eLINKED_LIST_INVALID_HANDLE:
  65. return eMSG_Q_INVALID_HANDLE;
  66. case eLINKED_LIST_UNAVAILABLE_RESOURCE:
  67. return eMSG_Q_UNAVAILABLE_RESOURCE;
  68. case eLINKED_LIST_INSUFFICIENT_BUFFER:
  69. return eMSG_Q_INSUFFICIENT_BUFFER;
  70. case eLINKED_LIST_EMPTY:
  71. return eMSG_Q_EMPTY;
  72. case eLINKED_LIST_FAILURE_GENERAL:
  73. default:
  74. return eMSG_Q_FAILURE_GENERAL;
  75. }
  76. }
  77. /* ----------------------- END INTERNAL FUNCTIONS ---------------------------------------- */
  78. /*===========================================================================
  79. FUNCTION: msg_q_init
  80. ===========================================================================*/
  81. msq_q_err_type msg_q_init(void** msg_q_data)
  82. {
  83. if( msg_q_data == NULL )
  84. {
  85. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  86. return eMSG_Q_INVALID_PARAMETER;
  87. }
  88. msg_q* tmp_msg_q;
  89. tmp_msg_q = (msg_q*)calloc(1, sizeof(msg_q));
  90. if( tmp_msg_q == NULL )
  91. {
  92. LOC_LOGE("%s: Unable to allocate space for message queue!\n", __FUNCTION__);
  93. return eMSG_Q_FAILURE_GENERAL;
  94. }
  95. if( linked_list_init(&tmp_msg_q->msg_list) != 0 )
  96. {
  97. LOC_LOGE("%s: Unable to initialize storage list!\n", __FUNCTION__);
  98. free(tmp_msg_q);
  99. return eMSG_Q_FAILURE_GENERAL;
  100. }
  101. if( pthread_mutex_init(&tmp_msg_q->list_mutex, NULL) != 0 )
  102. {
  103. LOC_LOGE("%s: Unable to initialize list mutex!\n", __FUNCTION__);
  104. linked_list_destroy(&tmp_msg_q->msg_list);
  105. free(tmp_msg_q);
  106. return eMSG_Q_FAILURE_GENERAL;
  107. }
  108. if( pthread_cond_init(&tmp_msg_q->list_cond, NULL) != 0 )
  109. {
  110. LOC_LOGE("%s: Unable to initialize msg q cond var!\n", __FUNCTION__);
  111. linked_list_destroy(&tmp_msg_q->msg_list);
  112. pthread_mutex_destroy(&tmp_msg_q->list_mutex);
  113. free(tmp_msg_q);
  114. return eMSG_Q_FAILURE_GENERAL;
  115. }
  116. tmp_msg_q->unblocked = 0;
  117. *msg_q_data = tmp_msg_q;
  118. return eMSG_Q_SUCCESS;
  119. }
  120. /*===========================================================================
  121. FUNCTION: msg_q_init2
  122. ===========================================================================*/
  123. const void* msg_q_init2()
  124. {
  125. void* q = NULL;
  126. if (eMSG_Q_SUCCESS != msg_q_init(&q)) {
  127. q = NULL;
  128. }
  129. return q;
  130. }
  131. /*===========================================================================
  132. FUNCTION: msg_q_destroy
  133. ===========================================================================*/
  134. msq_q_err_type msg_q_destroy(void** msg_q_data)
  135. {
  136. if( msg_q_data == NULL )
  137. {
  138. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  139. return eMSG_Q_INVALID_HANDLE;
  140. }
  141. msg_q* p_msg_q = (msg_q*)*msg_q_data;
  142. linked_list_destroy(&p_msg_q->msg_list);
  143. pthread_mutex_destroy(&p_msg_q->list_mutex);
  144. pthread_cond_destroy(&p_msg_q->list_cond);
  145. p_msg_q->unblocked = 0;
  146. free(*msg_q_data);
  147. *msg_q_data = NULL;
  148. return eMSG_Q_SUCCESS;
  149. }
  150. /*===========================================================================
  151. FUNCTION: msg_q_snd
  152. ===========================================================================*/
  153. msq_q_err_type msg_q_snd(void* msg_q_data, void* msg_obj, void (*dealloc)(void*))
  154. {
  155. msq_q_err_type rv;
  156. if( msg_q_data == NULL )
  157. {
  158. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  159. return eMSG_Q_INVALID_HANDLE;
  160. }
  161. if( msg_obj == NULL )
  162. {
  163. LOC_LOGE("%s: Invalid msg_obj parameter!\n", __FUNCTION__);
  164. return eMSG_Q_INVALID_PARAMETER;
  165. }
  166. msg_q* p_msg_q = (msg_q*)msg_q_data;
  167. pthread_mutex_lock(&p_msg_q->list_mutex);
  168. LOC_LOGV("%s: Sending message with handle = %p\n", __FUNCTION__, msg_obj);
  169. if( p_msg_q->unblocked )
  170. {
  171. LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__);
  172. pthread_mutex_unlock(&p_msg_q->list_mutex);
  173. return eMSG_Q_UNAVAILABLE_RESOURCE;
  174. }
  175. rv = convert_linked_list_err_type(linked_list_add(p_msg_q->msg_list, msg_obj, dealloc));
  176. /* Show data is in the message queue. */
  177. pthread_cond_signal(&p_msg_q->list_cond);
  178. pthread_mutex_unlock(&p_msg_q->list_mutex);
  179. LOC_LOGV("%s: Finished Sending message with handle = %p\n", __FUNCTION__, msg_obj);
  180. return rv;
  181. }
  182. /*===========================================================================
  183. FUNCTION: msg_q_rcv
  184. ===========================================================================*/
  185. msq_q_err_type msg_q_rcv(void* msg_q_data, void** msg_obj)
  186. {
  187. msq_q_err_type rv;
  188. if( msg_q_data == NULL )
  189. {
  190. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  191. return eMSG_Q_INVALID_HANDLE;
  192. }
  193. if( msg_obj == NULL )
  194. {
  195. LOC_LOGE("%s: Invalid msg_obj parameter!\n", __FUNCTION__);
  196. return eMSG_Q_INVALID_PARAMETER;
  197. }
  198. msg_q* p_msg_q = (msg_q*)msg_q_data;
  199. pthread_mutex_lock(&p_msg_q->list_mutex);
  200. if( p_msg_q->unblocked )
  201. {
  202. LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__);
  203. pthread_mutex_unlock(&p_msg_q->list_mutex);
  204. return eMSG_Q_UNAVAILABLE_RESOURCE;
  205. }
  206. /* Wait for data in the message queue */
  207. while( linked_list_empty(p_msg_q->msg_list) && !p_msg_q->unblocked )
  208. {
  209. pthread_cond_wait(&p_msg_q->list_cond, &p_msg_q->list_mutex);
  210. }
  211. rv = convert_linked_list_err_type(linked_list_remove(p_msg_q->msg_list, msg_obj));
  212. pthread_mutex_unlock(&p_msg_q->list_mutex);
  213. LOC_LOGV("%s: Received message %p rv = %d\n", __FUNCTION__, *msg_obj, rv);
  214. return rv;
  215. }
  216. /*===========================================================================
  217. FUNCTION: msg_q_rmv
  218. ===========================================================================*/
  219. msq_q_err_type msg_q_rmv(void* msg_q_data, void** msg_obj)
  220. {
  221. msq_q_err_type rv;
  222. if (msg_q_data == NULL) {
  223. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  224. return eMSG_Q_INVALID_HANDLE;
  225. }
  226. if (msg_obj == NULL) {
  227. LOC_LOGE("%s: Invalid msg_obj parameter!\n", __FUNCTION__);
  228. return eMSG_Q_INVALID_PARAMETER;
  229. }
  230. msg_q* p_msg_q = (msg_q*)msg_q_data;
  231. pthread_mutex_lock(&p_msg_q->list_mutex);
  232. if (p_msg_q->unblocked) {
  233. LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__);
  234. pthread_mutex_unlock(&p_msg_q->list_mutex);
  235. return eMSG_Q_UNAVAILABLE_RESOURCE;
  236. }
  237. if (linked_list_empty(p_msg_q->msg_list)) {
  238. LOC_LOGW("%s: list is empty !!\n", __FUNCTION__);
  239. pthread_mutex_unlock(&p_msg_q->list_mutex);
  240. return eMSG_Q_EMPTY;
  241. }
  242. rv = convert_linked_list_err_type(linked_list_remove(p_msg_q->msg_list, msg_obj));
  243. pthread_mutex_unlock(&p_msg_q->list_mutex);
  244. LOC_LOGV("%s: Removed message %p rv = %d\n", __FUNCTION__, *msg_obj, rv);
  245. return rv;
  246. }
  247. /*===========================================================================
  248. FUNCTION: msg_q_flush
  249. ===========================================================================*/
  250. msq_q_err_type msg_q_flush(void* msg_q_data)
  251. {
  252. msq_q_err_type rv;
  253. if ( msg_q_data == NULL )
  254. {
  255. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  256. return eMSG_Q_INVALID_HANDLE;
  257. }
  258. msg_q* p_msg_q = (msg_q*)msg_q_data;
  259. LOC_LOGD("%s: Flushing Message Queue\n", __FUNCTION__);
  260. pthread_mutex_lock(&p_msg_q->list_mutex);
  261. /* Remove all elements from the list */
  262. rv = convert_linked_list_err_type(linked_list_flush(p_msg_q->msg_list));
  263. pthread_mutex_unlock(&p_msg_q->list_mutex);
  264. LOC_LOGD("%s: Message Queue flushed\n", __FUNCTION__);
  265. return rv;
  266. }
  267. /*===========================================================================
  268. FUNCTION: msg_q_unblock
  269. ===========================================================================*/
  270. msq_q_err_type msg_q_unblock(void* msg_q_data)
  271. {
  272. if ( msg_q_data == NULL )
  273. {
  274. LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__);
  275. return eMSG_Q_INVALID_HANDLE;
  276. }
  277. msg_q* p_msg_q = (msg_q*)msg_q_data;
  278. pthread_mutex_lock(&p_msg_q->list_mutex);
  279. if( p_msg_q->unblocked )
  280. {
  281. LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__);
  282. pthread_mutex_unlock(&p_msg_q->list_mutex);
  283. return eMSG_Q_UNAVAILABLE_RESOURCE;
  284. }
  285. LOC_LOGD("%s: Unblocking Message Queue\n", __FUNCTION__);
  286. /* Unblocking message queue */
  287. p_msg_q->unblocked = 1;
  288. /* Allow all the waiters to wake up */
  289. pthread_cond_broadcast(&p_msg_q->list_cond);
  290. pthread_mutex_unlock(&p_msg_q->list_mutex);
  291. LOC_LOGD("%s: Message Queue unblocked\n", __FUNCTION__);
  292. return eMSG_Q_SUCCESS;
  293. }