db_semaphore.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_semaphore.c
  17. *
  18. * This file contains APIs that provides operations of semaphores.
  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. DkSemaphoreCreate (PAL_NUM initialCount, PAL_NUM maxCount)
  27. {
  28. ENTER_PAL_CALL(DkSemaphoreCreate);
  29. PAL_HANDLE handle = (PAL_HANDLE) malloc(HANDLE_SIZE(semaphore));
  30. int ret = _DkSemaphoreCreate(handle, initialCount, maxCount);
  31. if (ret < 0) {
  32. free(handle);
  33. _DkRaiseFailure(-ret);
  34. handle = NULL;
  35. }
  36. LEAVE_PAL_CALL_RETURN(handle);
  37. }
  38. void
  39. DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle)
  40. {
  41. ENTER_PAL_CALL(DkSemaphoreDestroy);
  42. if (!semaphoreHandle) {
  43. _DkRaiseFailure(PAL_ERROR_INVAL);
  44. LEAVE_PAL_CALL();
  45. }
  46. _DkSemaphoreDestroy(semaphoreHandle);
  47. LEAVE_PAL_CALL();
  48. }
  49. void DkSemaphoreRelease (PAL_HANDLE handle, PAL_NUM count)
  50. {
  51. ENTER_PAL_CALL(DkSemaphoreRelease);
  52. if (!handle ||
  53. !IS_HANDLE_TYPE(handle, semaphore)) {
  54. _DkRaiseFailure(PAL_ERROR_INVAL);
  55. LEAVE_PAL_CALL();
  56. }
  57. _DkSemaphoreRelease (handle, count);
  58. LEAVE_PAL_CALL();
  59. }
  60. static int sem_wait (PAL_HANDLE handle, int timeout)
  61. {
  62. return _DkSemaphoreAcquireTimeout(handle, 1, timeout);
  63. }
  64. struct handle_ops sem_ops = {
  65. .wait = &sem_wait,
  66. };