mq_timedsend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /****************************************************************************
  2. * sched/mqueue/mq_timedsend.c
  3. *
  4. * Copyright (C) 2007-2009, 2011, 2013-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <unistd.h>
  43. #include <mqueue.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <nuttx/irq.h>
  47. #include <nuttx/arch.h>
  48. #include <nuttx/wdog.h>
  49. #include <nuttx/cancelpt.h>
  50. #include "clock/clock.h"
  51. #include "sched/sched.h"
  52. #include "mqueue/mqueue.h"
  53. /****************************************************************************
  54. * Private Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: nxmq_sndtimeout
  58. *
  59. * Description:
  60. * This function is called if the timeout elapses before the message queue
  61. * becomes non-full.
  62. *
  63. * Input Parameters:
  64. * argc - the number of arguments (should be 1)
  65. * pid - the task ID of the task to wakeup
  66. *
  67. * Returned Value:
  68. * None
  69. *
  70. * Assumptions:
  71. *
  72. ****************************************************************************/
  73. static void nxmq_sndtimeout(int argc, wdparm_t pid)
  74. {
  75. FAR struct tcb_s *wtcb;
  76. irqstate_t flags;
  77. /* Disable interrupts. This is necessary because an interrupt handler may
  78. * attempt to send a message while we are doing this.
  79. */
  80. flags = enter_critical_section();
  81. /* Get the TCB associated with this pid. It is possible that task may no
  82. * longer be active when this watchdog goes off.
  83. */
  84. wtcb = sched_gettcb((pid_t)pid);
  85. /* It is also possible that an interrupt/context switch beat us to the
  86. * punch and already changed the task's state.
  87. */
  88. if (wtcb != NULL && wtcb->task_state == TSTATE_WAIT_MQNOTFULL)
  89. {
  90. /* Restart with task with a timeout error */
  91. nxmq_wait_irq(wtcb, ETIMEDOUT);
  92. }
  93. /* Interrupts may now be re-enabled. */
  94. leave_critical_section(flags);
  95. }
  96. /****************************************************************************
  97. * Public Functions
  98. ****************************************************************************/
  99. /****************************************************************************
  100. * Name: nxmq_timedsend
  101. *
  102. * Description:
  103. * This function adds the specified message (msg) to the message queue
  104. * (mqdes). nxmq_timedsend() behaves just like mq_send(), except
  105. * that if the queue is full and the O_NONBLOCK flag is not enabled for
  106. * the message queue description, then abstime points to a structure which
  107. * specifies a ceiling on the time for which the call will block.
  108. *
  109. * nxmq_timedsend() is functionally equivalent to mq_timedsend() except
  110. * that:
  111. *
  112. * - It is not a cancellation point, and
  113. * - It does not modify the errno value.
  114. *
  115. * See comments with mq_timedsend() for a more complete description of the
  116. * behavior of this function
  117. *
  118. * Input Parameters:
  119. * mqdes - Message queue descriptor
  120. * msg - Message to send
  121. * msglen - The length of the message in bytes
  122. * prio - The priority of the message
  123. * abstime - the absolute time to wait until a timeout is decleared
  124. *
  125. * Returned Value:
  126. * This is an internal OS interface and should not be used by applications.
  127. * It follows the NuttX internal error return policy: Zero (OK) is
  128. * returned on success. A negated errno value is returned on failure.
  129. * (see mq_timedsend() for the list list valid return values).
  130. *
  131. * EAGAIN The queue was empty, and the O_NONBLOCK flag was set for the
  132. * message queue description referred to by mqdes.
  133. * EINVAL Either msg or mqdes is NULL or the value of prio is invalid.
  134. * EPERM Message queue opened not opened for writing.
  135. * EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
  136. * message queue.
  137. * EINTR The call was interrupted by a signal handler.
  138. *
  139. ****************************************************************************/
  140. int nxmq_timedsend(mqd_t mqdes, FAR const char *msg, size_t msglen,
  141. unsigned int prio, FAR const struct timespec *abstime)
  142. {
  143. FAR struct tcb_s *rtcb = this_task();
  144. FAR struct mqueue_inode_s *msgq;
  145. FAR struct mqueue_msg_s *mqmsg = NULL;
  146. irqstate_t flags;
  147. sclock_t ticks;
  148. int result;
  149. int ret;
  150. DEBUGASSERT(up_interrupt_context() == false && rtcb->waitdog == NULL);
  151. /* Verify the input parameters on any failures to verify. */
  152. ret = nxmq_verify_send(mqdes, msg, msglen, prio);
  153. if (ret < 0)
  154. {
  155. return ret;
  156. }
  157. /* Pre-allocate a message structure */
  158. mqmsg = nxmq_alloc_msg();
  159. if (mqmsg == NULL)
  160. {
  161. /* Failed to allocate the message. nxmq_alloc_msg() does not set the
  162. * errno value.
  163. */
  164. return -ENOMEM;
  165. }
  166. /* Get a pointer to the message queue */
  167. sched_lock();
  168. msgq = mqdes->msgq;
  169. /* OpenGroup.org: "Under no circumstance shall the operation fail with a
  170. * timeout if there is sufficient room in the queue to add the message
  171. * immediately. The validity of the abstime parameter need not be checked
  172. * when there is sufficient room in the queue."
  173. *
  174. * Also ignore the time value if for some crazy reason we were called from
  175. * an interrupt handler. This probably really should be an assertion.
  176. *
  177. * NOTE: There is a race condition here: What if a message is added by
  178. * interrupt related logic so that queue again becomes non-empty. That
  179. * is handled because nxmq_do_send() will permit the maxmsgs limit to be
  180. * exceeded in that case.
  181. */
  182. if (msgq->nmsgs < msgq->maxmsgs || up_interrupt_context())
  183. {
  184. /* Do the send with no further checks (possibly exceeding maxmsgs)
  185. * Currently nxmq_do_send() always returns OK.
  186. */
  187. ret = nxmq_do_send(mqdes, mqmsg, msg, msglen, prio);
  188. sched_unlock();
  189. return ret;
  190. }
  191. /* The message queue is full... We are going to wait. Now we must have a
  192. * valid time value.
  193. */
  194. if (!abstime || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  195. {
  196. ret = -EINVAL;
  197. goto errout_with_mqmsg;
  198. }
  199. /* Create a watchdog. We will not actually need this watchdog
  200. * unless the queue is full, but we will reserve it up front
  201. * before we enter the following critical section.
  202. */
  203. rtcb->waitdog = wd_create();
  204. if (!rtcb->waitdog)
  205. {
  206. ret = -EINVAL;
  207. goto errout_with_mqmsg;
  208. }
  209. /* We are not in an interrupt handler and the message queue is full.
  210. * Set up a timed wait for the message queue to become non-full.
  211. *
  212. * Convert the timespec to clock ticks. We must have interrupts
  213. * disabled here so that this time stays valid until the wait begins.
  214. */
  215. flags = enter_critical_section();
  216. result = clock_abstime2ticks(CLOCK_REALTIME, abstime, &ticks);
  217. /* If the time has already expired and the message queue is empty,
  218. * return immediately.
  219. */
  220. if (result == OK && ticks <= 0)
  221. {
  222. result = ETIMEDOUT;
  223. }
  224. /* Handle any time-related errors */
  225. if (result != OK)
  226. {
  227. ret = -result;
  228. goto errout_in_critical_section;
  229. }
  230. /* Start the watchdog and begin the wait for MQ not full */
  231. (void)wd_start(rtcb->waitdog, ticks, (wdentry_t)nxmq_sndtimeout,
  232. 1, getpid());
  233. /* And wait for the message queue to be non-empty */
  234. ret = nxmq_wait_send(mqdes);
  235. /* This may return with an error and errno set to either EINTR
  236. * or ETIMEOUT. Cancel the watchdog timer in any event.
  237. */
  238. wd_cancel(rtcb->waitdog);
  239. /* Check if nxmq_wait_send() failed */
  240. if (ret < 0)
  241. {
  242. /* nxmq_wait_send() failed. */
  243. goto errout_in_critical_section;
  244. }
  245. /* That is the end of the atomic operations */
  246. leave_critical_section(flags);
  247. /* If any of the above failed, set the errno. Otherwise, there should
  248. * be space for another message in the message queue. NOW we can allocate
  249. * the message structure.
  250. *
  251. * Currently nxmq_do_send() always returns OK.
  252. */
  253. ret = nxmq_do_send(mqdes, mqmsg, msg, msglen, prio);
  254. sched_unlock();
  255. wd_delete(rtcb->waitdog);
  256. rtcb->waitdog = NULL;
  257. leave_cancellation_point();
  258. return ret;
  259. /* Exit here with (1) the scheduler locked, (2) a message allocated, (3) a
  260. * wdog allocated, and (4) interrupts disabled.
  261. */
  262. errout_in_critical_section:
  263. leave_critical_section(flags);
  264. wd_delete(rtcb->waitdog);
  265. rtcb->waitdog = NULL;
  266. /* Exit here with (1) the scheduler locked and 2) a message allocated. The
  267. * error code is in 'result'
  268. */
  269. errout_with_mqmsg:
  270. nxmq_free_msg(mqmsg);
  271. sched_unlock();
  272. return ret;
  273. }
  274. /****************************************************************************
  275. * Name: mq_timedsend
  276. *
  277. * Description:
  278. * This function adds the specified message (msg) to the message queue
  279. * (mqdes). The "msglen" parameter specifies the length of the message
  280. * in bytes pointed to by "msg." This length must not exceed the maximum
  281. * message length from the mq_getattr().
  282. *
  283. * If the message queue is not full, mq_timedsend() place the message in the
  284. * message queue at the position indicated by the "prio" argrument.
  285. * Messages with higher priority will be inserted before lower priority
  286. * messages. The value of "prio" must not exceed MQ_PRIO_MAX.
  287. *
  288. * If the specified message queue is full and O_NONBLOCK is not set in the
  289. * message queue, then mq_timedsend() will block until space becomes available
  290. * to the queue the message or a timeout occurs.
  291. *
  292. * mq_timedsend() behaves just like mq_send(), except that if the queue
  293. * is full and the O_NONBLOCK flag is not enabled for the message queue
  294. * description, then abstime points to a structure which specifies a
  295. * ceiling on the time for which the call will block. This ceiling is an
  296. * absolute timeout in seconds and nanoseconds since the Epoch (midnight
  297. * on the morning of 1 January 1970).
  298. *
  299. * If the message queue is full, and the timeout has already expired by
  300. * the time of the call, mq_timedsend() returns immediately.
  301. *
  302. * Input Parameters:
  303. * mqdes - Message queue descriptor
  304. * msg - Message to send
  305. * msglen - The length of the message in bytes
  306. * prio - The priority of the message
  307. * abstime - the absolute time to wait until a timeout is decleared
  308. *
  309. * Returned Value:
  310. * On success, mq_send() returns 0 (OK); on error, -1 (ERROR)
  311. * is returned, with errno set to indicate the error:
  312. *
  313. * EAGAIN The queue was empty, and the O_NONBLOCK flag was set for the
  314. * message queue description referred to by mqdes.
  315. * EINVAL Either msg or mqdes is NULL or the value of prio is invalid.
  316. * EPERM Message queue opened not opened for writing.
  317. * EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
  318. * message queue.
  319. * EINTR The call was interrupted by a signal handler.
  320. *
  321. * Assumptions/restrictions:
  322. *
  323. ****************************************************************************/
  324. int mq_timedsend(mqd_t mqdes, FAR const char *msg, size_t msglen,
  325. unsigned int prio, FAR const struct timespec *abstime)
  326. {
  327. int ret;
  328. /* mq_timedsend() is a cancellation point */
  329. (void)enter_cancellation_point();
  330. /* Let nxmq_send() do all of the work */
  331. ret = nxmq_timedsend(mqdes, msg, msglen, prio, abstime);
  332. if (ret < 0)
  333. {
  334. set_errno(-ret);
  335. ret = ERROR;
  336. }
  337. leave_cancellation_point();
  338. return ret;
  339. }