db_object.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_object.c
  17. *
  18. * This file contains APIs for closing or polling PAL handles.
  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. #include "atomic.h"
  27. /* Deprecated DkObjectReference. */
  28. int _DkObjectClose (PAL_HANDLE objectHandle)
  29. {
  30. const struct handle_ops * ops = HANDLE_OPS(objectHandle);
  31. int ret = 0;
  32. /* if the operation 'close' is defined, call the function. */
  33. if (ops && 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 || UNKNOWN_HANDLE(objectHandle)) {
  50. _DkRaiseFailure(PAL_ERROR_INVAL);
  51. LEAVE_PAL_CALL();
  52. }
  53. UNTRACE_HEAP(objectHandle);
  54. int ret = _DkObjectClose(objectHandle);
  55. if (ret < 0)
  56. _DkRaiseFailure(-ret);
  57. LEAVE_PAL_CALL();
  58. }
  59. /* PAL call DkObjectsWaitAny: wait for any of the handles in the handle array.
  60. The wait can be timed out, unless NO_TIMEOUT is given for the timeout
  61. argument. */
  62. PAL_HANDLE
  63. DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout)
  64. {
  65. ENTER_PAL_CALL(DkObjectsWaitAny);
  66. if (!count || !handleArray) {
  67. _DkRaiseFailure(PAL_ERROR_INVAL);
  68. LEAVE_PAL_CALL_RETURN(NULL);
  69. }
  70. for (int i = 0 ; i < count ; i++)
  71. // We modify the caller's handleArray?
  72. if (UNKNOWN_HANDLE(handleArray[i]))
  73. handleArray[i] = NULL;
  74. PAL_HANDLE polled = NULL;
  75. int ret = _DkObjectsWaitAny (count, handleArray,
  76. timeout == NO_TIMEOUT ? -1 : timeout,
  77. &polled);
  78. if (ret < 0) {
  79. _DkRaiseFailure(-ret);
  80. polled = NULL;
  81. }
  82. LEAVE_PAL_CALL_RETURN(polled);
  83. }