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. int ret = 0;
  30. /* if the operation 'close' is defined, call the function. */
  31. if (ops && ops->close)
  32. ret = ops->close(objectHandle);
  33. /*
  34. * Chia-Che 12/7/2017:
  35. * _DkObjectClose will free the object, unless the handle has
  36. * a 'close' operation, and the operation returns a non-zero value
  37. * (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. {
  46. ENTER_PAL_CALL(DkObjectClose);
  47. if (!objectHandle || UNKNOWN_HANDLE(objectHandle)) {
  48. _DkRaiseFailure(PAL_ERROR_INVAL);
  49. LEAVE_PAL_CALL();
  50. }
  51. UNTRACE_HEAP(objectHandle);
  52. int ret = _DkObjectClose(objectHandle);
  53. if (ret < 0)
  54. _DkRaiseFailure(-ret);
  55. LEAVE_PAL_CALL();
  56. }
  57. // PAL call DkObjectsWaitAny: wait for any of the handles in the handle array.
  58. // The wait can be timed out, unless NO_TIMEOUT is given for the timeout_us argument.
  59. PAL_HANDLE
  60. DkObjectsWaitAny(PAL_NUM count, PAL_HANDLE* handleArray, PAL_NUM timeout_us) {
  61. ENTER_PAL_CALL(DkObjectsWaitAny);
  62. if (!count || !handleArray) {
  63. _DkRaiseFailure(PAL_ERROR_INVAL);
  64. LEAVE_PAL_CALL_RETURN(NULL);
  65. }
  66. for (uint32_t i = 0 ; i < count ; i++)
  67. // We modify the caller's handleArray?
  68. if (handleArray[i] && UNKNOWN_HANDLE(handleArray[i]))
  69. handleArray[i] = NULL;
  70. PAL_HANDLE polled = NULL;
  71. int ret = _DkObjectsWaitAny(count, handleArray, timeout_us, &polled);
  72. if (ret < 0) {
  73. _DkRaiseFailure(-ret);
  74. polled = NULL;
  75. }
  76. LEAVE_PAL_CALL_RETURN(polled);
  77. }