db_threading.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * db_threading.c
  15. *
  16. * This file contain APIs to create, exit and yield a thread.
  17. */
  18. #include "pal_defs.h"
  19. #include "pal_freebsd_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_freebsd.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <errno.h>
  27. #include <signal.h>
  28. #include <sys/mman.h>
  29. #include <sched.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include <sys/timespec.h>
  33. #include <unistd.h>
  34. #define THREAD_STACK_SIZE (pal_state.alloc_align)
  35. /* _DkThreadCreate for internal use. Create an internal thread
  36. inside the current process. The arguments callback and param
  37. specify the starting function and parameters */
  38. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  39. const void * param)
  40. {
  41. void * child_stack = NULL;
  42. if (_DkVirtualMemoryAlloc(&child_stack, THREAD_STACK_SIZE, 0,
  43. PAL_PROT_READ|PAL_PROT_WRITE) < 0)
  44. return -PAL_ERROR_NOMEM;
  45. // move child_stack to the top of stack.
  46. child_stack += THREAD_STACK_SIZE;
  47. // align child_stack to 16
  48. child_stack = ALIGN_DOWN_PTR(child_stack, 16);
  49. flags &= PAL_THREAD_MASK;
  50. assert(!flags); //FreeBSD does not support any more flags for rfork!
  51. int ret = rfork_thread(
  52. RFPROC|RFNOWAIT|RFSIGSHARE|RFMEM,
  53. child_stack,
  54. callback,
  55. (void *)param);
  56. if (IS_ERR(ret))
  57. return -PAL_ERROR_DENIED;
  58. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  59. SET_HANDLE_TYPE(hdl, thread);
  60. hdl->thread.tid = ret;
  61. *handle = hdl;
  62. return 0;
  63. }
  64. int _DkThreadDelayExecution (unsigned long * duration)
  65. {
  66. struct timespec sleeptime;
  67. struct timespec remainingtime;
  68. const long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  69. if (*duration > VERY_LONG_TIME_IN_US) {
  70. /* avoid overflow with time_t */
  71. sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  72. sleeptime.tv_nsec = 0;
  73. } else {
  74. sleeptime.tv_sec = *duration / 1000000;
  75. sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
  76. }
  77. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  78. if (IS_ERR(ret)) {
  79. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  80. remainingtime.tv_nsec / 1000;
  81. *duration -= remaining;
  82. return -PAL_ERROR_INTERRUPTED;
  83. }
  84. return 0;
  85. }
  86. /* PAL call DkThreadYieldExecution. Yield the execution
  87. of the current thread. */
  88. void _DkThreadYieldExecution (void)
  89. {
  90. INLINE_SYSCALL(sched_yield, 0);
  91. }
  92. /* _DkThreadExit for internal use: Thread exiting */
  93. noreturn void _DkThreadExit(int* clear_child_tid) {
  94. if (clear_child_tid) {
  95. /* thread is ready to exit, must inform LibOS by setting *clear_child_tid to 0;
  96. * async helper thread in LibOS is waiting on this to wake up parent */
  97. __atomic_store_n(clear_child_tid, 0, __ATOMIC_RELAXED);
  98. }
  99. INLINE_SYSCALL(exit, 1, 0);
  100. }
  101. int _DkThreadResume (PAL_HANDLE threadHandle)
  102. {
  103. int ret = INLINE_SYSCALL(kill, 2, threadHandle->thread.tid, SIGCONT);
  104. if (IS_ERR(ret))
  105. return -PAL_ERROR_DENIED;
  106. return 0;
  107. }
  108. struct handle_ops thread_ops = {
  109. /* nothing */
  110. };