Hi,
I was learning about coroutines and I like it beside I did a similar code using void pointers which contain a lot of casting, so I for now like the coroutines more.
I did a small example on desktop compiler.
Codeblocks, 64-bit machine, running on Windows 10.
This is the code parts.
1. Example functions:
Code:
bool function1(void){
BeginCoroutine;
printf("\n1 part1\n");
co_delay(500);
printf("\n1 part2\n");
co_delay(500);
EndCoroutine;
}
bool function2(void){
BeginCoroutine;
printf("\n2 part1\n");
co_delay(500);
printf("\n2 part2\n");
co_delay(500);
EndCoroutine;
}
// in function5 and function6, I on purpose doing one delay
bool function5(void){
BeginCoroutine;
printf("\n5 part1\n");
co_delay(500);
printf("\n5 part2\n");
EndCoroutine;
}
bool function6(void){
BeginCoroutine;
printf("\n6 part1\n");
co_delay(500);
printf("\n6 part2\n");
EndCoroutine;
}
2. Coroutines header:
Code:
//////////////////////////////////////////////////////////////////////////////////
// definitions
#define START_TASK_COUNTER(c) task_counter = c;
#define BeginCoroutine static int state = 0; \
switch(state) \
{ \
case 0:
#define co_delay(prd) do \
{ \
static unsigned long st_millis = 0; \
for(st_millis = millis();millis() - st_millis < prd;)\
{ \
do \
{ \
state = __LINE__; \
return Yielding; \
case __LINE__: \
; \
}while (0); \
} \
} \
while (0)
#define EndCoroutine } \
task_counter--; \
printf("task %d finished\n",task_counter); \
state = 0; \
return Done
//////////////////////////////////////////////////////////////////////////////////
// enumerations
typedef enum { Done, Yielding } RoutineState;
// variables
uint8_t task_counter;
3. Coroutines source:
Code:
unsigned long millis(void){
unsigned long t_val;
t_val = GetTickCount();
return t_val;
}
4. main
Code:
void coroutine1(void){
START_TASK_COUNTER(2);
printf("\ncoroutine1 -------------------------------\n");
printf("tasks counts# %d\n",task_counter);
while(task_counter)
{
function1();
function2();
}
}
void coroutine2(void){
START_TASK_COUNTER(2);
printf("\ncoroutine2 -------------------------------\n");
printf("tasks counts# %d\n",task_counter);
while(task_counter)
{
function5();
function6();
}
}
int main(){
RunCoroutine(coroutine1);
RunCoroutine(coroutine2);
return 0;
}
=========================================
Now here 2 console outputs, one is perfect and the next one is different.
Example console output:
run#1: Perfect run time, no extra executions and each function part is in order:
Code:
coroutine1 -------------------------------
tasks counts# 2
1 part1
2 part1
1 part2
2 part2
task 1 finished
task 0 finished
coroutine2 -------------------------------
tasks counts# 2
5 part1
6 part1
5 part2
task 1 finished
6 part2
task 0 finished
run#2: Here, note that has executed again, after the coroutine should be finished:
Code:
coroutine1 -------------------------------
tasks counts# 2
1 part1
2 part1
1 part2
2 part2
task 1 finished
task 0 finished
2 part1
coroutine2 -------------------------------
tasks counts# 2
5 part1
6 part1
5 part2
task 1 finished
6 part2
task 0 finished
What could be the problem ?