db_threading.c 2.8 KB

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