db_object.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "pal_defs.h"
  19. #include "pal.h"
  20. #include "pal_internal.h"
  21. #include "pal_error.h"
  22. #include "pal_debug.h"
  23. #include "api.h"
  24. #include "atomic.h"
  25. /* Deprecated DkObjectReference. */
  26. int _DkObjectClose (PAL_HANDLE objectHandle)
  27. {
  28. const struct handle_ops * ops = HANDLE_OPS(objectHandle);
  29. if (!ops)
  30. return -PAL_ERROR_BADHANDLE;
  31. int ret = 0;
  32. /* if the operation 'close' is defined, call the function. */
  33. if (ops->close)
  34. ret = ops->close(objectHandle);
  35. /*
  36. * Chia-Che 12/7/2017:
  37. * _DkObjectClose will free the object, unless the handle has
  38. * a 'close' operation, and the operation returns a non-zero value
  39. * (e.g., 1 for skipping free() or -ERRNO).
  40. */
  41. if (!ret)
  42. free(objectHandle);
  43. return ret;
  44. }
  45. /* PAL call DkObjectClose: Close the given object handle. */
  46. void DkObjectClose (PAL_HANDLE objectHandle)
  47. {
  48. ENTER_PAL_CALL(DkObjectClose);
  49. if (!objectHandle) {
  50. _DkRaiseFailure(PAL_ERROR_INVAL);
  51. LEAVE_PAL_CALL();
  52. }
  53. int ret = _DkObjectClose(objectHandle);
  54. if (ret < 0)
  55. _DkRaiseFailure(-ret);
  56. LEAVE_PAL_CALL();
  57. }
  58. // PAL call DkObjectsWaitAny: wait for any of the handles in the handle array.
  59. // The wait can be timed out, unless NO_TIMEOUT is given for the timeout_us argument.
  60. PAL_HANDLE
  61. DkObjectsWaitAny(PAL_NUM count, PAL_HANDLE* handleArray, PAL_NUM timeout_us) {
  62. ENTER_PAL_CALL(DkObjectsWaitAny);
  63. if (!count || !handleArray) {
  64. _DkRaiseFailure(PAL_ERROR_INVAL);
  65. LEAVE_PAL_CALL_RETURN(NULL);
  66. }
  67. for (uint32_t i = 0 ; i < count ; i++)
  68. // We modify the caller's handleArray?
  69. if (handleArray[i] && UNKNOWN_HANDLE(handleArray[i]))
  70. handleArray[i] = NULL;
  71. PAL_HANDLE polled = NULL;
  72. int ret = _DkObjectsWaitAny(count, handleArray, timeout_us, &polled);
  73. if (ret < 0) {
  74. _DkRaiseFailure(-ret);
  75. polled = NULL;
  76. }
  77. LEAVE_PAL_CALL_RETURN(polled);
  78. }