Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. #include "devices/timer.h"
  2. #include <debug.h>
  3. #include <inttypes.h>
  4. #include <round.h>
  5. #include <stdio.h>
  6. #include "devices/pit.h"
  7. #include "threads/interrupt.h"
  8. #include "threads/synch.h"
  9. #include "threads/thread.h"
  10.  
  11. /* See [8254] for hardware details of the 8254 timer chip. */
  12.  
  13. #if TIMER_FREQ < 19
  14. #error 8254 timer requires TIMER_FREQ >= 19
  15. #endif
  16. #if TIMER_FREQ > 1000
  17. #error TIMER_FREQ <= 1000 recommended
  18. #endif
  19.  
  20. /* Number of timer ticks since OS booted. */
  21. static int64_t ticks;
  22.  
  23. /* Number of loops per timer tick.
  24. Initialized by timer_calibrate(). */
  25. static unsigned loops_per_tick;
  26.  
  27. static intr_handler_func timer_interrupt;
  28. static bool too_many_loops (unsigned loops);
  29. static void busy_wait (int64_t loops);
  30. static void real_time_sleep (int64_t num, int32_t denom);
  31. static void real_time_delay (int64_t num, int32_t denom);
  32. static void wakeup_elapsed_time_thread(struct thread* thread , void* aux);
  33.  
  34. /* Sets up the timer to interrupt TIMER_FREQ times per second,
  35. and registers the corresponding interrupt. */
  36. void
  37. timer_init (void)
  38. {
  39. pit_configure_channel (0, 2, TIMER_FREQ);
  40. intr_register_ext (0x20, timer_interrupt, "8254 Timer");
  41. }
  42.  
  43. /* Calibrates loops_per_tick, used to implement brief delays. */
  44. void
  45. timer_calibrate (void)
  46. {
  47. unsigned high_bit, test_bit;
  48.  
  49. ASSERT (intr_get_level () == INTR_ON);
  50. printf ("Calibrating timer... ");
  51.  
  52. /* Approximate loops_per_tick as the largest power-of-two
  53. still less than one timer tick. */
  54. loops_per_tick = 1u << 10;
  55. while (!too_many_loops (loops_per_tick << 1))
  56. {
  57. loops_per_tick <<= 1;
  58. ASSERT (loops_per_tick != 0);
  59. }
  60.  
  61. /* Refine the next 8 bits of loops_per_tick. */
  62. high_bit = loops_per_tick;
  63. for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
  64. if (!too_many_loops (high_bit | test_bit))
  65. loops_per_tick |= test_bit;
  66.  
  67. printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
  68. }
  69.  
  70. /* Returns the number of timer ticks since the OS booted. */
  71. int64_t
  72. timer_ticks (void)
  73. {
  74. enum intr_level old_level = intr_disable ();
  75. int64_t t = ticks;
  76. intr_set_level (old_level);
  77. return t;
  78. }
  79.  
  80. /* Returns the number of timer ticks elapsed since THEN, which
  81. should be a value once returned by timer_ticks(). */
  82. int64_t
  83. timer_elapsed (int64_t then)
  84. {
  85. return timer_ticks () - then;
  86. }
  87.  
  88. /* Sleeps for approximately TICKS timer ticks. Interrupts must
  89. be turned on. */
  90. void
  91. timer_sleep (int64_t ticks)
  92. {
  93. int64_t start = timer_ticks ();
  94. /*set wake up time for thread to be blocked in order to allow
  95. handler to check if time elapsed for the thread */
  96.  
  97. ASSERT (intr_get_level () == INTR_ON);
  98. /* while (timer_elapsed (start) < ticks)
  99. thread_yield ();*/
  100. /*we will disable interrupt bec the thread might set his starting time then interrupted before it block itself so
  101. it will be sent to ready queue and this is fault it must be ent to blocked*/
  102. enum intr_level old_level;
  103. old_level = intr_disable();
  104. //
  105.  
  106. // struct thread *cur = thread_current ();
  107. thread_current()->wakeup_time = start + ticks;
  108. thread_block();
  109. intr_set_level(old_level);
  110. intr_enable();
  111.  
  112. }
  113.  
  114. /* Sleeps for approximately MS milliseconds. Interrupts must be
  115. turned on. */
  116. void
  117. timer_msleep (int64_t ms)
  118. {
  119. real_time_sleep (ms, 1000);
  120. }
  121.  
  122. /* Sleeps for approximately US microseconds. Interrupts must be
  123. turned on. */
  124. void
  125. timer_usleep (int64_t us)
  126. {
  127. real_time_sleep (us, 1000 * 1000);
  128. }
  129.  
  130. /* Sleeps for approximately NS nanoseconds. Interrupts must be
  131. turned on. */
  132. void
  133. timer_nsleep (int64_t ns)
  134. {
  135. real_time_sleep (ns, 1000 * 1000 * 1000);
  136. }
  137.  
  138. /* Busy-waits for approximately MS milliseconds. Interrupts need
  139. not be turned on.
  140.  
  141. Busy waiting wastes CPU cycles, and busy waiting with
  142. interrupts off for the interval between timer ticks or longer
  143. will cause timer ticks to be lost. Thus, use timer_msleep()
  144. instead if interrupts are enabled. */
  145. void
  146. timer_mdelay (int64_t ms)
  147. {
  148. real_time_delay (ms, 1000);
  149. }
  150.  
  151. /* Sleeps for approximately US microseconds. Interrupts need not
  152. be turned on.
  153.  
  154. Busy waiting wastes CPU cycles, and busy waiting with
  155. interrupts off for the interval between timer ticks or longer
  156. will cause timer ticks to be lost. Thus, use timer_usleep()
  157. instead if interrupts are enabled. */
  158. void
  159. timer_udelay (int64_t us)
  160. {
  161. real_time_delay (us, 1000 * 1000);
  162. }
  163.  
  164. /* Sleeps execution for approximately NS nanoseconds. Interrupts
  165. need not be turned on.
  166.  
  167. Busy waiting wastes CPU cycles, and busy waiting with
  168. interrupts off for the interval between timer ticks or longer
  169. will cause timer ticks to be lost. Thus, use timer_nsleep()
  170. instead if interrupts are enabled.*/
  171. void
  172. timer_ndelay (int64_t ns)
  173. {
  174. real_time_delay (ns, 1000 * 1000 * 1000);
  175. }
  176.  
  177. /* Prints timer statistics. */
  178. void
  179. timer_print_stats (void)
  180. {
  181. printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
  182. }
  183. /* Timer interrupt handler. */
  184. static void
  185. timer_interrupt (struct intr_frame *args UNUSED)
  186. {
  187. ticks++;
  188. thread_tick ();
  189. thread_foreach(wakeup_elapsed_time_thread,0);
  190. }
  191.  
  192. /* Returns true if LOOPS iterations waits for more than one timer
  193. tick, otherwise false. */
  194. static bool
  195. too_many_loops (unsigned loops)
  196. {
  197. /* Wait for a timer tick. */
  198. int64_t start = ticks;
  199. while (ticks == start)
  200. barrier ();
  201.  
  202. /* Run LOOPS loops. */
  203. start = ticks;
  204. busy_wait (loops);
  205.  
  206. /* If the tick count changed, we iterated too long. */
  207. barrier ();
  208. return start != ticks;
  209. }
  210.  
  211. /* Iterates through a simple loop LOOPS times, for implementing
  212. brief delays.
  213.  
  214. Marked NO_INLINE because code alignment can significantly
  215. affect timings, so that if this function was inlined
  216. differently in different places the results would be difficult
  217. to predict. */
  218. static void NO_INLINE
  219. busy_wait (int64_t loops)
  220. {
  221. while (loops-- > 0)
  222. barrier ();
  223. }
  224.  
  225. /* Sleep for approximately NUM/DENOM seconds. */
  226. static void
  227. real_time_sleep (int64_t num, int32_t denom)
  228. {
  229. /* Convert NUM/DENOM seconds into timer ticks, rounding down.
  230.  
  231. (NUM / DENOM) s
  232. ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
  233. 1 s / TIMER_FREQ ticks
  234. */
  235. int64_t ticks = num * TIMER_FREQ / denom;
  236.  
  237. ASSERT (intr_get_level () == INTR_ON);
  238. if (ticks > 0)
  239. {
  240. /* We're waiting for at least one full timer tick. Use
  241. timer_sleep() because it will yield the CPU to other
  242. processes. */
  243. timer_sleep (ticks);
  244. }
  245. else
  246. {
  247. /* Otherwise, use a busy-wait loop for more accurate
  248. sub-tick timing. */
  249. real_time_delay (num, denom);
  250. }
  251. }
  252.  
  253. /* Busy-wait for approximately NUM/DENOM seconds. */
  254. static void
  255. real_time_delay (int64_t num, int32_t denom)
  256. {
  257. /* Scale the numerator and denominator down by 1000 to avoid
  258. the possibility of overflow. */
  259. ASSERT (denom % 1000 == 0);
  260. busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));
  261. }
  262.  
  263. static void wakeup_elapsed_time_thread(struct thread* thread , void* aux){
  264. //check if thread is blocked and it's time waiting has elapsed then it has to
  265. //be unblocked.
  266. int64_t current_time = timer_ticks ();
  267. if( thread->status == THREAD_BLOCKED && current_time >= thread->wakeup_time){
  268. thread_unblock(thread);
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement