posix_signal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * File : signal.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017/10/1 Bernard The first version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <time.h>
  27. #include "posix_signal.h"
  28. #define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
  29. void (*signal(int sig, void (*func)(int))) (int)
  30. {
  31. return rt_signal_install(sig, func);
  32. }
  33. int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
  34. {
  35. rt_base_t level;
  36. rt_thread_t tid;
  37. tid = rt_thread_self();
  38. level = rt_hw_interrupt_disable();
  39. if (oset) *oset = tid->sig_mask;
  40. if (set)
  41. {
  42. switch(how)
  43. {
  44. case SIG_BLOCK:
  45. tid->sig_mask |= *set;
  46. break;
  47. case SIG_UNBLOCK:
  48. tid->sig_mask &= ~*set;
  49. break;
  50. case SIG_SETMASK:
  51. tid->sig_mask = *set;
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. rt_hw_interrupt_enable(level);
  58. return 0;
  59. }
  60. int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
  61. {
  62. rt_sighandler_t old = RT_NULL;
  63. if (!sig_valid(signum)) return -RT_ERROR;
  64. if (act)
  65. old = rt_signal_install(signum, act->sa_handler);
  66. else
  67. {
  68. old = rt_signal_install(signum, RT_NULL);
  69. rt_signal_install(signum, old);
  70. }
  71. if (oldact)
  72. oldact->sa_handler = old;
  73. return 0;
  74. }
  75. int sigtimedwait(const sigset_t *set, siginfo_t *info,
  76. const struct timespec *timeout)
  77. {
  78. int ret = 0;
  79. int tick = RT_WAITING_FOREVER;
  80. #ifdef RT_USING_PTHREADS
  81. if (timeout)
  82. {
  83. extern int clock_time_to_tick(const struct timespec *time);
  84. tick = clock_time_to_tick(timeout);
  85. }
  86. #endif
  87. ret = rt_signal_wait(set, info, tick);
  88. if (ret == 0) return 0;
  89. errno = ret;
  90. return -1;
  91. }
  92. int sigwait(const sigset_t *set, int *sig)
  93. {
  94. siginfo_t si;
  95. if (sigtimedwait(set, &si, 0) < 0)
  96. return -1;
  97. *sig = si.si_signo;
  98. return 0;
  99. }
  100. int sigwaitinfo(const sigset_t *set, siginfo_t *info)
  101. {
  102. return sigtimedwait(set, info, NULL);
  103. }
  104. int raise(int sig)
  105. {
  106. rt_thread_kill(rt_thread_self(), sig);
  107. return 0;
  108. }