sxlock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  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 <kern/sleepq.h>
  18. #include <kern/sxlock.h>
  19. #include <machine/cpu.h>
  20. static inline int
  21. sxlock_exmark (struct sxlock *sxp)
  22. {
  23. while (1)
  24. {
  25. uint32_t val = atomic_load_rlx (&sxp->lock);
  26. if (! val)
  27. {
  28. if (atomic_cas_bool_acq (&sxp->lock, 0, SXLOCK_MASK))
  29. return (0);
  30. }
  31. else if ((val & SXLOCK_WAITERS) ||
  32. atomic_cas_bool_acq (&sxp->lock, val, val | SXLOCK_WAITERS))
  33. return (1);
  34. cpu_pause ();
  35. }
  36. }
  37. void
  38. sxlock_exlock_slow (struct sxlock *sxp)
  39. {
  40. _Auto sleepq = sleepq_lend (sxp);
  41. while (sxlock_exmark (sxp))
  42. sleepq_wait (sleepq, "sxlock/X");
  43. sleepq_return (sleepq);
  44. }
  45. static inline int
  46. sxlock_shmark (struct sxlock *sxp)
  47. {
  48. while (1)
  49. {
  50. uint32_t val = atomic_load_rlx (&sxp->lock);
  51. if ((val & SXLOCK_MASK) == 0)
  52. {
  53. if (atomic_cas_bool_acq (&sxp->lock, val, val + 1))
  54. return (0);
  55. }
  56. else if ((val & SXLOCK_WAITERS) ||
  57. atomic_cas_bool_acq (&sxp->lock, val, val | SXLOCK_WAITERS))
  58. return (1);
  59. cpu_pause ();
  60. }
  61. }
  62. void
  63. sxlock_shlock_slow (struct sxlock *sxp)
  64. {
  65. _Auto sleepq = sleepq_lend (sxp);
  66. while (sxlock_shmark (sxp))
  67. sleepq_wait (sleepq, "sxlock/S");
  68. sleepq_return (sleepq);
  69. }
  70. void
  71. sxlock_unlock (struct sxlock *sxp)
  72. {
  73. uint32_t prev, nval;
  74. while (1)
  75. {
  76. prev = atomic_load_rlx (&sxp->lock);
  77. nval = (prev & SXLOCK_MASK) == SXLOCK_MASK ||
  78. prev == (SXLOCK_WAITERS | 1) ? 0 : prev - 1;
  79. if (atomic_cas_bool_rel (&sxp->lock, prev, nval))
  80. break;
  81. cpu_pause ();
  82. }
  83. if (!nval && (prev & SXLOCK_WAITERS))
  84. sxlock_wake (sxp);
  85. }
  86. void
  87. sxlock_wake (struct sxlock *sxp)
  88. {
  89. struct sleepq *sleepq = sleepq_acquire (sxp);
  90. if (sleepq)
  91. {
  92. sleepq_broadcast (sleepq);
  93. sleepq_release (sleepq);
  94. }
  95. }