coroutine icon indicating copy to clipboard operation
coroutine copied to clipboard

save [stack, sp]function _save_stack疑问

Open Qinch opened this issue 7 years ago • 2 comments

_save_stack函数中dummy变量定义在coroutine_yield是不是更好?

static void
_save_stack(struct coroutine *C, char *top, char *bottom) {
       //top:stack bottom:sp
	assert(top - bottom <= STACK_SIZE);
	if (C->cap < top - bottom) {
		free(C->stack);
		C->cap = top-bottom;
		C->stack = malloc(C->cap);
	}
	C->size = top - bottom;
	memcpy(C->stack, bottom, C->size);
}

void
coroutine_yield(struct schedule * S) {
	int id = S->running;
	assert(id >= 0);
	struct coroutine * C = S->co[id];
	assert((char *)&C > S->stack);
	char dummy = 0;
	_save_stack(C,S->stack + STACK_SIZE, &dummy);
	C->status = COROUTINE_SUSPEND;
	S->running = -1;
	swapcontext(&C->ctx , &S->main);
}

Qinch avatar Oct 21 '18 15:10 Qinch

这样会多压一个,函数call栈

qingdujun avatar Oct 11 '19 07:10 qingdujun