db_mutex.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_mutex.c
  17. *
  18. * This file contains APIs that provides operations of mutexes.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_error.h"
  24. #include "api.h"
  25. PAL_HANDLE
  26. DkMutexCreate (PAL_NUM initialCount)
  27. {
  28. ENTER_PAL_CALL(DkMutexCreate);
  29. PAL_HANDLE handle = (PAL_HANDLE) malloc(HANDLE_SIZE(mutex));
  30. SET_HANDLE_TYPE(handle, mutex);
  31. int ret = _DkMutexCreate(handle, initialCount);
  32. if (ret < 0) {
  33. free(handle);
  34. _DkRaiseFailure(-ret);
  35. handle = NULL;
  36. }
  37. LEAVE_PAL_CALL_RETURN(handle);
  38. }
  39. void
  40. DkMutexDestroy (PAL_HANDLE handle)
  41. {
  42. ENTER_PAL_CALL(DkMutexDestroy);
  43. if (!handle) {
  44. _DkRaiseFailure(PAL_ERROR_INVAL);
  45. LEAVE_PAL_CALL();
  46. }
  47. _DkMutexDestroy(handle);
  48. free(handle);
  49. LEAVE_PAL_CALL();
  50. }
  51. void DkMutexRelease (PAL_HANDLE handle)
  52. {
  53. ENTER_PAL_CALL(DkMutexRelease);
  54. if (!handle ||
  55. !IS_HANDLE_TYPE(handle, mutex)) {
  56. _DkRaiseFailure(PAL_ERROR_INVAL);
  57. LEAVE_PAL_CALL();
  58. }
  59. _DkMutexRelease (handle);
  60. LEAVE_PAL_CALL();
  61. }
  62. static int mutex_wait (PAL_HANDLE handle, uint64_t timeout)
  63. {
  64. return _DkMutexAcquireTimeout(handle, timeout);
  65. }
  66. struct handle_ops mutex_ops = {
  67. .wait = &mutex_wait,
  68. };