db_semaphore.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. store_frame(SemaphoreCreate);
  29. PAL_HANDLE handle = (PAL_HANDLE) malloc(HANDLE_SIZE(semaphore));
  30. int ret = _DkSemaphoreCreate(handle, initialCount, maxCount);
  31. if (ret < 0)
  32. leave_frame(NULL, -ret);
  33. leave_frame(handle, -ret);
  34. }
  35. void
  36. DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle)
  37. {
  38. store_frame(SemaphoreDestroy);
  39. if (!semaphoreHandle)
  40. leave_frame(, PAL_ERROR_INVAL);
  41. _DkSemaphoreDestroy(semaphoreHandle);
  42. leave_frame(, 0);
  43. }
  44. void DkSemaphoreRelease (PAL_HANDLE handle, PAL_NUM count)
  45. {
  46. store_frame(SemaphoreRelease);
  47. if (!handle ||
  48. !IS_HANDLE_TYPE(handle, semaphore))
  49. leave_frame(, 0);
  50. _DkSemaphoreRelease (handle, count);
  51. leave_frame(, 0);
  52. }
  53. static int sem_wait (PAL_HANDLE handle, int timeout)
  54. {
  55. return _DkSemaphoreAcquireTimeout(handle, 1, timeout);
  56. }
  57. struct handle_ops sem_ops = {
  58. .wait = &sem_wait,
  59. };