Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timer_execute 为什么要调用两次 #1191

Closed
bitvcat opened this issue May 15, 2020 · 3 comments
Closed

timer_execute 为什么要调用两次 #1191

bitvcat opened this issue May 15, 2020 · 3 comments

Comments

@bitvcat
Copy link

bitvcat commented May 15, 2020

对于skynet 定时器的 timer_update ,一直疑惑为什么要调用两次 timer_execute,主要是为什么还要在 timer_shift(T); 前调用一次。

我个人理解,timer_update 应该只需要每隔 10ms tick 一次,然后派发tick后的定时器消息即可,
类似下面的执行过程:

static void 
timer_update(struct timer *T) {
	SPIN_LOCK(T);

	// shift time first, and then dispatch timer message
	timer_shift(T);

	// timer_execute
	int idx = T->time & TIME_NEAR_MASK;
	while (T->near[idx].head.next) {
		struct timer_node *current = link_clear(&T->near[idx]);
		SPIN_UNLOCK(T);

		// dispatch_list don't need lock T
		dispatch_list(current);
	}
}

例如,T->time 从 1 tick 到 2,这个过程中,如果在 T->time == 1 时又加入一个 timeout 0 的消息 A,根据 skynet_timeout 的逻辑,这个消息 A 会直接被 skynet_context_push 所消费掉,并不会插入到定时器中,那么在 10 ms 后,timer_update 的执行过程中,就不需要在 timer_shift 之前再调用一次 timer_execute,因为 near[1] 已经在上一轮被处理过了。

而我的疑惑是:为啥在 timer_shift(T); 前要多调用一次 timer_execute(T);,这样做的意图是什么呢?
希望有大佬能解惑一下,非常感谢。

@cloudwu
Copy link
Owner

cloudwu commented May 15, 2020

timeout 0 的消息怎么处理,并没有对 timer 这个模块做出什么承诺。

@bitvcat
Copy link
Author

bitvcat commented May 15, 2020

也就是说,是为了timer的模块化,timer本身是需要提供处理timeout 0的能力。

那可以这样认为吗,如果我这个timer就是只在skynet内使用,而skynet已经提供了处理timeout 0的逻辑,那么第一个timer_execute 其实也可以不需要的。我这样理解对吗?

@bitvcat bitvcat closed this as completed May 16, 2020
@xjdrew
Copy link
Contributor

xjdrew commented Mar 3, 2021

timeout 0 的消息怎么处理,并没有对 timer 这个模块做出什么承诺。

timer 的用户都在这个文件,skynet_timeout检查time的合法性,等价于timer的承诺。建议把timer_execute重复执行去掉。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants