Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
newNcy committed Mar 28, 2024
1 parent d33af6e commit 09950a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@
```c

#include "coroutine.h"
#include <stdio.h>

void func()
uint64_t s = 0;
void f(int i)
{
printf("func 1\n");
int local = i + 3;
printf("i: %d local:%d\n", i, local);
co_yield();
printf("func 2\n");
printf("i: %d local:%d\n", i, local);
}

int main()
int main(int argc, char * argv[])
{
// 初始化本线程的环境
co_init();

printf("main 1\n");
co_t * co = co_create(func, NULL);
co_t * co = co_create(f, 3);
s = ns();
co_resume(co);
printf("main 2\n");
printf("resume with %lld ns\n", ns() -s);
co_resume(co);
// 释放本线程环境
co_finish();
return 0;
}
```
Expand Down
3 changes: 3 additions & 0 deletions coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void co_init()
co_t * co_create(void * entry, void * args)
{
env_t * env = thread_env();
if (!env->inited) {
co_init();
}
co_t * co = NULL;
if (!list_empty(env->free_list)) {
co = list_pop_front(env->free_list);
Expand Down
1 change: 0 additions & 1 deletion tests/test_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void f(int i)

int main(int argc, char * argv[])
{
co_init();
co_t * co = co_create(f, 3);
s = ns();
co_resume(co);
Expand Down

0 comments on commit 09950a7

Please sign in to comment.