db_object.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_object.c
  15. *
  16. * This file contains APIs for closing or polling PAL handles.
  17. */
  18. #include "api.h"
  19. #include "atomic.h"
  20. #include "pal.h"
  21. #include "pal_debug.h"
  22. #include "pal_defs.h"
  23. #include "pal_error.h"
  24. #include "pal_internal.h"
  25. /* Deprecated DkObjectReference. */
  26. int _DkObjectClose(PAL_HANDLE objectHandle) {
  27. const struct handle_ops* ops = HANDLE_OPS(objectHandle);
  28. if (!ops)
  29. return -PAL_ERROR_BADHANDLE;
  30. int ret = 0;
  31. /* if the operation 'close' is defined, call the function. */
  32. if (ops->close)
  33. ret = ops->close(objectHandle);
  34. /*
  35. * Chia-Che 12/7/2017:
  36. * _DkObjectClose will free the object, unless the handle has a 'close' operation, and the
  37. * operation returns a non-zero value (e.g., 1 for skipping free() or -ERRNO).
  38. */
  39. if (!ret)
  40. free(objectHandle);
  41. return ret;
  42. }
  43. /* PAL call DkObjectClose: Close the given object handle. */
  44. void DkObjectClose(PAL_HANDLE objectHandle) {
  45. ENTER_PAL_CALL(DkObjectClose);
  46. if (!objectHandle) {
  47. _DkRaiseFailure(PAL_ERROR_INVAL);
  48. LEAVE_PAL_CALL();
  49. }
  50. int ret = _DkObjectClose(objectHandle);
  51. if (ret < 0)
  52. _DkRaiseFailure(-ret);
  53. LEAVE_PAL_CALL();
  54. }
  55. /* Wait on a synchronization handle and return true if this handle's event was triggered,
  56. * otherwise return false and additionally raise failure. */
  57. PAL_BOL DkSynchronizationObjectWait(PAL_HANDLE handle, PAL_NUM timeout_us) {
  58. ENTER_PAL_CALL(DkSynchronizationObjectWait);
  59. if (!handle) {
  60. _DkRaiseFailure(PAL_ERROR_INVAL);
  61. LEAVE_PAL_CALL_RETURN(NULL);
  62. }
  63. int ret = _DkSynchronizationObjectWait(handle, timeout_us);
  64. if (ret < 0) {
  65. _DkRaiseFailure(-ret);
  66. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  67. }
  68. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  69. }
  70. /* Wait for user-specified events of handles in the handle array. The wait can be timed out, unless
  71. * NO_TIMEOUT is given in the timeout_us argument. Returns PAL_TRUE if waiting was successful. */
  72. PAL_BOL DkStreamsWaitEvents(PAL_NUM count, PAL_HANDLE* handle_array, PAL_FLG* events,
  73. PAL_FLG* ret_events, PAL_NUM timeout_us) {
  74. ENTER_PAL_CALL(DkStreamsWaitEvents);
  75. if (!count || !handle_array || !events || !ret_events) {
  76. _DkRaiseFailure(PAL_ERROR_INVAL);
  77. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  78. }
  79. for (PAL_NUM i = 0; i < count; i++) {
  80. if (UNKNOWN_HANDLE(handle_array[i])) {
  81. _DkRaiseFailure(PAL_ERROR_INVAL);
  82. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  83. }
  84. }
  85. int ret = _DkStreamsWaitEvents(count, handle_array, events, ret_events, timeout_us);
  86. if (ret < 0) {
  87. _DkRaiseFailure(-ret);
  88. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  89. }
  90. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  91. }