db_threading.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. notify_failure (-ret);
  37. return NULL;
  38. }
  39. return handle;
  40. }
  41. PAL_BUF DkThreadPrivate (PAL_BUF addr)
  42. {
  43. void * ret = _DkThreadPrivate(addr);
  44. if (ret == NULL)
  45. notify_failure(PAL_ERROR_DENIED);
  46. return ret;
  47. }
  48. /* PAL call DkThreadDelayExecution. Delay the current thread
  49. (sleep) for the given duration */
  50. PAL_NUM
  51. DkThreadDelayExecution (PAL_NUM duration)
  52. {
  53. store_frame(ThreadDelayExecution);
  54. unsigned long dur = duration;
  55. int ret = _DkThreadDelayExecution(&dur);
  56. if (ret < 0)
  57. notify_failure(PAL_ERROR_INTERRUPTED);
  58. return dur;
  59. }
  60. /* PAL call DkThreadYieldExecution. Yield the execution
  61. of the current thread. */
  62. void DkThreadYieldExecution (void)
  63. {
  64. _DkThreadYieldExecution();
  65. }
  66. /* PAL call DkThreadExit: simply exit the current thread
  67. no matter what */
  68. void DkThreadExit (void)
  69. {
  70. store_frame(ThreadExit);
  71. _DkThreadExit(0);
  72. notify_failure(PAL_ERROR_NOTKILLABLE);
  73. }
  74. /* PAL call DkThreadResume: resume the execution of a thread
  75. which is delayed before */
  76. PAL_BOL DkThreadResume (PAL_HANDLE threadHandle)
  77. {
  78. store_frame(ThreadResume);
  79. if (!threadHandle ||
  80. !IS_HANDLE_TYPE(threadHandle, thread)) {
  81. notify_failure(PAL_ERROR_INVAL);
  82. return PAL_FALSE;
  83. }
  84. int ret = _DkThreadResume(threadHandle);
  85. if (ret < 0) {
  86. notify_failure(PAL_ERROR_DENIED);
  87. return PAL_FALSE;
  88. }
  89. return PAL_TRUE;
  90. }