futex.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2020 Richard Braun.
  3. * Copyright (c) 2020 Agustina Arzille.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <errno.h>
  20. #include <kern/atomic.h>
  21. #include <kern/clock.h>
  22. #include <kern/futex.h>
  23. #include <kern/sleepq.h>
  24. int
  25. futex_wait(void *addr, int value, unsigned int flags, uint64_t ticks)
  26. {
  27. struct sleepq *sleepq;
  28. int error;
  29. if (((uintptr_t)addr & (sizeof(int) - 1))) {
  30. return EINVAL;
  31. }
  32. sleepq = sleepq_lend(addr);
  33. if (*(int *)addr != value) {
  34. error = EAGAIN;
  35. } else if ((flags & FUTEX_TIMED) == 0) {
  36. sleepq_wait(sleepq, "futex");
  37. error = 0;
  38. } else {
  39. if ((flags & FUTEX_ABS) == 0) {
  40. ticks += clock_get_time() + 1;
  41. }
  42. error = sleepq_timedwait(sleepq, "futex", ticks);
  43. }
  44. sleepq_return(sleepq);
  45. return error;
  46. }
  47. int
  48. futex_wake(void *addr, unsigned int flags, int value)
  49. {
  50. struct sleepq *sleepq;
  51. if (((uintptr_t)addr & (sizeof(int) - 1))) {
  52. return EINVAL;
  53. }
  54. sleepq = sleepq_acquire(addr);
  55. if (flags & FUTEX_MODIFY) {
  56. atomic_store((int *)addr, value, ATOMIC_RELEASE);
  57. }
  58. if (sleepq != NULL) {
  59. if (flags & FUTEX_BROADCAST) {
  60. sleepq_broadcast(sleepq);
  61. } else {
  62. sleepq_signal(sleepq);
  63. }
  64. sleepq_release(sleepq);
  65. }
  66. return 0;
  67. }
  68. int
  69. futex_requeue(void *src_addr, void *dst_addr,
  70. unsigned int flags, bool wake_one)
  71. {
  72. if (((uintptr_t)src_addr | (uintptr_t)dst_addr) & (sizeof(int) - 1)) {
  73. return EINVAL;
  74. }
  75. return sleepq_move(src_addr, dst_addr, wake_one,
  76. (flags & FUTEX_BROADCAST) != 0);
  77. }