db_threading.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. store_frame(ThreadCreate);
  32. PAL_HANDLE handle = NULL;
  33. int ret = _DkThreadCreate (&handle, (int (*)(void *)) addr,
  34. param, flags);
  35. if (ret < 0)
  36. leave_frame(NULL, -ret);
  37. leave_frame(handle, 0);
  38. }
  39. /* PAL call DkThreadDelayExecution. Delay the current thread
  40. (sleep) for the given duration */
  41. PAL_NUM
  42. DkThreadDelayExecution (PAL_NUM duration)
  43. {
  44. store_frame(ThreadDelayExecution);
  45. unsigned long dur = duration;
  46. int ret = _DkThreadDelayExecution(&dur);
  47. if (ret < 0)
  48. leave_frame(dur, PAL_ERROR_INTERRUPTED);
  49. leave_frame(duration, 0);
  50. }
  51. /* PAL call DkThreadYieldExecution. Yield the execution
  52. of the current thread. */
  53. void DkThreadYieldExecution (void)
  54. {
  55. _DkThreadYieldExecution();
  56. }
  57. /* PAL call DkThreadExit: simply exit the current thread
  58. no matter what */
  59. void DkThreadExit (void)
  60. {
  61. store_frame(ThreadExit);
  62. _DkThreadExit();
  63. leave_frame(, PAL_ERROR_NOTKILLABLE);
  64. }
  65. /* PAL call DkThreadResume: resume the execution of a thread
  66. which is delayed before */
  67. PAL_BOL DkThreadResume (PAL_HANDLE threadHandle)
  68. {
  69. store_frame(ThreadResume);
  70. if (!threadHandle || !IS_HANDLE_TYPE(threadHandle, thread))
  71. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  72. int ret = _DkThreadResume(threadHandle);
  73. if (ret < 0)
  74. leave_frame(PAL_FALSE, PAL_ERROR_DENIED);
  75. leave_frame(PAL_TRUE, 0);
  76. }