RetroArch
coro.h
Go to the documentation of this file.
1 #ifndef CORO_H
2 #define CORO_H
3 
4 /*
5 Released under the CC0: https://creativecommons.org/publicdomain/zero/1.0/
6 */
7 
8 /* Use at the beginning of the coroutine, you must have declared a variable coro_t* coro */
9 #define CORO_ENTER() \
10  { \
11  CORO_again: ; \
12  switch ( coro->step ) { \
13  case CORO_BEGIN: ;
14 
15 /* Use to define labels which are targets to GOTO and GOSUB */
16 #define CORO_SUB( x ) \
17  case x: ;
18 
19 /* Use at the end of the coroutine */
20 #define CORO_LEAVE() \
21  } } \
22  do { return 0; } while ( 0 )
23 
24 /* Go to the x label */
25 #define CORO_GOTO( x ) \
26  do { \
27  coro->step = ( x ); \
28  goto CORO_again; \
29  } while ( 0 )
30 
31 /* Go to a subroutine, execution continues until the subroutine returns via RET */
32 #define CORO_GOSUB( x ) \
33  do { \
34  coro->stack[ coro->sp++ ] = __LINE__; \
35  coro->step = ( x ); \
36  goto CORO_again; \
37  case __LINE__: ; \
38  } while ( 0 )
39 
40 /* Returns from a subroutine */
41 #define CORO_RET() \
42  do { \
43  coro->step = coro->stack[ --coro->sp ]; \
44  goto CORO_again; \
45  } while ( 0 )
46 
47 /* Yields to the caller, execution continues from this point when the coroutine is resumed */
48 #define CORO_YIELD() \
49  do { \
50  coro->step = __LINE__; \
51  return 1; \
52  case __LINE__: ; \
53  } while ( 0 )
54 
55 /* The coroutine entry point, never use 0 as a label */
56 #define CORO_BEGIN 0
57 
58 /* Sets up the coroutine */
59 #define CORO_SETUP() \
60  do { \
61  coro->step = CORO_BEGIN; \
62  coro->sp = 0; \
63  } while ( 0 )
64 
65 #define CORO_STOP() \
66  do { \
67  return 0; \
68  } while ( 0 )
69 
70 /* Add this macro to your coro_t structure containing the variables for the coroutine */
71 #define CORO_FIELDS \
72  int step, sp; \
73  int stack[ 8 ];
74 
75 #endif /* CORO_H */