11 /* function prototypes */
12 void *thread_alpha_gen(void *arg
);
13 void *thread_num_gen(void *arg
);
14 void *thread_consumer(void *arg
);
15 void dief(const char *s
);
16 void diep(const char *s
);
17 void print_char(void *data
);
20 void *(*th_pfunc
)(void *);
23 { thread_alpha_gen
, 0 },
24 { thread_num_gen
, 0 },
25 { thread_consumer
, 0 }
35 fsm_init(&fsm
, 2<<10, 5, 2);
37 /* Initialize state */
38 if (state_init(&steadystate
, 2<<10, 2) == ST_NOMEM
) {
40 dief("state_init(): ST_NOMEM");
43 /* Add events to state
45 * We have only one state named "steady state",
46 * which handles all events from 'A' to 'Z'
49 for (c
= 'A'; c
< 'Z'; c
++) {
50 if (state_add_evt(steadystate
, c
, "", print_char
, steadystate
) == ST_NOMEM
) {
51 dief("state_add_evt(): ST_NOMEM");
55 for (c
= '0'; c
< '9'; c
++) {
56 if (state_add_evt(steadystate
, c
, "", print_char
, steadystate
) == ST_NOMEM
) {
57 dief("state_add_evt(): ST_NOMEM");
62 /* Add steady state to fsm */
63 fsm_add_state(fsm
, 0, steadystate
);
65 /* Set initial state */
66 fsm_set_state(fsm
, 0);
68 /* Create threads (alpha generator, num generator and consumer) */
69 for (i
= 0; i
< NUM_THREADS
; i
++) {
70 if (pthread_create(&thtbl
[i
].th_id
, NULL
, thtbl
[i
].th_pfunc
, (void *)fsm
))
71 diep("pthread_create");
83 void *thread_alpha_gen(void *arg
)
88 /* Get a pointer to the fsm we are interested in */
91 /* Initialize random number generator */
94 /* Broadcast events */
96 c
= 'A' + rand() % 26; /* from 'A' to 'Z' */
97 fsm_queue_event(fsm
, c
, &c
, 1, 0);
103 void *thread_num_gen(void *arg
)
108 /* Get a pointer to the fsm we are interested in */
111 /* Initialize random number generator */
114 /* Broadcast events */
116 c
= '0' + rand() % 10; /* from '0' to '9' */
117 fsm_queue_event(fsm
, c
, &c
, 1, 0);
123 void *thread_consumer(void *arg
)
127 /* Get a pointer to the fsm we are interested in */
130 /* Thread acts as event consumer */
132 fsm_dequeue_event(fsm
);
137 void dief(const char *s
)
139 fprintf(stderr
, "error: %s\n", s
);
143 void diep(const char *s
)
149 void print_char(void *data
)
151 printf("%c", *(char *)data
);