|
| 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 | +aco_t* next_co(){ |
| 28 | + assert(curr_co_amount > 0); |
| 29 | + curr_co_index = (curr_co_index + 1) % curr_co_amount; |
| 30 | + return 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 | + aco_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 | + aco_share_stack_t* sstk = aco_share_stack_new(0); |
| 60 | + // NOTE: size_t_safe_mul |
| 61 | + coarray = (aco_t**) malloc(sizeof(void*) * co_amount); |
| 62 | + assertptr(coarray); |
| 63 | + memset(coarray, 0, sizeof(void*) * co_amount); |
| 64 | + size_t ct = 0; |
| 65 | + while(ct < co_amount){ |
| 66 | +#ifdef ACO_USE_VALGRIND |
| 67 | + aco_share_stack_t* private_sstk = aco_share_stack_new2( |
| 68 | + 0, ct % 2 |
| 69 | + ); |
| 70 | + coarray[ct] = aco_create( |
| 71 | + main_co, private_sstk, 0, co_fp0, |
| 72 | + (void*)((uintptr_t)rand() % 1000) |
| 73 | + ); |
| 74 | + private_sstk = NULL; |
| 75 | +#else |
| 76 | + coarray[ct] = aco_create( |
| 77 | + main_co, sstk, 0, co_fp0, |
| 78 | + (void*)((uintptr_t)rand() % 1000) |
| 79 | + ); |
| 80 | +#endif |
| 81 | + ct++; |
| 82 | + } |
| 83 | + |
| 84 | + // naive scheduler |
| 85 | + printf("scheduler start: co_amount:%zu\n", co_amount); |
| 86 | + aco_t* curr_co = coarray[curr_co_index]; |
| 87 | + while(curr_co_amount > 0){ |
| 88 | + aco_resume(curr_co); |
| 89 | + // Update curr_co because aco_yield_to() may have changed it |
| 90 | + curr_co = coarray[curr_co_index]; |
| 91 | + assert(curr_co->is_end != 0); |
| 92 | + printf("aco_destroy: co:%zu\n", curr_co_index); |
| 93 | + #ifdef ACO_USE_VALGRIND |
| 94 | + aco_share_stack_t* private_sstk = curr_co->share_stack; |
| 95 | + #endif |
| 96 | + aco_destroy(curr_co); |
| 97 | + #ifdef ACO_USE_VALGRIND |
| 98 | + aco_share_stack_destroy(private_sstk); |
| 99 | + private_sstk = NULL; |
| 100 | + #endif |
| 101 | + curr_co_amount--; |
| 102 | + if(curr_co_index < curr_co_amount){ |
| 103 | + coarray[curr_co_index] = coarray[curr_co_amount]; |
| 104 | + }else{ |
| 105 | + curr_co_index = 0; |
| 106 | + } |
| 107 | + coarray[curr_co_amount] = NULL; |
| 108 | + curr_co = coarray[curr_co_index]; |
| 109 | + } |
| 110 | + |
| 111 | + // co cleaning |
| 112 | + ct = 0; |
| 113 | + while(ct < co_amount){ |
| 114 | + assert(coarray[ct] == NULL); |
| 115 | + ct++; |
| 116 | + } |
| 117 | + aco_share_stack_destroy(sstk); |
| 118 | + sstk = NULL; |
| 119 | + aco_destroy(main_co); |
| 120 | + main_co = NULL; |
| 121 | + free(coarray); |
| 122 | + |
| 123 | + printf("sheduler exit"); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments