Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vcos_msgqueue.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are 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 copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "vcos.h"
  26. #include "vcos_msgqueue.h"
  27. #include <stddef.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #define MAGIC VCOS_MSGQ_MAGIC
  31. /* Probably a good idea for MSG_T to be multiple of 8 so that doubles
  32. * are naturally aligned without problem.
  33. */
  34. vcos_static_assert((sizeof(VCOS_MSG_T) & 7) == 0);
  35. static void vcos_msgq_pool_on_reply(VCOS_MSG_WAITER_T *waiter,
  36. VCOS_MSG_T *msg);
  37. static void vcos_msgq_queue_waiter_on_reply(VCOS_MSG_WAITER_T *waiter,
  38. VCOS_MSG_T *msg);
  39. /** Simple reply protocol. The client creates a semaphore and waits
  40. * for it. No queuing of multiple replies is possible but nothing needs
  41. * to be setup in advance. Because creating semaphores is very fast on
  42. * VideoCore there's no need to do anything elaborate to optimize create
  43. * time - this might need revisiting on other platforms.
  44. */
  45. typedef struct
  46. {
  47. VCOS_MSG_WAITER_T waiter;
  48. VCOS_SEMAPHORE_T waitsem;
  49. } VCOS_MSG_SIMPLE_WAITER_T;
  50. static void vcos_msgq_simple_waiter_on_reply(VCOS_MSG_WAITER_T *waiter,
  51. VCOS_MSG_T *msg)
  52. {
  53. VCOS_MSG_SIMPLE_WAITER_T *self;
  54. (void)msg;
  55. self = (VCOS_MSG_SIMPLE_WAITER_T*)waiter;
  56. vcos_semaphore_post(&self->waitsem);
  57. }
  58. static VCOS_STATUS_T vcos_msgq_simple_waiter_init(VCOS_MSG_SIMPLE_WAITER_T *waiter)
  59. {
  60. VCOS_STATUS_T status;
  61. status = vcos_semaphore_create(&waiter->waitsem, "waiter", 0);
  62. waiter->waiter.on_reply = vcos_msgq_simple_waiter_on_reply;
  63. return status;
  64. }
  65. static void vcos_msgq_simple_waiter_deinit(VCOS_MSG_SIMPLE_WAITER_T *waiter)
  66. {
  67. vcos_semaphore_delete(&waiter->waitsem);
  68. }
  69. /*
  70. * Message queues
  71. */
  72. static VCOS_STATUS_T vcos_msgq_create_internal(VCOS_MSGQUEUE_T *q, const char *name)
  73. {
  74. VCOS_STATUS_T st;
  75. memset(q, 0, sizeof(*q));
  76. q->waiter.on_reply = vcos_msgq_queue_waiter_on_reply;
  77. st = vcos_semaphore_create(&q->sem, name, 0);
  78. if (st != VCOS_SUCCESS)
  79. goto fail_sem;
  80. st = vcos_mutex_create(&q->lock, name);
  81. if (st != VCOS_SUCCESS)
  82. goto fail_mtx;
  83. return st;
  84. fail_mtx:
  85. vcos_semaphore_delete(&q->sem);
  86. fail_sem:
  87. return st;
  88. }
  89. static void vcos_msgq_delete_internal(VCOS_MSGQUEUE_T *q)
  90. {
  91. vcos_semaphore_delete(&q->sem);
  92. vcos_mutex_delete(&q->lock);
  93. }
  94. VCOS_STATUS_T vcos_msgq_create(VCOS_MSGQUEUE_T *q, const char *name)
  95. {
  96. VCOS_STATUS_T st;
  97. st = vcos_msgq_create_internal(q, name);
  98. return st;
  99. }
  100. void vcos_msgq_delete(VCOS_MSGQUEUE_T *q)
  101. {
  102. vcos_msgq_delete_internal(q);
  103. }
  104. /* append a message to a message queue */
  105. static _VCOS_INLINE void msgq_append(VCOS_MSGQUEUE_T *q, VCOS_MSG_T *msg)
  106. {
  107. vcos_mutex_lock(&q->lock);
  108. if (q->head == NULL)
  109. {
  110. q->head = q->tail = msg;
  111. }
  112. else
  113. {
  114. q->tail->next = msg;
  115. q->tail = msg;
  116. }
  117. vcos_mutex_unlock(&q->lock);
  118. }
  119. /*
  120. * A waiter for a message queue. Just appends the message to the
  121. * queue, waking up the waiting thread.
  122. */
  123. static void vcos_msgq_queue_waiter_on_reply(VCOS_MSG_WAITER_T *waiter,
  124. VCOS_MSG_T *msg)
  125. {
  126. VCOS_MSGQUEUE_T *queue = (VCOS_MSGQUEUE_T*)waiter;
  127. msgq_append(queue, msg);
  128. vcos_semaphore_post(&queue->sem);
  129. }
  130. /* initialise this library */
  131. VCOS_STATUS_T vcos_msgq_init(void)
  132. {
  133. return VCOS_SUCCESS;
  134. }
  135. void vcos_msgq_deinit(void)
  136. {
  137. }
  138. static _VCOS_INLINE
  139. void vcos_msg_send_helper(VCOS_MSG_WAITER_T *waiter,
  140. VCOS_MSGQUEUE_T *dest,
  141. uint32_t code,
  142. VCOS_MSG_T *msg)
  143. {
  144. vcos_assert(msg);
  145. vcos_assert(dest);
  146. msg->code = code;
  147. if (waiter)
  148. msg->waiter = waiter;
  149. msg->next = NULL;
  150. msg->src_thread = vcos_thread_current();
  151. msgq_append(dest, msg);
  152. vcos_semaphore_post(&dest->sem);
  153. }
  154. /* wait on a queue for a message */
  155. VCOS_MSG_T *vcos_msg_wait(VCOS_MSGQUEUE_T *queue)
  156. {
  157. VCOS_MSG_T *msg;
  158. vcos_semaphore_wait(&queue->sem);
  159. vcos_mutex_lock(&queue->lock);
  160. msg = queue->head;
  161. vcos_assert(msg); /* should always be a message here! */
  162. queue->head = msg->next;
  163. if (queue->head == NULL)
  164. queue->tail = NULL;
  165. vcos_mutex_unlock(&queue->lock);
  166. return msg;
  167. }
  168. /* peek on a queue for a message */
  169. VCOS_MSG_T *vcos_msg_peek(VCOS_MSGQUEUE_T *queue)
  170. {
  171. VCOS_MSG_T *msg;
  172. vcos_mutex_lock(&queue->lock);
  173. msg = queue->head;
  174. /* if there's a message, remove it from the queue */
  175. if (msg)
  176. {
  177. queue->head = msg->next;
  178. if (queue->head == NULL)
  179. queue->tail = NULL;
  180. /* keep the semaphore count consistent */
  181. /* coverity[lock_order]
  182. * the semaphore must have a non-zero count so cannot block here.
  183. */
  184. vcos_semaphore_wait(&queue->sem);
  185. }
  186. vcos_mutex_unlock(&queue->lock);
  187. return msg;
  188. }
  189. void vcos_msg_send(VCOS_MSGQUEUE_T *dest, uint32_t code, VCOS_MSG_T *msg)
  190. {
  191. vcos_assert(msg->magic == MAGIC);
  192. vcos_msg_send_helper(NULL, dest, code, msg);
  193. }
  194. /** Send on to the target queue, then wait on a simple waiter for the reply
  195. */
  196. VCOS_STATUS_T vcos_msg_sendwait(VCOS_MSGQUEUE_T *dest, uint32_t code, VCOS_MSG_T *msg)
  197. {
  198. VCOS_STATUS_T st;
  199. VCOS_MSG_SIMPLE_WAITER_T waiter;
  200. vcos_assert(msg->magic == MAGIC);
  201. /* if this fires, you've set a waiter up but are now about to obliterate it
  202. * with the 'wait for a reply' waiter.
  203. */
  204. vcos_assert(msg->waiter == NULL);
  205. if ((st=vcos_msgq_simple_waiter_init(&waiter)) != VCOS_SUCCESS)
  206. return st;
  207. vcos_msg_send_helper(&waiter.waiter, dest, code, msg);
  208. vcos_semaphore_wait(&waiter.waitsem);
  209. vcos_msgq_simple_waiter_deinit(&waiter);
  210. return VCOS_SUCCESS;
  211. }
  212. /** Send a reply to a message
  213. */
  214. void vcos_msg_reply(VCOS_MSG_T *msg)
  215. {
  216. vcos_assert(msg->magic == MAGIC);
  217. msg->code |= MSG_REPLY_BIT;
  218. if (msg->waiter)
  219. {
  220. msg->waiter->on_reply(msg->waiter, msg);
  221. }
  222. else
  223. {
  224. VCOS_ALERT("%s: reply to non-reply message id %d",
  225. VCOS_FUNCTION,
  226. msg->code);
  227. vcos_assert(0);
  228. }
  229. }
  230. void vcos_msg_set_source(VCOS_MSG_T *msg, VCOS_MSGQUEUE_T *queue)
  231. {
  232. vcos_assert(msg);
  233. vcos_assert(msg->magic == MAGIC);
  234. vcos_assert(queue);
  235. msg->waiter = &queue->waiter;
  236. }
  237. /*
  238. * Message pools
  239. */
  240. VCOS_STATUS_T vcos_msgq_pool_create(VCOS_MSGQ_POOL_T *pool,
  241. size_t count,
  242. size_t payload_size,
  243. const char *name)
  244. {
  245. VCOS_STATUS_T status;
  246. int bp_size = payload_size + sizeof(VCOS_MSG_T);
  247. status = vcos_blockpool_create_on_heap(&pool->blockpool,
  248. count, bp_size,
  249. VCOS_BLOCKPOOL_ALIGN_DEFAULT,
  250. 0,
  251. name);
  252. if (status != VCOS_SUCCESS)
  253. goto fail_pool;
  254. status = vcos_semaphore_create(&pool->sem, name, count);
  255. if (status != VCOS_SUCCESS)
  256. goto fail_sem;
  257. pool->waiter.on_reply = vcos_msgq_pool_on_reply;
  258. pool->magic = MAGIC;
  259. return status;
  260. fail_sem:
  261. vcos_blockpool_delete(&pool->blockpool);
  262. fail_pool:
  263. return status;
  264. }
  265. void vcos_msgq_pool_delete(VCOS_MSGQ_POOL_T *pool)
  266. {
  267. vcos_blockpool_delete(&pool->blockpool);
  268. vcos_semaphore_delete(&pool->sem);
  269. }
  270. /** Called when a message from a pool is replied-to. Just returns
  271. * the message back to the blockpool.
  272. */
  273. static void vcos_msgq_pool_on_reply(VCOS_MSG_WAITER_T *waiter,
  274. VCOS_MSG_T *msg)
  275. {
  276. vcos_unused(waiter);
  277. vcos_assert(msg->magic == MAGIC);
  278. vcos_msgq_pool_free(msg);
  279. }
  280. VCOS_MSG_T *vcos_msgq_pool_alloc(VCOS_MSGQ_POOL_T *pool)
  281. {
  282. VCOS_MSG_T *msg;
  283. if (vcos_semaphore_trywait(&pool->sem) == VCOS_SUCCESS)
  284. {
  285. msg = vcos_blockpool_calloc(&pool->blockpool);
  286. vcos_assert(msg);
  287. msg->magic = MAGIC;
  288. msg->waiter = &pool->waiter;
  289. msg->pool = pool;
  290. }
  291. else
  292. {
  293. msg = NULL;
  294. }
  295. return msg;
  296. }
  297. void vcos_msgq_pool_free(VCOS_MSG_T *msg)
  298. {
  299. if (msg)
  300. {
  301. VCOS_MSGQ_POOL_T *pool;
  302. vcos_assert(msg->pool);
  303. pool = msg->pool;
  304. vcos_assert(msg->pool->magic == MAGIC);
  305. vcos_blockpool_free(msg);
  306. vcos_semaphore_post(&pool->sem);
  307. }
  308. }
  309. VCOS_MSG_T *vcos_msgq_pool_wait(VCOS_MSGQ_POOL_T *pool)
  310. {
  311. VCOS_MSG_T *msg;
  312. vcos_semaphore_wait(&pool->sem);
  313. msg = vcos_blockpool_calloc(&pool->blockpool);
  314. vcos_assert(msg);
  315. msg->magic = MAGIC;
  316. msg->waiter = &pool->waiter;
  317. msg->pool = pool;
  318. return msg;
  319. }
  320. void vcos_msg_init(VCOS_MSG_T *msg)
  321. {
  322. msg->magic = MAGIC;
  323. msg->next = NULL;
  324. msg->waiter = NULL;
  325. msg->pool = NULL;
  326. }