db_threading.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.h"
  22. #include "pal_internal.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. /* PAL call DkThreadCreate: create a thread inside the current
  27. process */
  28. PAL_HANDLE
  29. DkThreadCreate (PAL_PTR addr, PAL_PTR param, PAL_FLG flags)
  30. {
  31. ENTER_PAL_CALL(DkThreadCreate);
  32. PAL_HANDLE handle = NULL;
  33. int ret = _DkThreadCreate (&handle, (int (*)(void *)) addr,
  34. (const void *) param, flags);
  35. if (ret < 0) {
  36. _DkRaiseFailure(-ret);
  37. handle = NULL;
  38. }
  39. TRACE_HEAP(handle);
  40. LEAVE_PAL_CALL_RETURN(handle);
  41. }
  42. /* PAL call DkThreadDelayExecution. Delay the current thread
  43. (sleep) for the given duration */
  44. PAL_NUM
  45. DkThreadDelayExecution (PAL_NUM duration)
  46. {
  47. ENTER_PAL_CALL(DkThreadDelayExecution);
  48. unsigned long dur = duration;
  49. int ret = _DkThreadDelayExecution(&dur);
  50. if (ret < 0) {
  51. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  52. duration = dur;
  53. }
  54. LEAVE_PAL_CALL_RETURN(duration);
  55. }
  56. /* PAL call DkThreadYieldExecution. Yield the execution
  57. of the current thread. */
  58. void DkThreadYieldExecution (void)
  59. {
  60. ENTER_PAL_CALL(DkThreadYieldExecution);
  61. _DkThreadYieldExecution();
  62. LEAVE_PAL_CALL();
  63. }
  64. /* PAL call DkThreadExit: simply exit the current thread
  65. no matter what */
  66. void DkThreadExit (void)
  67. {
  68. ENTER_PAL_CALL(DkThreadExit);
  69. _DkThreadExit();
  70. _DkRaiseFailure(PAL_ERROR_NOTKILLABLE);
  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. }