core_queu.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ******************************************************************************
  3. * History: Date; Author; Description
  4. * 07 Dec. 2015; Chen, George; file creation
  5. ******************************************************************************
  6. */
  7. /*
  8. ******************************************************************************
  9. * explanation
  10. ******************************************************************************
  11. * Multiple Task Pending On Single Msg Queue.
  12. *
  13. * +============+ +===========+ +===========+
  14. * | msgq | | pdat | | pdat |
  15. * +============+ +===========+ +===========+
  16. * | ~ | | ~ | | ~ |
  17. * | pdat_head |-+ NULL/-+| pdat_list |/-~-/| pdat_list |->NULL
  18. * | ~ | +---------/+| ~ | | ~ |
  19. * +------------+ +-----------+ +-----------+
  20. *
  21. */
  22. /*
  23. ******************************************************************************
  24. * Includes
  25. ******************************************************************************
  26. */
  27. #include "core_queu.h"
  28. // #include "core_imgr.h"
  29. /*
  30. ******************************************************************************
  31. * Definition
  32. ******************************************************************************
  33. */
  34. // #define CORE_QUEU_CFGS_LOGS_TAG "QUEU"
  35. #ifdef CORE_QUEU_CFGS_LOGS_TAG
  36. /* ERROR */
  37. #define log_err(fmt, ...) \
  38. logs_logs_err(CORE_QUEU_CFGS_LOGS_TAG, fmt, ##__VA_ARGS__)
  39. /* WARNING */
  40. #define log_war(fmt, ...) \
  41. logs_logs_war(CORE_QUEU_CFGS_LOGS_TAG, fmt, ##__VA_ARGS__)
  42. /* INFORMATION */
  43. #define log_inf(fmt, ...) \
  44. logs_logs_inf(CORE_QUEU_CFGS_LOGS_TAG, fmt, ##__VA_ARGS__)
  45. /* VERB */
  46. #define log_ver(fmt, ...) \
  47. logs_logs_ver(CORE_QUEU_CFGS_LOGS_TAG, fmt, ##__VA_ARGS__)
  48. /* Function entry */
  49. #define log_ent(fmt, ...) \
  50. logs_logs_ent(fmt, ##__VA_ARGS__)
  51. /* function exit */
  52. #define log_exi(fmt, ...) \
  53. logs_logs_exi(fmt, ##__VA_ARGS__)
  54. #else
  55. /* ERROR */
  56. #define log_err(fmt, ...)
  57. /* WARNING */
  58. #define log_war(fmt, ...)
  59. /* INFORMATION */
  60. #define log_inf(fmt, ...)
  61. /* VERB */
  62. #define log_ver(fmt, ...)
  63. /* Function entry */
  64. #define log_ent(fmt, ...)
  65. /* function exit */
  66. #define log_exi(fmt, ...)
  67. #endif /* CORE_QUEU_CFGS_LOGS_TAG */
  68. /*
  69. ******************************************************************************
  70. * Definition
  71. ******************************************************************************
  72. */
  73. /*
  74. ******************************************************************************
  75. * public function implementations
  76. ******************************************************************************
  77. */
  78. /*
  79. * # Name: core_msgq_ini
  80. * # Desc: initialize a msg queue
  81. * # Para: msgq: msg queue to be initialised.
  82. * name: name of new task, in c string.
  83. * # rslt: 0 success; others failure.
  84. */
  85. int32
  86. core_sque_ini(core_sque_t* sque)
  87. {
  88. int32 rslt = FALSE;
  89. uint32 size = 0;
  90. uint32 coun = 0;
  91. void* blck = NULL;
  92. log_ent("PARA>>msgq:0x%08x", sque);
  93. /* message pool */
  94. // sque->sque_pool; // [set by usr]
  95. // sque->pool_size; // [set by usr]
  96. /* message size */
  97. // sque->size_qdat; // [set by usr]
  98. if ( NULL == sque->sque_pool ) {
  99. goto ERR_POOL;
  100. }
  101. /* ini queue */
  102. if ( TRUE != list_sque_ini(&sque->sque_dats) ) {
  103. goto ERR_SQUE;
  104. }
  105. /* insert queue node */
  106. size = sizeof ( core_qdat_t ) + sque->size_qdat;
  107. coun = (sque->size_pool / size);
  108. blck = ((uint8*) sque->sque_pool) + coun * size;
  109. while ( coun -- ) {
  110. blck = ((uint8*) blck) - size;
  111. // if ( TRUE != list_snod_ini_cir((list_snod_t*) blck) ) {
  112. if ( TRUE != list_snod_ini((list_snod_t*) blck, list_slst_enum_snod_cir) ) {
  113. goto ERR_SQUE;
  114. }
  115. if ( TRUE != list_slst_add(&sque->sque_dats.list, (list_snod_t*) blck) ) {
  116. goto ERR_SQUE;
  117. }
  118. }
  119. /* get ready to use */
  120. if ( TRUE != list_sque_fsh(&sque->sque_dats) ) {
  121. goto ERR_SQUE;
  122. }
  123. rslt = TRUE;
  124. goto ERR_SUCC;
  125. ERR_SQUE:
  126. ERR_POOL:
  127. ERR_SUCC:
  128. log_exi("RSLT>>rslt: %d", rslt);
  129. return ( rslt );
  130. }
  131. core_sque_t*
  132. core_sque_new(uint32 size, uint32 coun)
  133. {
  134. uint32 plsz = (size + sizeof ( core_qdat_t )) * coun; // pool size of all msg blk + msg dat
  135. core_sque_t* sque = NULL;
  136. log_ent("PARA>>size:%d, cunt:%d", size, coun);
  137. // allocate memory for task control block & task stack, shares one mem control block
  138. if ( NULL == (sque = (core_sque_t*) osal_mem_alloc(sizeof ( core_sque_t ) + plsz)) ) {
  139. goto ERR_MMGR;
  140. }
  141. // initialize sque control block
  142. sque->sque_pool = ((uint8*) sque + sizeof ( core_sque_t ));
  143. sque->size_pool = plsz;
  144. sque->size_qdat = size;
  145. // sque->sque_msgs
  146. if ( TRUE != core_sque_ini(sque) ) {
  147. goto ERR_SQUE;
  148. }
  149. goto ERR_SUCC;
  150. ERR_SQUE:
  151. osal_mem_free(sque);
  152. sque = NULL;
  153. ERR_MMGR:
  154. ERR_SUCC:
  155. log_exi("RSLT>>sque:0x%08x", sque);
  156. return ( sque );
  157. }
  158. int32
  159. core_sque_del(core_sque_t* sque)
  160. {
  161. osal_mem_free(sque);
  162. return ( TRUE );
  163. }
  164. int32
  165. core_sque_pop(core_sque_t* sque, void* data)
  166. {
  167. int32 rslt = FALSE;
  168. log_ent("PARA>>sque:0x%08x, data:0x%08x", sque, data);
  169. if ( NULL == sque ) {
  170. goto ERR_SQUE;
  171. }
  172. if ( TRUE != list_sque_pop(&sque->sque_dats, data, sque->size_qdat) ) {
  173. goto ERR_SQUE;
  174. }
  175. rslt = TRUE;
  176. goto ERR_SUCC;
  177. ERR_SQUE:
  178. ERR_SUCC:
  179. log_exi("RSLT>>rslt: %d", rslt);
  180. return ( rslt );
  181. }
  182. int32
  183. core_sque_psh(core_sque_t* sque, const void* data)
  184. {
  185. int32 rslt = FALSE;
  186. log_ent("PARA>>sque:0x%08x, data:0x%08x", sque, data);
  187. if ( NULL == sque ) {
  188. goto ERR_SQUE;
  189. }
  190. if ( TRUE != list_sque_psh(&sque->sque_dats, (void*)data, sque->size_qdat) ) {
  191. goto ERR_SQUE;
  192. }
  193. rslt = TRUE;
  194. goto ERR_SUCC;
  195. ERR_SQUE:
  196. ERR_SUCC:
  197. log_exi("RSLT>>rslt: %d", rslt);
  198. return ( rslt );
  199. }
  200. /*
  201. * # Name: core_msgq_ini
  202. * # Desc: initialize a msg queue
  203. * # Para: msgq: msg queue to be initialised.
  204. * name: name of new task, in c string.
  205. * # rslt: 0 success; others failure.
  206. */
  207. // int32
  208. // core_msgq_ini(core_msgq_t* msgq)
  209. // {
  210. // int32 rslt = FALSE;
  211. // log_ent("PARA>>msgq:0x%08x", msgq);
  212. // /* pending task */
  213. // msgq->pend_task = NULL;
  214. // /* init' queue */
  215. // if ( TRUE != core_sque_ini(&msgq->msgq_msgs) ) {
  216. // goto ERRN_SQUE;
  217. // }
  218. // rslt = TRUE;
  219. // goto ERRN_SUCC;
  220. // ERRN_SQUE:
  221. // ERRN_SUCC:
  222. // log_exi("RSLT>>rslt: %d", rslt);
  223. // return ( rslt );
  224. // }
  225. // core_msgq_t*
  226. // core_msgq_new(uint32 size, uint32 coun)
  227. // {
  228. // uint32 plsz = (size + sizeof ( core_qdat_t )) * coun; // pool size of all msg blk + msg dat
  229. // core_msgq_t* msgq = NULL;
  230. // log_ent("PARA>>size:%d, cunt:%d", size, coun);
  231. // // allocate memory for task control block & task stack, shares one mem control block
  232. // if ( NULL == (msgq = (core_msgq_t*) core_mmgr_new(sizeof ( core_msgq_t ) + plsz)) ) {
  233. // goto ERR_MMGR;
  234. // }
  235. // // initialize msgq control block
  236. // msgq->msgq_msgs.sque_pool = ((uint8*) msgq + sizeof ( core_msgq_t ));
  237. // msgq->msgq_msgs.size_pool = plsz;
  238. // msgq->msgq_msgs.size_qdat = size;
  239. // //msgq->sque_msgs
  240. // if ( TRUE != core_msgq_ini(msgq) ) {
  241. // goto ERR_MSGQ;
  242. // }
  243. // goto ERR_SUCC;
  244. // ERR_MSGQ:
  245. // core_mmgr_del(msgq);
  246. // msgq = NULL;
  247. // ERR_MMGR:
  248. // ERR_SUCC:
  249. // log_exi("RSLT>>msgq:0x%08x", msgq);
  250. // return ( msgq );
  251. // }
  252. // int32
  253. // core_msgq_del(core_msgq_t* msgq)
  254. // {
  255. // return ( core_mmgr_del(msgq) );
  256. // }
  257. // int32
  258. // core_msgq_pop(core_msgq_t* msgq, void* data)
  259. // {
  260. // return ( core_sque_pop(&msgq->msgq_msgs, data) );
  261. // }
  262. // int32
  263. // core_msgq_psh(core_msgq_t* msgq, const void* data)
  264. // {
  265. // return ( core_sque_psh(&msgq->msgq_msgs, data) );
  266. // }