sem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2019 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/sem.h>
  7. #include <syscall_handler.h>
  8. #ifdef CONFIG_USERSPACE
  9. #define SYS_SEM_MINIMUM 0
  10. #define SYS_SEM_CONTENDED (SYS_SEM_MINIMUM - 1)
  11. static inline atomic_t bounded_dec(atomic_t *val, atomic_t minimum)
  12. {
  13. atomic_t old_value, new_value;
  14. do {
  15. old_value = atomic_get(val);
  16. if (old_value < minimum) {
  17. break;
  18. }
  19. new_value = old_value - 1;
  20. } while (atomic_cas(val, old_value, new_value) == 0);
  21. return old_value;
  22. }
  23. static inline atomic_t bounded_inc(atomic_t *val, atomic_t minimum,
  24. atomic_t maximum)
  25. {
  26. atomic_t old_value, new_value;
  27. do {
  28. old_value = atomic_get(val);
  29. if (old_value >= maximum) {
  30. break;
  31. }
  32. new_value = old_value < minimum ?
  33. minimum + 1 : old_value + 1;
  34. } while (atomic_cas(val, old_value, new_value) == 0U);
  35. return old_value;
  36. }
  37. int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
  38. unsigned int limit)
  39. {
  40. if (sem == NULL || limit == SYS_SEM_MINIMUM ||
  41. initial_count > limit || limit > INT_MAX) {
  42. return -EINVAL;
  43. }
  44. atomic_set(&sem->futex.val, initial_count);
  45. sem->limit = limit;
  46. return 0;
  47. }
  48. int sys_sem_give(struct sys_sem *sem)
  49. {
  50. int ret = 0;
  51. atomic_t old_value;
  52. old_value = bounded_inc(&sem->futex.val,
  53. SYS_SEM_MINIMUM, sem->limit);
  54. if (old_value < 0) {
  55. ret = k_futex_wake(&sem->futex, true);
  56. if (ret > 0) {
  57. return 0;
  58. }
  59. } else if (old_value >= sem->limit) {
  60. return -EAGAIN;
  61. } else {
  62. ;
  63. }
  64. return ret;
  65. }
  66. int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout)
  67. {
  68. int ret = 0;
  69. atomic_t old_value;
  70. do {
  71. old_value = bounded_dec(&sem->futex.val,
  72. SYS_SEM_MINIMUM);
  73. if (old_value > 0) {
  74. return 0;
  75. }
  76. ret = k_futex_wait(&sem->futex,
  77. SYS_SEM_CONTENDED, timeout);
  78. } while (ret == 0 || ret == -EAGAIN);
  79. return ret;
  80. }
  81. unsigned int sys_sem_count_get(struct sys_sem *sem)
  82. {
  83. int value = atomic_get(&sem->futex.val);
  84. return value > SYS_SEM_MINIMUM ? value : SYS_SEM_MINIMUM;
  85. }
  86. #else
  87. int sys_sem_init(struct sys_sem *sem, unsigned int initial_count,
  88. unsigned int limit)
  89. {
  90. k_sem_init(&sem->kernel_sem, initial_count, limit);
  91. return 0;
  92. }
  93. int sys_sem_give(struct sys_sem *sem)
  94. {
  95. k_sem_give(&sem->kernel_sem);
  96. return 0;
  97. }
  98. int sys_sem_take(struct sys_sem *sem, k_timeout_t timeout)
  99. {
  100. int ret_value = 0;
  101. ret_value = k_sem_take(&sem->kernel_sem, timeout);
  102. if (ret_value == -EAGAIN || ret_value == -EBUSY) {
  103. ret_value = -ETIMEDOUT;
  104. }
  105. return ret_value;
  106. }
  107. unsigned int sys_sem_count_get(struct sys_sem *sem)
  108. {
  109. return k_sem_count_get(&sem->kernel_sem);
  110. }
  111. #endif