lwp_itimer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-30 Shell Add itimer support
  9. */
  10. #define _GNU_SOURCE
  11. #include <sys/time.h>
  12. #undef _GNU_SOURCE
  13. #define DBG_TAG "lwp.signal"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <string.h>
  19. #include "lwp_internal.h"
  20. #include "sys/signal.h"
  21. #include "syscall_generic.h"
  22. rt_err_t lwp_signal_setitimer(rt_lwp_t lwp, int which, const struct itimerspec *restrict new, struct itimerspec *restrict old)
  23. {
  24. rt_err_t rc = RT_EOK;
  25. timer_t timerid = 0;
  26. int flags = 0;
  27. if (lwp->signal.real_timer == LWP_SIG_INVALID_TIMER)
  28. {
  29. struct sigevent sevp = {
  30. .sigev_signo = SIGALRM,
  31. .sigev_notify = SIGEV_SIGNAL,
  32. };
  33. rc = timer_create(CLOCK_REALTIME_ALARM, &sevp, &timerid);
  34. if (rc == RT_EOK)
  35. {
  36. RT_ASSERT(timerid != LWP_SIG_INVALID_TIMER);
  37. lwp->signal.real_timer = timerid;
  38. }
  39. else
  40. {
  41. /* failed to create timer */
  42. }
  43. }
  44. else
  45. {
  46. timerid = lwp->signal.real_timer;
  47. }
  48. if (rc == RT_EOK)
  49. {
  50. switch (which)
  51. {
  52. case ITIMER_REAL:
  53. rc = timer_settime(timerid, flags, new, old);
  54. break;
  55. default:
  56. rc = -ENOSYS;
  57. LOG_W("%s() unsupported timer", __func__);
  58. break;
  59. }
  60. }
  61. return rc;
  62. }