Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. /* This file is derived from source code for the Nachos
  2. instructional operating system. The Nachos copyright notice
  3. is reproduced in full below. */
  4.  
  5. /* Copyright (c) 1992-1996 The Regents of the University of California.
  6. All rights reserved.
  7.  
  8. Permission to use, copy, modify, and distribute this software
  9. and its documentation for any purpose, without fee, and
  10. without written agreement is hereby granted, provided that the
  11. above copyright notice and the following two paragraphs appear
  12. in all copies of this software.
  13.  
  14. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO
  15. ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
  16. CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE
  17. AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA
  18. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19.  
  20. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  21. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  24. BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  26. MODIFICATIONS.
  27. */
  28.  
  29. #include "threads/synch.h"
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "threads/interrupt.h"
  33. #include "threads/thread.h"
  34.  
  35. /* Initializes semaphore SEMA to VALUE. A semaphore is a
  36. nonnegative integer along with two atomic operators for
  37. manipulating it:
  38.  
  39. - down or "P": wait for the value to become positive, then
  40. decrement it.
  41.  
  42. - up or "V": increment the value (and wake up one waiting
  43. thread, if any). */
  44. void
  45. sema_init (struct semaphore *sema, unsigned value)
  46. {
  47. ASSERT (sema != NULL);
  48.  
  49. sema->value = value;
  50. list_init (&sema->waiters);
  51. }
  52.  
  53. /* Down or "P" operation on a semaphore. Waits for SEMA's value
  54. to become positive and then atomically decrements it.
  55.  
  56. This function may sleep, so it must not be called within an
  57. interrupt handler. This function may be called with
  58. interrupts disabled, but if it sleeps then the next scheduled
  59. thread will probably turn interrupts back on. */
  60. void
  61. sema_down (struct semaphore *sema)
  62. {
  63. enum intr_level old_level;
  64.  
  65. ASSERT (sema != NULL);
  66. ASSERT (!intr_context ());
  67.  
  68. old_level = intr_disable ();
  69. while (sema->value == 0)
  70. {
  71. list_insert_ordered (&sema->waiters, &thread_current ()->elem,
  72. compare_thread_priority, NULL);
  73. thread_block ();
  74. }
  75. sema->value--;
  76. intr_set_level (old_level);
  77. }
  78.  
  79. /* Down or "P" operation on a semaphore, but only if the
  80. semaphore is not already 0. Returns true if the semaphore is
  81. decremented, false otherwise.
  82.  
  83. This function may be called from an interrupt handler. */
  84. bool
  85. sema_try_down (struct semaphore *sema)
  86. {
  87. enum intr_level old_level;
  88. bool success;
  89.  
  90. ASSERT (sema != NULL);
  91.  
  92. old_level = intr_disable ();
  93. if (sema->value > 0)
  94. {
  95. sema->value--;
  96. success = true;
  97. }
  98. else
  99. success = false;
  100. intr_set_level (old_level);
  101.  
  102. return success;
  103. }
  104.  
  105. /* Up or "V" operation on a semaphore. Increments SEMA's value
  106. and wakes up one thread of those waiting for SEMA, if any.
  107.  
  108. This function may be called from an interrupt handler. */
  109. void
  110. sema_up (struct semaphore *sema)
  111. {
  112. enum intr_level old_level;
  113. ASSERT (sema != NULL);
  114. old_level = intr_disable ();
  115. struct thread *t;
  116. if (!list_empty (&sema->waiters)){
  117. t = list_entry (list_pop_front (&sema->waiters), struct thread, elem);
  118. thread_unblock (t);
  119. }
  120. sema->value++;
  121. if(t->priority > thread_current()->priority) {
  122. if(intr_context())
  123. intr_yield_on_return();
  124. else
  125. thread_yield();
  126. }
  127. intr_set_level (old_level);
  128. }
  129.  
  130. static void sema_test_helper (void *sema_);
  131.  
  132. /* Self-test for semaphores that makes control "ping-pong"
  133. between a pair of threads. Insert calls to printf() to see
  134. what's going on. */
  135. void
  136. sema_self_test (void)
  137. {
  138. struct semaphore sema[2];
  139. int i;
  140.  
  141. printf ("Testing semaphores...");
  142. sema_init (&sema[0], 0);
  143. sema_init (&sema[1], 0);
  144. thread_create ("sema-test", PRI_DEFAULT, sema_test_helper, &sema);
  145. for (i = 0; i < 10; i++)
  146. {
  147. sema_up (&sema[0]);
  148. sema_down (&sema[1]);
  149. }
  150. printf ("done.\n");
  151. }
  152.  
  153. /* Thread function used by sema_self_test(). */
  154. static void
  155. sema_test_helper (void *sema_)
  156. {
  157. struct semaphore *sema = sema_;
  158. int i;
  159.  
  160. for (i = 0; i < 10; i++)
  161. {
  162. sema_down (&sema[0]);
  163. sema_up (&sema[1]);
  164. }
  165. }
  166. /* Initializes LOCK. A lock can be held by at most a single
  167. thread at any given time. Our locks are not "recursive", that
  168. is, it is an error for the thread currently holding a lock to
  169. try to acquire that lock.
  170.  
  171. A lock is a specialization of a semaphore with an initial
  172. value of 1. The difference between a lock and such a
  173. semaphore is twofold. First, a semaphore can have a value
  174. greater than 1, but a lock can only be owned by a single
  175. thread at a time. Second, a semaphore does not have an owner,
  176. meaning that one thread can "down" the semaphore and then
  177. another one "up" it, but with a lock the same thread must both
  178. acquire and release it. When these restrictions prove
  179. onerous, it's a good sign that a semaphore should be used,
  180. instead of a lock. */
  181. void
  182. lock_init (struct lock *lock)
  183. {
  184. ASSERT (lock != NULL);
  185.  
  186. lock->holder = NULL;
  187. sema_init (&lock->semaphore, 1);
  188. }
  189.  
  190. /* Acquires LOCK, sleeping until it becomes available if
  191. necessary. The lock must not already be held by the current
  192. thread.
  193.  
  194. This function may sleep, so it must not be called within an
  195. interrupt handler. This function may be called with
  196. interrupts disabled, but interrupts will be turned back on if
  197. we need to sleep. */
  198. void
  199. lock_acquire (struct lock *lock)
  200. {
  201. ASSERT (lock != NULL);
  202. ASSERT (!intr_context ());
  203. ASSERT (!lock_held_by_current_thread (lock));
  204.  
  205. sema_down (&lock->semaphore);
  206. lock->holder = thread_current ();
  207. }
  208.  
  209. /* Tries to acquires LOCK and returns true if successful or false
  210. on failure. The lock must not already be held by the current
  211. thread.
  212.  
  213. This function will not sleep, so it may be called within an
  214. interrupt handler. */
  215. bool
  216. lock_try_acquire (struct lock *lock)
  217. {
  218. bool success;
  219.  
  220. ASSERT (lock != NULL);
  221. ASSERT (!lock_held_by_current_thread (lock));
  222.  
  223. success = sema_try_down (&lock->semaphore);
  224. if (success)
  225. lock->holder = thread_current ();
  226. return success;
  227. }
  228.  
  229. /* Releases LOCK, which must be owned by the current thread.
  230.  
  231. An interrupt handler cannot acquire a lock, so it does not
  232. make sense to try to release a lock within an interrupt
  233. handler. */
  234. void
  235. lock_release (struct lock *lock)
  236. {
  237. ASSERT (lock != NULL);
  238. ASSERT (lock_held_by_current_thread (lock));
  239.  
  240. lock->holder = NULL;
  241. sema_up (&lock->semaphore);
  242. }
  243.  
  244. /* Returns true if the current thread holds LOCK, false
  245. otherwise. (Note that testing whether some other thread holds
  246. a lock would be racy.) */
  247. bool
  248. lock_held_by_current_thread (const struct lock *lock)
  249. {
  250. ASSERT (lock != NULL);
  251.  
  252. return lock->holder == thread_current ();
  253. }
  254. /* One semaphore in a list. */
  255. struct semaphore_elem
  256. {
  257. struct list_elem elem; /* List element. */
  258. struct semaphore semaphore; /* This semaphore. */
  259. int priority;
  260. };
  261.  
  262. /* Initializes condition variable COND. A condition variable
  263. allows one piece of code to signal a condition and cooperating
  264. code to receive the signal and act upon it. */
  265. void
  266. cond_init (struct condition *cond)
  267. {
  268. ASSERT (cond != NULL);
  269.  
  270. list_init (&cond->waiters);
  271. }
  272.  
  273. int
  274. compare_cond_priority(struct list_elem *elem,
  275. struct list_elem *e, void *aux){
  276.  
  277. struct semaphore_elem *semaA = NULL;
  278. struct semaphore_elem *semaB = NULL;
  279.  
  280. semaA = list_entry(e,struct semaphore_elem, elem);
  281. semaB = list_entry(elem,struct semaphore_elem, elem);
  282.  
  283. return semaA->priority < semaB->priority;
  284. }
  285.  
  286.  
  287. /* Atomically releases LOCK and waits for COND to be signaled by
  288. some other piece of code. After COND is signaled, LOCK is
  289. reacquired before returning. LOCK must be held before calling
  290. this function.
  291.  
  292. The monitor implemented by this function is "Mesa" style, not
  293. "Hoare" style, that is, sending and receiving a signal are not
  294. an atomic operation. Thus, typically the caller must recheck
  295. the condition after the wait completes and, if necessary, wait
  296. again.
  297.  
  298. A given condition variable is associated with only a single
  299. lock, but one lock may be associated with any number of
  300. condition variables. That is, there is a one-to-many mapping
  301. from locks to condition variables.
  302.  
  303. This function may sleep, so it must not be called within an
  304. interrupt handler. This function may be called with
  305. interrupts disabled, but interrupts will be turned back on if
  306. we need to sleep. */
  307. void
  308. cond_wait (struct condition *cond, struct lock *lock)
  309. {
  310. struct semaphore_elem waiter;
  311.  
  312. ASSERT (cond != NULL);
  313. ASSERT (lock != NULL);
  314. ASSERT (!intr_context ());
  315. ASSERT (lock_held_by_current_thread (lock));
  316.  
  317. sema_init (&waiter.semaphore, 0);
  318. waiter.priority = lock->holder->priority;
  319. list_insert_ordered (&cond->waiters, &waiter.elem, compare_cond_priority, NULL);
  320.  
  321.  
  322. lock_release (lock);
  323. sema_down (&waiter.semaphore);
  324. lock_acquire (lock);
  325. }
  326.  
  327. /* If any threads are waiting on COND (protected by LOCK), then
  328. this function signals one of them to wake up from its wait.
  329. LOCK must be held before calling this function.
  330.  
  331. An interrupt handler cannot acquire a lock, so it does not
  332. make sense to try to signal a condition variable within an
  333. interrupt handler. */
  334. void
  335. cond_signal (struct condition *cond, struct lock *lock UNUSED)
  336. {
  337. ASSERT (cond != NULL);
  338. ASSERT (lock != NULL);
  339. ASSERT (!intr_context ());
  340. ASSERT (lock_held_by_current_thread (lock));
  341.  
  342. if (!list_empty (&cond->waiters))
  343. sema_up (&list_entry (list_pop_front (&cond->waiters),
  344. struct semaphore_elem, elem)->semaphore);
  345. }
  346.  
  347. /* Wakes up all threads, if any, waiting on COND (protected by
  348. LOCK). LOCK must be held before calling this function.
  349.  
  350. An interrupt handler cannot acquire a lock, so it does not
  351. make sense to try to signal a condition variable within an
  352. interrupt handler. */
  353. void
  354. cond_broadcast (struct condition *cond, struct lock *lock)
  355. {
  356. ASSERT (cond != NULL);
  357. ASSERT (lock != NULL);
  358.  
  359. while (!list_empty (&cond->waiters))
  360. cond_signal (cond, lock);
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement