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");
84 void *thread_alpha_gen(void *arg
)
89 /* Get a pointer to the fsm we are interested in */
92 /* Initialize random number generator */
95 /* Broadcast events */
97 c
= 'A' + rand() % 26; /* from 'A' to 'Z' */
98 fsm_queue_event(fsm
, c
, &c
, 1, 0);
104 void *thread_num_gen(void *arg
)
109 /* Get a pointer to the fsm we are interested in */
112 /* Initialize random number generator */
115 /* Broadcast events */
117 c
= '0' + rand() % 10; /* from '0' to '9' */
118 fsm_queue_event(fsm
, c
, &c
, 1, 0);
124 void *thread_consumer(void *arg
)
128 /* Get a pointer to the fsm we are interested in */
131 /* Thread acts as event consumer */
133 fsm_dequeue_event(fsm
);
138 void dief(const char *s
)
140 fprintf(stderr
, "error: %s\n", s
);
144 void diep(const char *s
)
150 void print_char(void *data
)
152 printf("%c", *(char *)data
);