Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.05 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.  
  33.  //
  34.  
  35.  
  36. /* Sets up the timer to interrupt TIMER_FREQ times per second,
  37.    and registers the corresponding interrupt. */
  38. void
  39. timer_init (void)
  40. {
  41.   pit_configure_channel (0, 2, TIMER_FREQ);
  42.   intr_register_ext (0x20, timer_interrupt, "8254 Timer");
  43. }
  44. struct sleeping_threads *head=NULL;
  45. struct sleeping_threads *root=NULL;
  46. struct sleeping_threads *root_temp=NULL;
  47. int lavax=0;
  48. int numOfProccess=0;
  49. void Sleeping_threads_init (int priority, int64_t tickses, int64_t StartTimeSleeping)
  50. {
  51.   struct sleeping_threads *tmp=(struct sleeping_threads*)malloc(sizeof(struct sleeping_threads));
  52.   tmp->sleeping_priority=priority;
  53.   tmp->ticksForGetUp=tickses;
  54.   tmp->startTimeSleep=StartTimeSleeping;
  55.   tmp->next=head;
  56.   root=tmp;
  57.   head=tmp;
  58.   numOfProccess++;
  59.   Sleeping_threads_control();
  60. }
  61. void Sleeping_threads_control()
  62. {
  63.    printf("Main Process");
  64.    printf("%d ", timer_elapsed (root->startTimeSleep));
  65.    printf("%d  \n", root->ticksForGetUp);
  66.   while (timer_elapsed (root->startTimeSleep) < root->ticksForGetUp)
  67.   {
  68.    // CheckForTurgetProc();
  69.     if (numOfProccess>1)
  70.     {
  71.       if (timer_elapsed (root->next->startTimeSleep) < root->next->ticksForGetUp)
  72.       {
  73.         //struct thread *tur = thread_current ();
  74.         //thread_unblock(root->next->proc); // разблокировать процесс
  75.         //enum intr_level old_level;
  76.         //old_level = intr_disable ();
  77.         if (root->next->proc->status==THREAD_BLOCKED)
  78.         {
  79.           printf("This is blocked process\n");
  80.         }
  81.           //root->next->proc->status=THREAD_READY; // заблокировать роцесс
  82.        // Sleeping_threads_block();
  83.           //thread_yield();
  84.        
  85.         //intr_set_level (old_level);
  86.         struct sleeping_threads *t;
  87.         if (numOfProccess>2)
  88.         {
  89.          
  90.           t=root->next;
  91.           t->proc->sleep_ident=0;
  92.          // printf("%s ", t->proc->name);
  93.           root->next=t->next;
  94.  
  95.           free(t);
  96.  
  97.  
  98.           //root_temp=root;
  99.           numOfProccess--;
  100.           //Sleeping_threads_print();
  101.         }
  102.         else
  103.         {
  104.           free(root->next);
  105.           root->next=NULL;
  106.           numOfProccess--;
  107.         }
  108.         InputReadyList(root->next->proc);
  109.  
  110.       }
  111.     }
  112.   }
  113. }
  114. void Sleeping_threads_add(int priority_1, int64_t tickses_1, int64_t StartTimeSleeping, struct thread *proces)
  115. {
  116.   struct sleeping_threads *tmp;
  117.   tmp=root_temp;
  118.   if (tmp->next==NULL || (tmp->ticksForGetUp<tickses_1 && tmp->next->ticksForGetUp>=tickses_1))
  119.   {
  120.    
  121.     struct sleeping_threads *p=NULL;
  122.     p=(struct sleeping_threads*)malloc(sizeof(struct sleeping_threads));
  123.     p->sleeping_priority=priority_1;
  124.     p->ticksForGetUp=tickses_1;
  125.     p->startTimeSleep=StartTimeSleeping;
  126.     p->proc=proces;
  127.     proces->sleep_ident=1;
  128.     //Sleeping_threads_block();
  129.    // thread_yield();
  130.     //printf("%s\n", p->proc->name);
  131.     //thread_block();
  132.     printf("Add sleeping process as: ");
  133.    
  134.     //thread_yield();
  135.     if (tmp->next!=NULL)
  136.     {
  137.       printf("NOT LAST ELEM\n");
  138.       p->next=tmp->next;
  139.     }
  140.     else
  141.     {
  142.       printf("LAST ELEM\n");
  143.       p->next=NULL;
  144.     }
  145.     tmp->next=p;
  146.    
  147.    
  148.     numOfProccess++;
  149.  
  150.     //root_temp=root;
  151.     //Sleeping_threads_print();
  152.     enum intr_level old_level;
  153.     old_level = intr_disable ();
  154.    
  155.     proces->status=THREAD_BLOCKED; // заблокировать роцесс
  156.     Sleeping_threads_block();
  157.  
  158.     intr_set_level (old_level);
  159.  
  160.   }
  161.   else
  162.   {
  163.     root_temp=tmp->next;
  164.     Sleeping_threads_add(priority_1, tickses_1, StartTimeSleeping, proces);
  165.   }
  166. }
  167. void Sleeping_threads_print()
  168. {
  169.   //struct sleeping_threads *tmp=(struct sleeping_threads*)malloc(sizeof(struct sleeping_threads));
  170.   if (root_temp!=NULL)
  171.   {
  172.     struct sleeping_threads *tmp;
  173.     tmp=root_temp;
  174.     printf("\n");
  175.     printf("%s  ", tmp->proc->name);
  176.     printf("Processe's prior=");
  177.     printf("%d", tmp->sleeping_priority);
  178.     printf(" Timing=");
  179.     printf("%d\n", tmp->ticksForGetUp);
  180.     printf("\n");
  181.     root_temp=tmp->next;
  182.  
  183.     //numOfProccess--;
  184.     Sleeping_threads_print();
  185.   }
  186. }
  187.  
  188.  
  189. /*static void Sleeping_threads_init(void)
  190. {
  191.   printf("Init complete\n");
  192.   t_user=malloc(sizeof(struct sleeping_threads));
  193.   t_user->next=NULL;
  194.   root_user=t_user->next;
  195.  
  196. }
  197. static void Sleeping_threads_add(struct sleeping_threads *t, int64_t timing, int prior)
  198. {
  199.   struct sleeping_threads *temp, *p;
  200.   temp=malloc(sizeof(struct sleeping_threads));
  201.   t=malloc(sizeof(struct sleeping_threads));
  202.   p=t->next;
  203.   t->next=temp;
  204.   temp->sleeping_priority=prior;
  205.   temp->ticksForGetUp=timing;
  206.   printf("Processe's prior=");
  207.   printf("%d", temp->sleeping_priority);
  208.   printf(" Timing=");
  209.   printf("%d\n", temp->ticksForGetUp);
  210.   temp->next=p;
  211.   t_user=temp;
  212. }
  213. static void Search_Place_for_process(struct sleeping_threads *wt, int64_t timing_1, int prior_1)
  214. {
  215.  
  216.   int64_t temp_time=timing_1;
  217.   int temp_prior=prior_1;
  218.   if (wt!=t_user)
  219.   {
  220.     printf("GO WAIT\n");
  221.     if (timing_1>wt->ticksForGetUp)
  222.     {
  223.       printf("GO NEXT\n");
  224.       wt=wt->next;
  225.       Search_Place_for_process(wt, temp_time, temp_prior);
  226.     }
  227.     else
  228.     {
  229.       printf("INPUT BETWEEN\n");
  230.       Sleeping_threads_add(wt, temp_time, temp_prior);
  231.     }
  232.  
  233.   }
  234.   else
  235.   {
  236.     printf("INPUT\n");
  237.     Sleeping_threads_add(wt, temp_time, temp_prior);
  238.   }
  239.  
  240.  
  241. }*/
  242.  
  243. /* Calibrates loops_per_tick, used to implement brief delays. */
  244. void
  245. timer_calibrate (void)
  246. {
  247.   unsigned high_bit, test_bit;
  248.  
  249.   ASSERT (intr_get_level () == INTR_ON);
  250.   printf ("Calibrating timer...  ");
  251.  
  252.   /* Approximate loops_per_tick as the largest power-of-two
  253.      still less than one timer tick. */
  254.   loops_per_tick = 1u << 10;
  255.   while (!too_many_loops (loops_per_tick << 1))
  256.     {
  257.       loops_per_tick <<= 1;
  258.       ASSERT (loops_per_tick != 0);
  259.     }
  260.  
  261.   /* Refine the next 8 bits of loops_per_tick. */
  262.   high_bit = loops_per_tick;
  263.   for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
  264.     if (!too_many_loops (high_bit | test_bit))
  265.       loops_per_tick |= test_bit;
  266.  
  267.   printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
  268. }
  269.  
  270. /* Returns the number of timer ticks since the OS booted. */
  271. int64_t
  272. timer_ticks (void)
  273. {
  274.   enum intr_level old_level = intr_disable ();
  275.   int64_t t = ticks;
  276.   intr_set_level (old_level);
  277.   return t;
  278. }
  279.  
  280. /* Returns the number of timer ticks elapsed since THEN, which
  281.    should be a value once returned by timer_ticks(). */
  282. int64_t
  283. timer_elapsed (int64_t then)
  284. {
  285.   return timer_ticks () - then;
  286. }
  287.  
  288. /* Sleeps for approximately TICKS timer ticks.  Interrupts must
  289.    be turned on. */
  290. void
  291. timer_sleep (int64_t ticks)
  292. {
  293.  
  294.   int64_t start = timer_ticks ();
  295.     //printf("Go to sleep_L");
  296.     //printf("%s\n",thread_current ()->name );
  297.   ASSERT (intr_get_level () == INTR_ON);
  298.  
  299.   //Sleeping_threads_add(root, ticks, thread_current()->priority);
  300.   if (lavax==0)
  301.   {
  302.     lavax=1;
  303.     Sleeping_threads_init(thread_current()->priority, ticks, start);
  304.   }
  305.   else
  306.   {
  307.     printf("NEW PROCESS WANT TO SLEEP\n");
  308.     root_temp=root;
  309.     Sleeping_threads_add(thread_current()->priority, ticks, start, thread_current());
  310.  
  311.     //root_temp=root;
  312.     //Sleeping_threads_print();
  313.   }
  314.  
  315.  
  316.  
  317.   if (numOfProccess==6)
  318.   {
  319.    
  320.   }
  321.   //while (timer_elapsed (start) < ticks)
  322.   // {
  323.    // printf("waitL\n");
  324.   //  thread_yield ();
  325.    //}
  326.  
  327. }
  328.  
  329. /* Sleeps for approximately MS milliseconds.  Interrupts must be
  330.    turned on. */
  331. void
  332. timer_msleep (int64_t ms)
  333. {
  334.   real_time_sleep (ms, 1000);
  335. }
  336.  
  337. /* Sleeps for approximately US microseconds.  Interrupts must be
  338.    turned on. */
  339. void
  340. timer_usleep (int64_t us)
  341. {
  342.   real_time_sleep (us, 1000 * 1000);
  343. }
  344.  
  345. /* Sleeps for approximately NS nanoseconds.  Interrupts must be
  346.    turned on. */
  347. void
  348. timer_nsleep (int64_t ns)
  349. {
  350.   real_time_sleep (ns, 1000 * 1000 * 1000);
  351. }
  352.  
  353. /* Busy-waits for approximately MS milliseconds.  Interrupts need
  354.    not be turned on.
  355.  
  356.    Busy waiting wastes CPU cycles, and busy waiting with
  357.    interrupts off for the interval between timer ticks or longer
  358.    will cause timer ticks to be lost.  Thus, use timer_msleep()
  359.    instead if interrupts are enabled. */
  360. void
  361. timer_mdelay (int64_t ms)
  362. {
  363.   real_time_delay (ms, 1000);
  364. }
  365.  
  366. /* Sleeps for approximately US microseconds.  Interrupts need not
  367.    be turned on.
  368.  
  369.    Busy waiting wastes CPU cycles, and busy waiting with
  370.    interrupts off for the interval between timer ticks or longer
  371.    will cause timer ticks to be lost.  Thus, use timer_usleep()
  372.    instead if interrupts are enabled. */
  373. void
  374. timer_udelay (int64_t us)
  375. {
  376.   real_time_delay (us, 1000 * 1000);
  377. }
  378.  
  379. /* Sleeps execution for approximately NS nanoseconds.  Interrupts
  380.    need not be turned on.
  381.  
  382.    Busy waiting wastes CPU cycles, and busy waiting with
  383.    interrupts off for the interval between timer ticks or longer
  384.    will cause timer ticks to be lost.  Thus, use timer_nsleep()
  385.    instead if interrupts are enabled.*/
  386. void
  387. timer_ndelay (int64_t ns)
  388. {
  389.   real_time_delay (ns, 1000 * 1000 * 1000);
  390. }
  391.  
  392. /* Prints timer statistics. */
  393. void
  394. timer_print_stats (void)
  395. {
  396.   printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
  397. }
  398. /* Timer interrupt handler. */
  399. static void
  400. timer_interrupt (struct intr_frame *args UNUSED)
  401. {
  402.   ticks++;
  403.   thread_tick ();
  404. }
  405.  
  406. /* Returns true if LOOPS iterations waits for more than one timer
  407.    tick, otherwise false. */
  408. static bool
  409. too_many_loops (unsigned loops)
  410. {
  411.   /* Wait for a timer tick. */
  412.   int64_t start = ticks;
  413.   while (ticks == start)
  414.     barrier ();
  415.  
  416.   /* Run LOOPS loops. */
  417.   start = ticks;
  418.   busy_wait (loops);
  419.  
  420.   /* If the tick count changed, we iterated too long. */
  421.   barrier ();
  422.   return start != ticks;
  423. }
  424.  
  425. /* Iterates through a simple loop LOOPS times, for implementing
  426.    brief delays.
  427.  
  428.    Marked NO_INLINE because code alignment can significantly
  429.    affect timings, so that if this function was inlined
  430.    differently in different places the results would be difficult
  431.    to predict. */
  432. static void NO_INLINE
  433. busy_wait (int64_t loops)
  434. {
  435.   while (loops-- > 0)
  436.     barrier ();
  437. }
  438.  
  439. /* Sleep for approximately NUM/DENOM seconds. */
  440. static void
  441. real_time_sleep (int64_t num, int32_t denom)
  442. {
  443.   /* Convert NUM/DENOM seconds into timer ticks, rounding down.
  444.          
  445.         (NUM / DENOM) s          
  446.      ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
  447.      1 s / TIMER_FREQ ticks
  448.   */
  449.   int64_t ticks = num * TIMER_FREQ / denom;
  450.  
  451.   ASSERT (intr_get_level () == INTR_ON);
  452.   if (ticks > 0)
  453.     {
  454.       /* We're waiting for at least one full timer tick.  Use
  455.          timer_sleep() because it will yield the CPU to other
  456.          processes. */                
  457.       timer_sleep (ticks);
  458.     }
  459.   else
  460.     {
  461.       /* Otherwise, use a busy-wait loop for more accurate
  462.          sub-tick timing. */
  463.       real_time_delay (num, denom);
  464.     }
  465. }
  466.  
  467. /* Busy-wait for approximately NUM/DENOM seconds. */
  468. static void
  469. real_time_delay (int64_t num, int32_t denom)
  470. {
  471.   /* Scale the numerator and denominator down by 1000 to avoid
  472.      the possibility of overflow. */
  473.   ASSERT (denom % 1000 == 0);
  474.   busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement