1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include <unistd.h> #include <stdio.h> #include <pthread.h>
typedef enum { sta_origin=0, sta_running, sta_owencpu, sta_sleep_int, sta_sleep_unint }State;
static const char *statename[]={ "origin", "running", "owencpu", "sleep_int", "sleep_unint", };
typedef enum{ evt_fork=0, evt_sched, evt_wait, evt_wait_unint, evt_wake_up, evt_wake, }EventID;
typedef void (*CallBack)(void *) ;
typedef struct { State curState; EventID eventId; State nextState; CallBack action; }StateTransform ;
State globalState = sta_origin; pthread_t pid;
void action_callback(void *arg) { StateTransform *statTran = (StateTransform *)arg; if(statename[statTran->curState] == statename[statTran->nextState]) { printf("invalid event,state not change\n"); }else{ printf("call back state from %s --> %s\n", statename[statTran->curState], statename[statTran->nextState]); } }
StateTransform stateTran_0[]={ {sta_origin,evt_fork, sta_running,action_callback}, {sta_origin,evt_sched, sta_origin,NULL}, {sta_origin,evt_wait, sta_origin,NULL}, {sta_origin,evt_wait_unint,sta_origin,NULL}, {sta_origin,evt_wake_up, sta_origin,NULL}, {sta_origin,evt_wake, sta_origin,NULL}, };
StateTransform stateTran_1[]={ {sta_running,evt_fork, sta_running,NULL}, {sta_running,evt_sched, sta_owencpu,action_callback}, {sta_running,evt_wait, sta_running,NULL}, {sta_running,evt_wait_unint,sta_running,NULL}, {sta_running,evt_wake_up, sta_running,NULL}, {sta_running,evt_wake, sta_running,NULL}, };
StateTransform stateTran_2[]={ {sta_owencpu,evt_fork, sta_owencpu,NULL}, {sta_owencpu,evt_sched, sta_owencpu,NULL}, {sta_owencpu,evt_wait, sta_sleep_int,action_callback}, {sta_owencpu,evt_wait_unint,sta_sleep_unint,action_callback}, {sta_owencpu,evt_wake_up, sta_owencpu,NULL}, {sta_owencpu,evt_wake, sta_owencpu,NULL}, };
StateTransform stateTran_3[]={ {sta_sleep_int,evt_fork, sta_sleep_int,NULL}, {sta_sleep_int,evt_sched, sta_sleep_int,NULL}, {sta_sleep_int,evt_wait, sta_sleep_int,NULL}, {sta_sleep_int,evt_wait_unint,sta_sleep_int,NULL}, {sta_sleep_int,evt_wake_up, sta_sleep_int,NULL}, {sta_sleep_int,evt_wake, sta_running,action_callback}, };
StateTransform stateTran_4[]={ {sta_sleep_unint,evt_fork, sta_sleep_unint,NULL}, {sta_sleep_unint,evt_sched, sta_sleep_unint,NULL}, {sta_sleep_unint,evt_wait, sta_sleep_unint,NULL}, {sta_sleep_unint,evt_wait_unint,sta_sleep_unint,NULL}, {sta_sleep_unint,evt_wake_up, sta_running,action_callback}, {sta_sleep_unint,evt_wake, sta_sleep_unint,NULL}, };
void init_machine(void) { globalState = sta_origin; }
void *show_stat(void *arg) { int len; char buf[64]={0}; while(1) { sleep(1); printf("cur stat:%s\n",statename[globalState]); } }
#define STATETRANS(n) (stateTran_##n)
void do_action(StateTransform *statTran) { if(NULL == statTran) { perror("statTran is NULL\n"); return; } globalState = statTran->nextState; if(statTran->action != NULL) { statTran->action((void*)statTran); }else{ printf("invalid event,state not change\n"); } } void event_happen(unsigned int event) { switch(globalState) { case sta_origin: do_action(&STATETRANS(0)[event]);
break; case sta_running: do_action(&STATETRANS(1)[event]); break; case sta_owencpu: do_action(&STATETRANS(2)[event]); break; case sta_sleep_int: do_action(&STATETRANS(3)[event]); break; case sta_sleep_unint: do_action(&STATETRANS(4)[event]); break; default: printf("state is invalid\n"); break; } }
void main(void) { init_machine();
pthread_create(&pid, NULL,show_stat, NULL);
sleep(5); event_happen(evt_fork);
sleep(5); event_happen(evt_sched); sleep(5); event_happen(evt_sched); sleep(5); event_happen(evt_wait); sleep(5); event_happen(evt_wake); }
|