sig_pselect.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /****************************************************************************
  2. * sched/signal/sig_pselect.c
  3. *
  4. * Copyright (C) 2018 Pinecone Inc. All rights reserved.
  5. * Author: Xiang Xiao <xiaoxiang@pinecone.net>
  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/select.h>
  40. #include <sys/time.h>
  41. #include "sched/sched.h"
  42. #include "signal/signal.h"
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: pselect
  48. *
  49. * Description:
  50. * pselect() allows a program to monitor multiple file descriptors, waiting
  51. * until one or more of the file descriptors become "ready" for some class
  52. * of I/O operation (e.g., input possible). A file descriptor is
  53. * considered ready if it is possible to perform the corresponding I/O
  54. * operation (e.g., read(2)) without blocking.
  55. *
  56. * Input Parameters:
  57. * nfds - the maximum fd number (+1) of any descriptor in any of the
  58. * three sets.
  59. * readfds - the set of descriptions to monitor for read-ready events
  60. * writefds - the set of descriptions to monitor for write-ready events
  61. * exceptfds - the set of descriptions to monitor for error events
  62. * timeout - Return at this time if none of these events of interest
  63. * occur.
  64. * sigmask - Replace the current signal mask temporarily during execution
  65. *
  66. * Returned Value:
  67. * 0: Timer expired
  68. * >0: The number of bits set in the three sets of descriptors
  69. * -1: An error occurred (errno will be set appropriately)
  70. *
  71. ****************************************************************************/
  72. int pselect(int nfds, FAR fd_set *readfds, FAR fd_set *writefds,
  73. FAR fd_set *exceptfds, FAR const struct timespec *timeout,
  74. FAR const sigset_t *sigmask)
  75. {
  76. FAR struct tcb_s *rtcb = this_task();
  77. sigset_t saved_sigprocmask;
  78. irqstate_t flags;
  79. int ret = ERROR;
  80. /* Several operations must be performed below: We must determine if any
  81. * signal is pending and, if not, wait for the signal. Since signals can
  82. * be posted from the interrupt level, there is a race condition that
  83. * can only be eliminated by disabling interrupts!
  84. */
  85. flags = enter_critical_section();
  86. /* Save a copy of the old sigprocmask and install
  87. * the new (temporary) sigprocmask
  88. */
  89. saved_sigprocmask = rtcb->sigprocmask;
  90. if (sigmask)
  91. {
  92. rtcb->sigprocmask = *sigmask;
  93. }
  94. rtcb->sigwaitmask = NULL_SIGNAL_SET;
  95. /* Check if there is a pending signal corresponding to one of the
  96. * signals that will be unblocked by the new sigprocmask.
  97. */
  98. if (nxsig_unmask_pendingsignal())
  99. {
  100. /* Dispatching one or more of the signals is sufficient to cause
  101. * us to not wait. Restore the original sigprocmask.
  102. */
  103. rtcb->sigprocmask = saved_sigprocmask;
  104. leave_critical_section(flags);
  105. set_errno(EINTR);
  106. }
  107. else
  108. {
  109. FAR struct timeval *timeval = NULL;
  110. struct timeval timeval_buf;
  111. /* And call select to do the real work */
  112. if (timeout)
  113. {
  114. timeval_buf.tv_sec = timeout->tv_sec;
  115. timeval_buf.tv_usec = timeout->tv_nsec / 1000;
  116. timeval = &timeval_buf;
  117. }
  118. ret = select(nfds, readfds, writefds, exceptfds, timeval);
  119. /* We are running again, restore the original sigprocmask */
  120. rtcb->sigprocmask = saved_sigprocmask;
  121. leave_critical_section(flags);
  122. /* Now, handle the (rare?) case where (a) a blocked signal was received
  123. * while the task was suspended but (b) restoring the original
  124. * sigprocmask will unblock the signal.
  125. */
  126. nxsig_unmask_pendingsignal();
  127. }
  128. return ret;
  129. }