synch.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. char *lk_name;
  68. // add what you need here
  69. // (don't forget to mark things volatile as needed)
  70. };
  71. struct lock *lock_create(const char *name);
  72. void lock_acquire(struct lock *);
  73. /*
  74. * Operations:
  75. * lock_acquire - Get the lock. Only one thread can hold the lock at the
  76. * same time.
  77. * lock_release - Free the lock. Only the thread holding the lock may do
  78. * this.
  79. * lock_do_i_hold - Return true if the current thread holds the lock;
  80. * false otherwise.
  81. *
  82. * These operations must be atomic. You get to write them.
  83. */
  84. void lock_release(struct lock *);
  85. bool lock_do_i_hold(struct lock *);
  86. void lock_destroy(struct lock *);
  87. /*
  88. * Condition variable.
  89. *
  90. * Note that the "variable" is a bit of a misnomer: a CV is normally used
  91. * to wait until a variable meets a particular condition, but there's no
  92. * actual variable, as such, in the CV.
  93. *
  94. * These CVs are expected to support Mesa semantics, that is, no
  95. * guarantees are made about scheduling.
  96. *
  97. * The name field is for easier debugging. A copy of the name is
  98. * (should be) made internally.
  99. */
  100. struct cv {
  101. char *cv_name;
  102. // add what you need here
  103. // (don't forget to mark things volatile as needed)
  104. };
  105. struct cv *cv_create(const char *name);
  106. void cv_destroy(struct cv *);
  107. /*
  108. * Operations:
  109. * cv_wait - Release the supplied lock, go to sleep, and, after
  110. * waking up again, re-acquire the lock.
  111. * cv_signal - Wake up one thread that's sleeping on this CV.
  112. * cv_broadcast - Wake up all threads sleeping on this CV.
  113. *
  114. * For all three operations, the current thread must hold the lock passed
  115. * in. Note that under normal circumstances the same lock should be used
  116. * on all operations with any particular CV.
  117. *
  118. * These operations must be atomic. You get to write them.
  119. */
  120. void cv_wait(struct cv *cv, struct lock *lock);
  121. void cv_signal(struct cv *cv, struct lock *lock);
  122. void cv_broadcast(struct cv *cv, struct lock *lock);
  123. #endif /* _SYNCH_H_ */