db_threading.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "api.h"
  19. #include "pal.h"
  20. #include "pal_debug.h"
  21. #include "pal_defs.h"
  22. #include "pal_error.h"
  23. #include "pal_internal.h"
  24. /* PAL call DkThreadCreate: create a thread inside the current
  25. process */
  26. PAL_HANDLE
  27. DkThreadCreate(PAL_PTR addr, PAL_PTR param) {
  28. ENTER_PAL_CALL(DkThreadCreate);
  29. PAL_HANDLE handle = NULL;
  30. int ret = _DkThreadCreate(&handle, (int (*)(void*))addr, (const void*)param);
  31. if (ret < 0) {
  32. _DkRaiseFailure(-ret);
  33. handle = NULL;
  34. }
  35. LEAVE_PAL_CALL_RETURN(handle);
  36. }
  37. /* PAL call DkThreadDelayExecution. Delay the current thread
  38. (sleep) for the given duration */
  39. PAL_NUM
  40. DkThreadDelayExecution(PAL_NUM duration) {
  41. ENTER_PAL_CALL(DkThreadDelayExecution);
  42. unsigned long dur = duration;
  43. int ret = _DkThreadDelayExecution(&dur);
  44. if (ret < 0) {
  45. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  46. duration = dur;
  47. }
  48. LEAVE_PAL_CALL_RETURN(duration);
  49. }
  50. /* PAL call DkThreadYieldExecution. Yield the execution
  51. of the current thread. */
  52. void DkThreadYieldExecution(void) {
  53. ENTER_PAL_CALL(DkThreadYieldExecution);
  54. _DkThreadYieldExecution();
  55. LEAVE_PAL_CALL();
  56. }
  57. /* PAL call DkThreadExit: simply exit the current thread
  58. no matter what */
  59. noreturn void DkThreadExit(PAL_PTR clear_child_tid) {
  60. ENTER_PAL_CALL(DkThreadExit);
  61. _DkThreadExit((int*)clear_child_tid);
  62. _DkRaiseFailure(PAL_ERROR_NOTKILLABLE);
  63. while (true)
  64. /* nothing */;
  65. LEAVE_PAL_CALL();
  66. }
  67. /* PAL call DkThreadResume: resume the execution of a thread
  68. which is delayed before */
  69. PAL_BOL DkThreadResume(PAL_HANDLE threadHandle) {
  70. ENTER_PAL_CALL(DkThreadResume);
  71. if (!threadHandle || !IS_HANDLE_TYPE(threadHandle, thread)) {
  72. _DkRaiseFailure(PAL_ERROR_INVAL);
  73. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  74. }
  75. int ret = _DkThreadResume(threadHandle);
  76. if (ret < 0) {
  77. _DkRaiseFailure(PAL_ERROR_DENIED);
  78. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  79. }
  80. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  81. }