db_object.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /* PAL call DkObjectsWaitAny: wait for any of the handles in the handle array. The wait can be timed
  56. * out, unless NO_TIMEOUT is given for the timeout_us argument. */
  57. PAL_HANDLE
  58. DkObjectsWaitAny(PAL_NUM count, PAL_HANDLE* handle_array, PAL_NUM timeout_us) {
  59. ENTER_PAL_CALL(DkObjectsWaitAny);
  60. if (!count || !handle_array) {
  61. _DkRaiseFailure(PAL_ERROR_INVAL);
  62. LEAVE_PAL_CALL_RETURN(NULL);
  63. }
  64. for (PAL_NUM i = 0; i < count; i++)
  65. if (UNKNOWN_HANDLE(handle_array[i])) {
  66. _DkRaiseFailure(PAL_ERROR_INVAL);
  67. LEAVE_PAL_CALL_RETURN(NULL);
  68. }
  69. PAL_HANDLE polled = NULL;
  70. int ret = _DkObjectsWaitAny(count, handle_array, timeout_us, &polled);
  71. if (ret < 0) {
  72. _DkRaiseFailure(-ret);
  73. polled = NULL;
  74. }
  75. LEAVE_PAL_CALL_RETURN(polled);
  76. }
  77. /* Wait for user-specified events of handles in the handle array. The wait can be timed out, unless
  78. * NO_TIMEOUT is given in the timeout_us argument. Returns PAL_TRUE if waiting was successful. */
  79. PAL_BOL DkObjectsWaitEvents(PAL_NUM count, PAL_HANDLE* handle_array, PAL_FLG* events,
  80. PAL_FLG* ret_events, PAL_NUM timeout_us) {
  81. ENTER_PAL_CALL(DkObjectsWaitEvents);
  82. if (!count || !handle_array || !events || !ret_events) {
  83. _DkRaiseFailure(PAL_ERROR_INVAL);
  84. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  85. }
  86. for (PAL_NUM i = 0; i < count; i++) {
  87. if (UNKNOWN_HANDLE(handle_array[i])) {
  88. _DkRaiseFailure(PAL_ERROR_INVAL);
  89. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  90. }
  91. }
  92. int ret = _DkObjectsWaitEvents(count, handle_array, events, ret_events, timeout_us);
  93. if (ret < 0) {
  94. _DkRaiseFailure(-ret);
  95. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  96. }
  97. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  98. }