Dummy commit to test new ssh key
[eleutheria.git] / fsm / test_thread.c
blobab72469f7bd9ba8950b1dfdc5fa10f8d9e4ac2a3
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 /* Loop forever */
75 for (;;)
78 /* Free memory */
79 fsm_free(fsm);
81 return EXIT_SUCCESS;
84 void *thread_alpha_gen(void *arg)
86 fsm_t *fsm;
87 size_t c;
89 /* Get a pointer to the fsm we are interested in */
90 fsm = (fsm_t *)arg;
92 /* Initialize random number generator */
93 srand(time(NULL));
95 /* Broadcast events */
96 for (;;) {
97 c = 'A' + rand() % 26; /* from 'A' to 'Z' */
98 fsm_queue_event(fsm, c, &c, 1, 0);
101 pthread_exit(NULL);
104 void *thread_num_gen(void *arg)
106 fsm_t *fsm;
107 size_t c;
109 /* Get a pointer to the fsm we are interested in */
110 fsm = (fsm_t *)arg;
112 /* Initialize random number generator */
113 srand(time(NULL));
115 /* Broadcast events */
116 for (;;) {
117 c = '0' + rand() % 10; /* from '0' to '9' */
118 fsm_queue_event(fsm, c, &c, 1, 0);
121 pthread_exit(NULL);
124 void *thread_consumer(void *arg)
126 fsm_t *fsm;
128 /* Get a pointer to the fsm we are interested in */
129 fsm = (fsm_t *)arg;
131 /* Thread acts as event consumer */
132 for (;;)
133 fsm_dequeue_event(fsm);
135 pthread_exit(NULL);
138 void dief(const char *s)
140 fprintf(stderr, "error: %s\n", s);
141 exit(EXIT_FAILURE);
144 void diep(const char *s)
146 perror(s);
147 exit(EXIT_FAILURE);
150 void print_char(void *data)
152 printf("%c", *(char *)data);