Skip to content

Commit a055b82

Browse files
committed
add test_aco_tutorial_7.c to test and examplify aco_yield_to()
1 parent b1bc743 commit a055b82

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ One of the rules in libaco is to call `aco_exit()` to terminate the execution of
266266

267267
You could also define your own protector to substitute the default one (to do some customized "last words" stuff). But no matter in what case, the process will be aborted after the protector was executed. The `test_aco_tutorial_5.c` shows how to define the customized last word function.
268268

269-
The last example is a simple coroutine scheduler in `test_aco_tutorial_6.c`.
269+
The example `test_aco_tutorial_6.c` is a simple coroutine scheduler. And the last example, namely `test_aco_tutorial_7.c`, is the same scheduler using `aco_yield_to`.
270270

271271
# API
272272

Diff for: make.sh

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test_aco_tutorial_3 -lpthread
3030
test_aco_tutorial_4
3131
test_aco_tutorial_5
3232
test_aco_tutorial_6
33+
test_aco_tutorial_7
3334
test_aco_synopsis
3435
test_aco_benchmark
3536
'''

Diff for: test_aco_tutorial_7.c

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2018 Sen Han <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// A pretty simple scheduler demo using aco_yield_to().
16+
17+
#include "aco.h"
18+
#include <stdlib.h>
19+
#include <stdio.h>
20+
#include <unistd.h>
21+
#include "aco_assert_override.h"
22+
23+
size_t curr_co_amount;
24+
size_t curr_co_index;
25+
aco_t** coarray;
26+
27+
void yield_to_next_co(){
28+
assert(curr_co_amount > 0);
29+
curr_co_index = (curr_co_index + 1) % curr_co_amount;
30+
aco_yield_to(coarray[curr_co_index]);
31+
}
32+
33+
void co_fp0(){
34+
int ct = 0;
35+
int loop_ct = (int)((uintptr_t)(aco_get_co()->arg));
36+
if(loop_ct < 0){
37+
loop_ct = 0;
38+
}
39+
while(ct < loop_ct){
40+
yield_to_next_co();
41+
ct++;
42+
}
43+
aco_exit();
44+
}
45+
46+
int main() {
47+
aco_thread_init(NULL);
48+
49+
time_t seed_t = time(NULL);
50+
assert((time_t)-1 != seed_t);
51+
srand(seed_t);
52+
53+
size_t co_amount = 100;
54+
curr_co_amount = co_amount;
55+
56+
// create co
57+
assert(co_amount > 0);
58+
aco_t* main_co = aco_create(NULL, NULL, 0, NULL, NULL);
59+
// NOTE: size_t_safe_mul
60+
coarray = (aco_t**) malloc(sizeof(void*) * co_amount);
61+
assertptr(coarray);
62+
memset(coarray, 0, sizeof(void*) * co_amount);
63+
size_t ct = 0;
64+
while(ct < co_amount){
65+
aco_share_stack_t* private_sstk = aco_share_stack_new2(
66+
0, ct % 2
67+
);
68+
coarray[ct] = aco_create(
69+
main_co, private_sstk, 0, co_fp0,
70+
(void*)((uintptr_t)rand() % 1000)
71+
);
72+
private_sstk = NULL;
73+
ct++;
74+
}
75+
76+
// naive scheduler
77+
printf("scheduler start: co_amount:%zu\n", co_amount);
78+
aco_t* curr_co = coarray[curr_co_index];
79+
while(curr_co_amount > 0){
80+
aco_resume(curr_co);
81+
// Update curr_co because aco_yield_to() may have changed it
82+
curr_co = coarray[curr_co_index];
83+
assert(curr_co->is_end != 0);
84+
printf("aco_destroy: co currently at:%zu\n", curr_co_index);
85+
aco_share_stack_t* private_sstk = curr_co->share_stack;
86+
aco_destroy(curr_co);
87+
aco_share_stack_destroy(private_sstk);
88+
private_sstk = NULL;
89+
curr_co_amount--;
90+
if(curr_co_index < curr_co_amount){
91+
coarray[curr_co_index] = coarray[curr_co_amount];
92+
}else{
93+
curr_co_index = 0;
94+
}
95+
coarray[curr_co_amount] = NULL;
96+
curr_co = coarray[curr_co_index];
97+
}
98+
99+
// co cleaning
100+
ct = 0;
101+
while(ct < co_amount){
102+
assert(coarray[ct] == NULL);
103+
ct++;
104+
}
105+
aco_destroy(main_co);
106+
main_co = NULL;
107+
free(coarray);
108+
109+
printf("sheduler exit");
110+
111+
return 0;
112+
}

0 commit comments

Comments
 (0)