condition.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.h>
  31. struct condition;
  32. // Initialize a condition variable.
  33. #define condition_init(c) ((void)(c))
  34. /*
  35. * Wait for a signal on the given condition variable.
  36. *
  37. * The associated mutex must be locked when calling this function.
  38. * It is unlocked before waiting and relocked before returning.
  39. *
  40. * When bounding the duration of the wait, the caller must pass an absolute
  41. * time in ticks, and ETIMEDOUT is returned if that time is reached before
  42. * the sleep queue is signalled.
  43. */
  44. void condition_wait (struct condition *condition, struct mutex *mutex);
  45. int condition_timedwait (struct condition *condition,
  46. struct mutex *mutex, uint64_t ticks);
  47. /*
  48. * Wake up one (signal) or all (broadcast) threads waiting on a
  49. * condition variable, if any.
  50. *
  51. * Although it is not necessary to hold the mutex associated to the
  52. * condition variable when calling these functions, doing so guarantees
  53. * that a wake-up done when changing the predicate cannot be missed by
  54. * waiting threads.
  55. */
  56. void condition_signal (struct condition *condition);
  57. void condition_broadcast (struct condition *condition);
  58. #endif