synch.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _SYNCH_H_
  30. #define _SYNCH_H_
  31. /*
  32. * Header file for synchronization primitives.
  33. */
  34. #include <spinlock.h>
  35. /*
  36. * Dijkstra-style semaphore.
  37. *
  38. * The name field is for easier debugging. A copy of the name is made
  39. * internally.
  40. */
  41. struct semaphore {
  42. char *sem_name;
  43. struct wchan *sem_wchan;
  44. struct spinlock sem_lock;
  45. volatile int sem_count;
  46. };
  47. struct semaphore *sem_create(const char *name, int initial_count);
  48. void sem_destroy(struct semaphore *);
  49. /*
  50. * Operations (both atomic):
  51. * P (proberen): decrement count. If the count is 0, block until
  52. * the count is 1 again before decrementing.
  53. * V (verhogen): increment count.
  54. */
  55. void P(struct semaphore *);
  56. void V(struct semaphore *);
  57. /*
  58. * Simple lock for mutual exclusion.
  59. *
  60. * When the lock is created, no thread should be holding it. Likewise,
  61. * when the lock is destroyed, no thread should be holding it.
  62. *
  63. * The name field is for easier debugging. A copy of the name is
  64. * (should be) made internally.
  65. */
  66. struct lock
  67. {
  68. char * lk_name;
  69. struct wchan * wc;
  70. struct spinlock spin;
  71. struct thread * volatile owner;
  72. };
  73. struct lock *lock_create(const char *name);
  74. void lock_acquire(struct lock *);
  75. /*
  76. * Operations:
  77. * lock_acquire - Get the lock. Only one thread can hold the lock at the
  78. * same time.
  79. * lock_release - Free the lock. Only the thread holding the lock may do
  80. * this.
  81. * lock_do_i_hold - Return true if the current thread holds the lock;
  82. * false otherwise.
  83. *
  84. * These operations must be atomic. You get to write them.
  85. */
  86. void lock_release(struct lock *);
  87. bool lock_do_i_hold(struct lock *);
  88. void lock_destroy(struct lock *);
  89. /*
  90. * Condition variable.
  91. *
  92. * Note that the "variable" is a bit of a misnomer: a CV is normally used
  93. * to wait until a variable meets a particular condition, but there's no
  94. * actual variable, as such, in the CV.
  95. *
  96. * These CVs are expected to support Mesa semantics, that is, no
  97. * guarantees are made about scheduling.
  98. *
  99. * The name field is for easier debugging. A copy of the name is
  100. * (should be) made internally.
  101. */
  102. struct cv
  103. {
  104. char * cv_name;
  105. struct wchan * wc;
  106. };
  107. struct cv *cv_create(const char *name);
  108. void cv_destroy(struct cv *);
  109. /*
  110. * Operations:
  111. * cv_wait - Release the supplied lock, go to sleep, and, after
  112. * waking up again, re-acquire the lock.
  113. * cv_signal - Wake up one thread that's sleeping on this CV.
  114. * cv_broadcast - Wake up all threads sleeping on this CV.
  115. *
  116. * For all three operations, the current thread must hold the lock passed
  117. * in. Note that under normal circumstances the same lock should be used
  118. * on all operations with any particular CV.
  119. *
  120. * These operations must be atomic. You get to write them.
  121. */
  122. void cv_wait(struct cv *cv, struct lock *lock);
  123. void cv_signal(struct cv *cv, struct lock *lock);
  124. void cv_broadcast(struct cv *cv, struct lock *lock);
  125. #endif /* _SYNCH_H_ */