db_threading.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. LEAVE_PAL_CALL_RETURN(handle);
  40. }
  41. /* PAL call DkThreadDelayExecution. Delay the current thread
  42. (sleep) for the given duration */
  43. PAL_NUM
  44. DkThreadDelayExecution (PAL_NUM duration)
  45. {
  46. ENTER_PAL_CALL(DkThreadDelayExecution);
  47. unsigned long dur = duration;
  48. int ret = _DkThreadDelayExecution(&dur);
  49. if (ret < 0) {
  50. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  51. duration = dur;
  52. }
  53. LEAVE_PAL_CALL_RETURN(duration);
  54. }
  55. /* PAL call DkThreadYieldExecution. Yield the execution
  56. of the current thread. */
  57. void DkThreadYieldExecution (void)
  58. {
  59. ENTER_PAL_CALL(DkThreadYieldExecution);
  60. _DkThreadYieldExecution();
  61. LEAVE_PAL_CALL();
  62. }
  63. /* PAL call DkThreadExit: simply exit the current thread
  64. no matter what */
  65. void DkThreadExit (void)
  66. {
  67. ENTER_PAL_CALL(DkThreadExit);
  68. _DkThreadExit();
  69. _DkRaiseFailure(PAL_ERROR_NOTKILLABLE);
  70. LEAVE_PAL_CALL();
  71. }
  72. /* PAL call DkThreadResume: resume the execution of a thread
  73. which is delayed before */
  74. PAL_BOL DkThreadResume (PAL_HANDLE threadHandle)
  75. {
  76. ENTER_PAL_CALL(DkThreadResume);
  77. if (!threadHandle || !IS_HANDLE_TYPE(threadHandle, thread)) {
  78. _DkRaiseFailure(PAL_ERROR_INVAL);
  79. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  80. }
  81. int ret = _DkThreadResume(threadHandle);
  82. if (ret < 0) {
  83. _DkRaiseFailure(PAL_ERROR_DENIED);
  84. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  85. }
  86. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  87. }