db_object.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. int _DkObjectReference (PAL_HANDLE objectHandle)
  28. {
  29. if (!objectHandle || UNKNOWN_HANDLE(objectHandle))
  30. return -PAL_ERROR_INVAL;
  31. atomic_inc(&HANDLE_HDR(objectHandle)->ref);
  32. return 0;
  33. }
  34. void DkObjectReference (PAL_HANDLE objectHandle)
  35. {
  36. ENTER_PAL_CALL(DkObjectReference);
  37. int ret = _DkObjectReference(objectHandle);
  38. if (ret < 0)
  39. _DkRaiseFailure(-ret);
  40. LEAVE_PAL_CALL();
  41. }
  42. int _DkObjectClose (PAL_HANDLE objectHandle)
  43. {
  44. if (!objectHandle || UNKNOWN_HANDLE(objectHandle))
  45. return -PAL_ERROR_INVAL;
  46. if (atomic_dec_and_test_nonnegative(&HANDLE_HDR(objectHandle)->ref))
  47. return 0;
  48. const struct handle_ops * ops = HANDLE_OPS(objectHandle);
  49. /* if the operation 'close' is defined, call the function. */
  50. if (ops && ops->close)
  51. return ops->close(objectHandle);
  52. free(objectHandle);
  53. return 0;
  54. }
  55. /* PAL call DkObjectClose: Close the given object handle. This function is
  56. different from DkStreamDelete. It works on all kinds of handles, and it
  57. simply close the reference to the object, the stream is not actually
  58. deleted. */
  59. void DkObjectClose (PAL_HANDLE objectHandle)
  60. {
  61. ENTER_PAL_CALL(DkObjectClose);
  62. int ret = _DkObjectClose(objectHandle);
  63. if (ret < 0)
  64. _DkRaiseFailure(-ret);
  65. LEAVE_PAL_CALL();
  66. }
  67. /* PAL call DkObjectsWaitAny: wait for any of the handles in the handle array.
  68. The wait can be timed out, unless NO_TIMEOUT is given for the timeout
  69. argument. */
  70. PAL_HANDLE
  71. DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout)
  72. {
  73. ENTER_PAL_CALL(DkObjectsWaitAny);
  74. if (!count || !handleArray) {
  75. _DkRaiseFailure(PAL_ERROR_INVAL);
  76. LEAVE_PAL_CALL_RETURN(NULL);
  77. }
  78. for (int i = 0 ; i < count ; i++)
  79. if (UNKNOWN_HANDLE(handleArray[i]))
  80. handleArray[i] = NULL;
  81. PAL_HANDLE polled = NULL;
  82. int ret = _DkObjectsWaitAny (count, handleArray,
  83. timeout == NO_TIMEOUT ? -1 : timeout,
  84. &polled);
  85. if (ret < 0) {
  86. _DkRaiseFailure(-ret);
  87. polled = NULL;
  88. }
  89. LEAVE_PAL_CALL_RETURN(polled);
  90. }