mq_notify.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /****************************************************************************
  2. * sched/mqueue/mq_notify.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011, 2013, 2015 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 <mqueue.h>
  40. #include <sched.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <nuttx/sched.h>
  44. #include "sched/sched.h"
  45. #include "mqueue/mqueue.h"
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: mq_notify
  51. *
  52. * Description:
  53. * If "notification" is not NULL, this function connects the task with
  54. * the message queue such that the specified signal will be sent to the
  55. * task whenever the message changes from empty to non-empty. Only one
  56. * notification can be attached to a message queue.
  57. *
  58. * If "notification" is NULL, the attached notification is detached (if
  59. * it was held by the calling task) and the queue is available to attach
  60. * another notification.
  61. *
  62. * When the notification is sent to the registered process, its
  63. * registration will be removed. The message queue will then be
  64. * available for registration.
  65. *
  66. * Input Parameters:
  67. * mqdes - Message queue descriptor
  68. * notification - Real-time signal structure containing:
  69. * sigev_notify - Should be SIGEV_SIGNAL or SIGEV_THREAD
  70. * sigev_signo - The signo to use for the notification
  71. * sigev_value - Value associated with the signal
  72. *
  73. * Returned Value:
  74. * On success mq_notify() returns 0; on error, -1 is returned, with
  75. * errno set to indicate the error.
  76. *
  77. * EBADF The descriptor specified in mqdes is invalid.
  78. * EBUSY Another process has already registered to receive notification
  79. * for this message queue.
  80. * EINVAL sevp->sigev_notify is not one of the permitted values; or
  81. * sevp->sigev_notify is SIGEV_SIGNAL and sevp->sigev_signo is not a
  82. * valid signal number.
  83. * ENOMEM
  84. * Insufficient memory.
  85. *
  86. * Assumptions:
  87. *
  88. * POSIX Compatibility:
  89. * int mq_notify(mqd_t mqdes, const struct sigevent *notification);
  90. *
  91. * The notification will be sent to the registered task even if another
  92. * task is waiting for the message queue to become non-empty. This is
  93. * inconsistent with the POSIX specification which says, "If a process
  94. * has registered for notification of message a arrival at a message
  95. * queue and some process is blocked in [nx]mq_receive() waiting to receive
  96. * a message when a message arrives at the queue, the arriving message
  97. * message shall satisfy [nx]mq_receive()... The resulting behavior is as
  98. * if the message queue remains empty, and no notification shall be sent."
  99. *
  100. ****************************************************************************/
  101. int mq_notify(mqd_t mqdes, FAR const struct sigevent *notification)
  102. {
  103. FAR struct tcb_s *rtcb;
  104. FAR struct mqueue_inode_s *msgq;
  105. int errval;
  106. /* Was a valid message queue descriptor provided? */
  107. if (!mqdes)
  108. {
  109. /* No.. return EBADF */
  110. errval = EBADF;
  111. goto errout;
  112. }
  113. /* Get a pointer to the message queue */
  114. sched_lock();
  115. msgq = mqdes->msgq;
  116. /* Get the current process ID */
  117. rtcb = this_task();
  118. /* Is there already a notification attached */
  119. if (!msgq->ntmqdes)
  120. {
  121. /* No... Have we been asked to establish one? */
  122. if (notification)
  123. {
  124. /* Yes... Was a valid signal number supplied? */
  125. if (!GOOD_SIGNO(notification->sigev_signo))
  126. {
  127. /* No... Return EINVAL */
  128. errval = EINVAL;
  129. goto errout;
  130. }
  131. /* Yes... Assign it to the current task. */
  132. memcpy(&msgq->ntevent, notification,
  133. sizeof(struct sigevent));
  134. msgq->ntpid = rtcb->pid;
  135. msgq->ntmqdes = mqdes;
  136. }
  137. }
  138. /* Yes... a notification is attached. Does this task own it?
  139. * Is it trying to remove it?
  140. */
  141. else if ((msgq->ntpid != rtcb->pid) || (notification))
  142. {
  143. /* This thread does not own the notification OR it is
  144. * not trying to remove it. Return EBUSY.
  145. */
  146. errval = EBUSY;
  147. goto errout;
  148. }
  149. else
  150. {
  151. /* Yes, the notification belongs to this thread. Allow the
  152. * thread to detach the notification.
  153. */
  154. memset(&msgq->ntevent, 0, sizeof(struct sigevent));
  155. msgq->ntpid = INVALID_PROCESS_ID;
  156. msgq->ntmqdes = NULL;
  157. nxsig_cancel_notification(&msgq->ntwork);
  158. }
  159. sched_unlock();
  160. return OK;
  161. errout:
  162. set_errno(errval);
  163. sched_unlock();
  164. return ERROR;
  165. }