semaphore.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2017-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <kern/semaphore.h>
  22. #include <kern/sleepq.h>
  23. void
  24. semaphore_init (struct semaphore *semaphore, uint16_t value, uint16_t max_value)
  25. {
  26. assert (value <= max_value);
  27. semaphore->value = value;
  28. semaphore->max_value = max_value;
  29. }
  30. int
  31. semaphore_trywait (struct semaphore *semaphore)
  32. {
  33. cpu_flags_t flags;
  34. _Auto sleepq = sleepq_lend_intr_save (semaphore, &flags);
  35. int error;
  36. if (!semaphore->value)
  37. error = EAGAIN;
  38. else
  39. {
  40. --semaphore->value;
  41. error = 0;
  42. }
  43. sleepq_return_intr_restore (sleepq, flags);
  44. return (error);
  45. }
  46. void
  47. semaphore_wait (struct semaphore *semaphore)
  48. {
  49. cpu_flags_t flags;
  50. _Auto sleepq = sleepq_lend_intr_save (semaphore, &flags);
  51. while (1)
  52. {
  53. if (semaphore->value)
  54. {
  55. --semaphore->value;
  56. break;
  57. }
  58. sleepq_wait (sleepq, "sem");
  59. }
  60. sleepq_return_intr_restore (sleepq, flags);
  61. }
  62. int
  63. semaphore_timedwait (struct semaphore *semaphore, uint64_t ticks)
  64. {
  65. cpu_flags_t flags;
  66. _Auto sleepq = sleepq_lend_intr_save (semaphore, &flags);
  67. int error;
  68. while (1)
  69. {
  70. if (semaphore->value)
  71. {
  72. --semaphore->value;
  73. error = 0;
  74. break;
  75. }
  76. error = sleepq_timedwait (sleepq, "sem", ticks);
  77. if (error)
  78. break;
  79. }
  80. sleepq_return_intr_restore (sleepq, flags);
  81. return (error);
  82. }
  83. int
  84. semaphore_post (struct semaphore *semaphore)
  85. {
  86. cpu_flags_t flags;
  87. _Auto sleepq = sleepq_lend_intr_save (semaphore, &flags);
  88. int error;
  89. if (semaphore->value == semaphore->max_value)
  90. error = EOVERFLOW;
  91. else
  92. {
  93. assert (semaphore->value < semaphore->max_value);
  94. ++semaphore->value;
  95. sleepq_signal (sleepq);
  96. error = 0;
  97. }
  98. sleepq_return_intr_restore (sleepq, flags);
  99. return (error);
  100. }