2024-11-02 11:31:51 +00:00
|
|
|
#include <mango/sched.h>
|
|
|
|
|
#include <mango/printk.h>
|
|
|
|
|
#include <mango/cpu.h>
|
|
|
|
|
#include <mango/clock.h>
|
2023-04-30 21:09:36 +01:00
|
|
|
|
|
|
|
|
static void timeout_expiry(struct timer *timer)
|
|
|
|
|
{
|
|
|
|
|
struct thread *thread = timer->t_owner;
|
|
|
|
|
struct cpu_data *cpu = get_this_cpu();
|
|
|
|
|
struct runqueue *rq = &cpu->c_rq;
|
|
|
|
|
|
|
|
|
|
thread->tr_state = THREAD_READY;
|
|
|
|
|
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
rq_lock(rq, &flags);
|
|
|
|
|
rq_enqueue(rq, thread);
|
|
|
|
|
rq_unlock(rq, flags);
|
2023-05-01 08:26:13 +01:00
|
|
|
|
|
|
|
|
put_cpu(cpu);
|
2023-04-30 21:09:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add_timer(struct timer *timer)
|
|
|
|
|
{
|
|
|
|
|
struct cpu_data *cpu = get_this_cpu();
|
|
|
|
|
timer->t_cpu = cpu;
|
|
|
|
|
queue_push_back(&cpu->c_timers, &timer->t_entry);
|
|
|
|
|
put_cpu(cpu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void remove_timer(struct timer *timer)
|
|
|
|
|
{
|
|
|
|
|
if (!timer->t_cpu) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
preempt_disable();
|
|
|
|
|
queue_delete(&timer->t_cpu->c_timers, &timer->t_entry);
|
|
|
|
|
timer->t_cpu = NULL;
|
|
|
|
|
preempt_enable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long schedule_timeout(unsigned long ticks)
|
|
|
|
|
{
|
|
|
|
|
struct timer timer;
|
|
|
|
|
|
|
|
|
|
struct thread *self = current_thread();
|
|
|
|
|
|
|
|
|
|
timer.t_entry = QUEUE_ENTRY_INIT;
|
|
|
|
|
timer.t_expiry = clock_ticks + ticks;
|
|
|
|
|
timer.t_owner = self;
|
|
|
|
|
timer.t_callback = timeout_expiry;
|
|
|
|
|
|
|
|
|
|
self->tr_state = THREAD_SLEEPING;
|
|
|
|
|
|
|
|
|
|
add_timer(&timer);
|
|
|
|
|
|
|
|
|
|
schedule(SCHED_NORMAL);
|
|
|
|
|
|
|
|
|
|
remove_timer(&timer);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long milli_sleep(unsigned long ms)
|
|
|
|
|
{
|
|
|
|
|
unsigned long ticks = (ms * HZ) / 1000;
|
|
|
|
|
ticks = schedule_timeout(ticks);
|
|
|
|
|
return (ticks * 1000) / HZ;
|
|
|
|
|
}
|