We now have 2 generators and 1 consumer thread
[eleutheria.git] / misc / fsm / test_thread.c
blobf58de75d0e51f68f230e957fc78860bb016f8bf5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
5 #include "fsm.h"
6 #include "states.h"
7 #include "types.h"
9 #define NUM_THREADS 3
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);
19 struct mythread {
20 void *(*th_pfunc)(void *);
21 pthread_t th_id;
22 } thtbl[] = {
23 { thread_alpha_gen, 0 },
24 { thread_num_gen, 0 },
25 { thread_consumer, 0 }
28 int main(void)
30 fsm_t *fsm;
31 state_t *steadystate;
32 int c, i;
34 /* Initialize fsm */
35 fsm_init(&fsm, 2<<10, 5, 2);
37 /* Initialize state */
38 if (state_init(&steadystate, 2<<10, 2) == ST_NOMEM) {
39 fsm_free(fsm);
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'
47 * and '0' to '9'.
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");
52 fsm_free(fsm);
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");
58 fsm_free(fsm);
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");
74 for (;;)
77 /* Free memory */
78 fsm_free(fsm);
80 return EXIT_SUCCESS;
83 void *thread_alpha_gen(void *arg)
85 fsm_t *fsm;
86 size_t c;
88 /* Get a pointer to the fsm we are interested in */
89 fsm = (fsm_t *)arg;
91 /* Initialize random number generator */
92 srand(time(NULL));
94 /* Broadcast events */
95 for (;;) {
96 c = 'A' + rand() % 26; /* from 'A' to 'Z' */
97 fsm_queue_event(fsm, c, &c, 1, 0);
100 pthread_exit(NULL);
103 void *thread_num_gen(void *arg)
105 fsm_t *fsm;
106 size_t c;
108 /* Get a pointer to the fsm we are interested in */
109 fsm = (fsm_t *)arg;
111 /* Initialize random number generator */
112 srand(time(NULL));
114 /* Broadcast events */
115 for (;;) {
116 c = '0' + rand() % 10; /* from '0' to '9' */
117 fsm_queue_event(fsm, c, &c, 1, 0);
120 pthread_exit(NULL);
123 void *thread_consumer(void *arg)
125 fsm_t *fsm;
127 /* Get a pointer to the fsm we are interested in */
128 fsm = (fsm_t *)arg;
130 /* Thread acts as event consumer */
131 for (;;)
132 fsm_dequeue_event(fsm);
134 pthread_exit(NULL);
137 void dief(const char *s)
139 fprintf(stderr, "error: %s\n", s);
140 exit(EXIT_FAILURE);
143 void diep(const char *s)
145 perror(s);
146 exit(EXIT_FAILURE);
149 void print_char(void *data)
151 printf("%c", *(char *)data);