condition.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2013-2018 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. *
  18. * Condition variables.
  19. *
  20. * A condition variable is a synchronization primitive used to wait
  21. * until a predicate becomes true. Multiple threads can be waiting
  22. * for this condition. In order to synchronize changes on the predicate
  23. * with waiting and signalling, a condition variable must be associated
  24. * with a mutex.
  25. */
  26. #ifndef KERN_CONDITION_H
  27. #define KERN_CONDITION_H
  28. #include <stdint.h>
  29. #include <kern/condition_types.h>
  30. #include <kern/mutex_types.h>
  31. struct condition;
  32. /*
  33. * Initialize a condition variable.
  34. */
  35. #define condition_init(c) ((void)(c))
  36. /*
  37. * Wait for a signal on the given condition variable.
  38. *
  39. * The associated mutex must be locked when calling this function.
  40. * It is unlocked before waiting and relocked before returning.
  41. *
  42. * When bounding the duration of the wait, the caller must pass an absolute
  43. * time in ticks, and ETIMEDOUT is returned if that time is reached before
  44. * the sleep queue is signalled.
  45. */
  46. void condition_wait(struct condition *condition, struct mutex *mutex);
  47. int condition_timedwait(struct condition *condition,
  48. struct mutex *mutex, uint64_t ticks);
  49. /*
  50. * Wake up one (signal) or all (broadcast) threads waiting on a
  51. * condition variable, if any.
  52. *
  53. * Although it is not necessary to hold the mutex associated to the
  54. * condition variable when calling these functions, doing so guarantees
  55. * that a wake-up done when changing the predicate cannot be missed by
  56. * waiting threads.
  57. */
  58. void condition_signal(struct condition *condition);
  59. void condition_broadcast(struct condition *condition);
  60. #endif /* KERN_CONDITION_H */