shim_sleep.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <errno.h>
  19. #include <pal.h>
  20. #include <pal_error.h>
  21. #include <shim_handle.h>
  22. #include <shim_internal.h>
  23. #include <shim_table.h>
  24. #include <shim_thread.h>
  25. #include <shim_utils.h>
  26. #include <shim_vma.h>
  27. static bool signal_pending(void) {
  28. struct shim_thread* cur = get_cur_thread();
  29. if (!cur)
  30. return false;
  31. lock(&cur->lock);
  32. if (!cur->signal_logs || !cur->has_signal.counter) {
  33. unlock(&cur->lock);
  34. return false;
  35. }
  36. for (int sig = 1; sig <= NUM_SIGS; sig++) {
  37. if (signal_logs_pending(cur->signal_logs, sig)) {
  38. /* at least one signal of type sig... */
  39. if (!__sigismember(&cur->signal_mask, sig)) {
  40. /* ...and this type is not blocked */
  41. unlock(&cur->lock);
  42. return true;
  43. }
  44. }
  45. }
  46. unlock(&cur->lock);
  47. return false;
  48. }
  49. int shim_do_pause(void) {
  50. if (signal_pending())
  51. return -EINTR;
  52. /* ~0ULL micro sec ~= 805675 years */
  53. DkThreadDelayExecution(~((PAL_NUM)0));
  54. return -EINTR;
  55. }
  56. int shim_do_nanosleep(const struct __kernel_timespec* rqtp, struct __kernel_timespec* rmtp) {
  57. if (!rqtp)
  58. return -EFAULT;
  59. if (signal_pending()) {
  60. if (rmtp) {
  61. /* no time elapsed, so copy time interval from rqtp to rmtp */
  62. rmtp->tv_sec = rqtp->tv_sec;
  63. rmtp->tv_nsec = rqtp->tv_nsec;
  64. }
  65. return -EINTR;
  66. }
  67. unsigned long time = rqtp->tv_sec * 1000000L + rqtp->tv_nsec / 1000;
  68. unsigned long ret = DkThreadDelayExecution(time);
  69. if (ret < time) {
  70. if (rmtp) {
  71. unsigned long remtime = time - ret;
  72. rmtp->tv_sec = remtime / 1000000L;
  73. rmtp->tv_nsec = (remtime - rmtp->tv_sec * 1000) * 1000;
  74. }
  75. return -EINTR;
  76. }
  77. return 0;
  78. }