time.c (1014B)
1 // GRR20235159 Gabriel Gioia de Brito 2 // PingPongOS - PingPong Operating System 3 4 // Este arquivo PODE/DEVE ser alterado. 5 6 // Gerência básica do tempo. 7 8 #include "dispatcher.h" 9 #include "hardware/cpu.h" 10 #include "task.h" 11 #include "tcb.h" 12 13 unsigned long ctime; 14 extern struct task_t* current; 15 extern struct task_t kernel_task; 16 17 bool klock; 18 int should_preempt; 19 20 void lock_kernel() 21 { 22 klock = 1; 23 } 24 25 void unlock_kernel() 26 { 27 klock = 0; 28 if (should_preempt) { 29 should_preempt = 0; 30 task_yield(); 31 } 32 } 33 34 unsigned int time() 35 { 36 return ctime; 37 } 38 39 void tick(int arg) 40 { 41 (void) arg; 42 ctime += 1; 43 44 if (current == NULL) 45 return; 46 if (current->is_system) 47 return; 48 if (klock) { 49 should_preempt = 1; 50 return; 51 } 52 53 current->quantum -= 1; 54 if (current->quantum == 0) 55 task_yield(); 56 } 57 58 void time_init() 59 { 60 ctime = 0; 61 klock = 0; 62 hw_irq_handle(IRQ_TIMER, tick); 63 hw_timer(1, 1); 64 } 65 66 void time_term() 67 { 68 hw_timer(0, 0); 69 }