shim_sleep.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_sleep.c
  15. *
  16. * Implementation of system call "pause" and "nanosleep".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_handle.h>
  22. #include <shim_vma.h>
  23. #include <shim_thread.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <errno.h>
  27. static bool signal_pending (void)
  28. {
  29. struct shim_thread* cur = get_cur_thread();
  30. if (!cur)
  31. return false;
  32. lock(&cur->lock);
  33. if (!cur->signal_logs || !cur->has_signal.counter) {
  34. unlock(&cur->lock);
  35. return false;
  36. }
  37. for (int sig = 1; sig <= NUM_SIGS; sig++) {
  38. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  39. atomic_read(&cur->signal_logs[sig - 1].tail)) {
  40. /* at least one signal of type sig... */
  41. if (!__sigismember(&cur->signal_mask, sig)) {
  42. /* ...and this type is not blocked */
  43. unlock(&cur->lock);
  44. return true;
  45. }
  46. }
  47. }
  48. unlock(&cur->lock);
  49. return false;
  50. }
  51. int shim_do_pause (void)
  52. {
  53. if (signal_pending())
  54. return -EINTR;
  55. /* ~0ULL micro sec ~= 805675 years */
  56. DkThreadDelayExecution(~((PAL_NUM)0));
  57. return -EINTR;
  58. }
  59. int shim_do_nanosleep (const struct __kernel_timespec * rqtp,
  60. struct __kernel_timespec * rmtp)
  61. {
  62. if (!rqtp)
  63. return -EFAULT;
  64. if (signal_pending()) {
  65. if (rmtp) {
  66. /* no time elapsed, so copy time interval from rqtp to rmtp */
  67. rmtp->tv_sec = rqtp->tv_sec;
  68. rmtp->tv_nsec = rqtp->tv_nsec;
  69. }
  70. return -EINTR;
  71. }
  72. unsigned long time = rqtp->tv_sec * 1000000L + rqtp->tv_nsec / 1000;
  73. unsigned long ret = DkThreadDelayExecution(time);
  74. if (ret < time) {
  75. if (rmtp) {
  76. unsigned long remtime = time - ret;
  77. rmtp->tv_sec = remtime / 1000000L;
  78. rmtp->tv_nsec = (remtime - rmtp->tv_sec * 1000) * 1000;
  79. }
  80. return -EINTR;
  81. }
  82. return 0;
  83. }