db_threading.c 2.8 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. * 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. TRACE_HEAP(handle);
  36. LEAVE_PAL_CALL_RETURN(handle);
  37. }
  38. /* PAL call DkThreadDelayExecution. Delay the current thread
  39. (sleep) for the given duration */
  40. PAL_NUM
  41. DkThreadDelayExecution(PAL_NUM duration) {
  42. ENTER_PAL_CALL(DkThreadDelayExecution);
  43. unsigned long dur = duration;
  44. int ret = _DkThreadDelayExecution(&dur);
  45. if (ret < 0) {
  46. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  47. duration = dur;
  48. }
  49. LEAVE_PAL_CALL_RETURN(duration);
  50. }
  51. /* PAL call DkThreadYieldExecution. Yield the execution
  52. of the current thread. */
  53. void DkThreadYieldExecution(void) {
  54. ENTER_PAL_CALL(DkThreadYieldExecution);
  55. _DkThreadYieldExecution();
  56. LEAVE_PAL_CALL();
  57. }
  58. /* PAL call DkThreadExit: simply exit the current thread
  59. no matter what */
  60. noreturn void DkThreadExit(void) {
  61. ENTER_PAL_CALL(DkThreadExit);
  62. _DkThreadExit();
  63. _DkRaiseFailure(PAL_ERROR_NOTKILLABLE);
  64. while (true)
  65. /* nothing */;
  66. LEAVE_PAL_CALL();
  67. }
  68. /* PAL call DkThreadResume: resume the execution of a thread
  69. which is delayed before */
  70. PAL_BOL DkThreadResume(PAL_HANDLE threadHandle) {
  71. ENTER_PAL_CALL(DkThreadResume);
  72. if (!threadHandle || !IS_HANDLE_TYPE(threadHandle, thread)) {
  73. _DkRaiseFailure(PAL_ERROR_INVAL);
  74. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  75. }
  76. int ret = _DkThreadResume(threadHandle);
  77. if (ret < 0) {
  78. _DkRaiseFailure(PAL_ERROR_DENIED);
  79. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  80. }
  81. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  82. }