db_threading.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_threading.c
  17. *
  18. * This file contain APIs to create, exit and yield a thread.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <errno.h>
  29. #include <signal.h>
  30. #include <sys/mman.h>
  31. #include <sched.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <sys/timespec.h>
  35. #include <unistd.h>
  36. #define THREAD_STACK_SIZE (pal_state.alloc_align)
  37. /* _DkThreadCreate for internal use. Create an internal thread
  38. inside the current process. The arguments callback and param
  39. specify the starting function and parameters */
  40. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  41. const void * param)
  42. {
  43. void * child_stack = NULL;
  44. if (_DkVirtualMemoryAlloc(&child_stack, THREAD_STACK_SIZE, 0,
  45. PAL_PROT_READ|PAL_PROT_WRITE) < 0)
  46. return -PAL_ERROR_NOMEM;
  47. // move child_stack to the top of stack.
  48. child_stack += THREAD_STACK_SIZE;
  49. // align child_stack to 16
  50. child_stack = ALIGN_DOWN_PTR(child_stack, 16);
  51. flags &= PAL_THREAD_MASK;
  52. assert(!flags); //FreeBSD does not support any more flags for rfork!
  53. int ret = rfork_thread(
  54. RFPROC|RFNOWAIT|RFSIGSHARE|RFMEM,
  55. child_stack,
  56. callback,
  57. (void *)param);
  58. if (IS_ERR(ret))
  59. return -PAL_ERROR_DENIED;
  60. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  61. SET_HANDLE_TYPE(hdl, thread);
  62. hdl->thread.tid = ret;
  63. *handle = hdl;
  64. return 0;
  65. }
  66. int _DkThreadDelayExecution (unsigned long * duration)
  67. {
  68. struct timespec sleeptime;
  69. struct timespec remainingtime;
  70. #define VERY_LONG_TIME_IN_US (1000000L * 60 * 60 * 24 * 365 * 128)
  71. if (*duration > VERY_LONG_TIME_IN_US) {
  72. /* avoid overflow with time_t */
  73. sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  74. sleeptime.tv_nsec = 0;
  75. } else {
  76. sleeptime.tv_sec = *duration / 1000000;
  77. sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
  78. }
  79. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  80. if (IS_ERR(ret)) {
  81. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  82. remainingtime.tv_nsec / 1000;
  83. *duration -= remaining;
  84. return -PAL_ERROR_INTERRUPTED;
  85. }
  86. return 0;
  87. }
  88. /* PAL call DkThreadYieldExecution. Yield the execution
  89. of the current thread. */
  90. void _DkThreadYieldExecution (void)
  91. {
  92. INLINE_SYSCALL(sched_yield, 0);
  93. }
  94. /* _DkThreadExit for internal use: Thread exiting */
  95. void _DkThreadExit (void)
  96. {
  97. INLINE_SYSCALL(exit, 1, 0);
  98. }
  99. int _DkThreadResume (PAL_HANDLE threadHandle)
  100. {
  101. int ret = INLINE_SYSCALL(kill, 2, threadHandle->thread.tid, SIGCONT);
  102. if (IS_ERR(ret))
  103. return -PAL_ERROR_DENIED;
  104. return 0;
  105. }
  106. struct handle_ops thread_ops = {
  107. /* nothing */
  108. };