Snail OS 0.03 timer目录是定时器的实现

 timer的思想是来自《30天自制操作系统》,用核心数据结构双向链表实现,看起来工作的还不错。

// timer.h 作者:ideal, 创建时间2022-05-09。
#ifndef __TIMER_H
#define __TIMER_H
#include "global.h"

struct timer {
	struct timer* prev, *next;
	unsigned int status, timeout, ioq_data;
	struct ioqueue* ioq;
};

struct timer_list {
	struct timer head, tail;
};

struct timer_man {
	struct timer_list list;
	struct timer timers[1024];
};

extern unsigned int ticks;
extern struct timer_man* t_m;

void timer_man_init(void);
struct timer* timer_alloc(struct timer_man* tm);
void timer_list_init(struct timer_list* tl);
void timer_list_insert_before(struct timer* cur, struct timer* e);
void timer_list_push(struct timer_list* tl, struct timer* e);
void timer_list_append(struct timer_list* tl, struct timer* e);
struct timer* timer_list_remove(struct timer* e);
struct timer* timer_list_pop(struct timer_list* tl);
bool timer_list_empty(struct timer_list* tl);
struct timer* timer_list_traversal(struct timer_list* tl, unsigned int timeout);
int timer_set(struct timer_man* tm, unsigned int timeout, struct ioqueue* ioq, unsigned int ioq_data);

#endif
// timer.h 作者:ideal, 创建时间2022-05-09。
#include "timer.h"
#include "memory.h"
#include "ioqueue.h"
#include "intr_status_op.h"

struct timer_man* t_m;

void timer_man_init(void) {
	t_m = get_kernel_pages(up_pgs(sizeof(struct timer_man)));
	timer_list_init(&t_m->list);
	int i;
	for(i = 0; i < 1024; i++) {
		t_m->timers[i].status = 0; // 0未占用,1已占用
	}
}

struct timer* timer_alloc(struct timer_man* tm) {
	int i;
	for(i = 0; i < 1024; i++) {
		if(!(tm->timers[i].status)) {
			break;
		}
	}
	if(i == 1024) {
		return NULL;
	}
	return &tm->timers[i];
}

void timer_list_init(struct timer_list* tl) {
	tl->head.prev = NULL;
	tl->head.next = &tl->tail;
	tl->tail.prev = &tl->head;
	tl->tail.next = NULL;	
}

void timer_list_insert_before(struct timer* cur, struct timer* e) {
	cur->prev->next = e;
	e->prev = cur->prev;
	e->next = cur;
	cur->prev = e;
}

void timer_list_push(struct timer_list* tl, struct timer* e) {
	timer_list_insert_before(tl->head.next, e);
}

void timer_list_append(struct timer_list* tl, struct timer* e) {
	timer_list_insert_before(&tl->tail, e);
}

struct timer* timer_list_remove(struct timer* e) {
	e->prev->next = e->next;
	e->next->prev = e->prev;
	return e;
}

struct timer* timer_list_pop(struct timer_list* tl) {
	struct timer* e = tl->head.next;
	timer_list_remove(e);
	return e;
}

bool timer_list_empty(struct timer_list* tl) {
	return (tl->head.next == &tl->tail);
}

struct timer* timer_list_traversal(struct timer_list* tl, unsigned int timeout) {
	struct timer* t = tl->head.next;
	while(t != &tl->tail) {
		if(t->timeout > timeout) {
			break;
		}
		t = t->next;
	}
	return t;
}

int timer_set(struct timer_man* tm, unsigned int timeout, struct ioqueue* ioq, unsigned int ioq_data) {
	enum intr_status old_status = intr_disable();
	struct timer* e = timer_alloc(tm);
	if(!e) {
		return 0;
	}
	e->status = 1;
	e->timeout = timeout + ticks;
	e->ioq = ioq;
	e->ioq_data = ioq_data;
	struct timer* back = timer_list_traversal(&tm->list, timeout);
	if(!back) {
		return 0;
	}
// 在第一个比自己超时时间数值大的元素前插入,从而形成有序队列
	timer_list_insert_before(back, e);
	intr_set_status(old_status);
	return 1;
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值