forked from JSchaenzle/cedux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
104 lines (88 loc) · 2.61 KB
/
example.c
File metadata and controls
104 lines (88 loc) · 2.61 KB
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
#include "cedux.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/time.h>
void timer_handler (int signum);
void setup_timer(void);
struct branch_a { int leaves; };
struct tree { struct branch_a a; };
enum action_type {
ADD_LEAVES,
REMOVE_LEAVES,
};
struct add_leaves_data { int count; };
struct subtract_leaves_data { int count; };
// Tagged union action type definition
struct my_action_def {
enum action_type type;
union {
struct add_leaves_data add_leaves_data;
struct subtract_leaves_data subtract_leaves_data;
};
};
// Normally this would be in a header file. This line defines a struct
// for the store handles called 'struct my_store_handle'
CEDUX_DECLARE_STORE(struct tree, struct my_action_def, my_store);
CEDUX_DEFINE_STORE(struct tree, struct my_action_def, my_store);
struct my_store_handle my_store;
void reducer_1(struct tree * p_tree, struct my_action_def action)
{
switch (action.type)
{
case ADD_LEAVES:
p_tree->a.leaves += action.add_leaves_data.count;
break;
case REMOVE_LEAVES:
p_tree->a.leaves -= action.add_leaves_data.count;
break;
default:
break;
}
}
void subscriber_func(const struct tree * p_tree, void *data)
{
int number = (int)data;
printf("Subscriber %d: Num leaves %d!\n", number, p_tree->a.leaves);
}
int main(void)
{
my_store = cedux_init_my_store(); // Initialize the internals of the store (list, queue)
cedux_register_my_store_reducer(&my_store, reducer_1);
cedux_register_my_store_subscriber(&my_store, subscriber_func, (void *)1);
cedux_register_my_store_subscriber(&my_store, subscriber_func, (void *)2);
setup_timer();
while(1) {
bool did_work = cedux_run_my_store(&my_store);
if (did_work) {
printf("Did work.\n");
}
}
}
void timer_handler (int signum)
{
cedux_dispatch_my_store(&my_store, (struct my_action_def){
.type = rand() % 2 != 1 ? ADD_LEAVES : REMOVE_LEAVES,
.add_leaves_data = {
.count = rand() % 100
}
});
}
void setup_timer(void)
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGVTALRM, &sa, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 250000;
/* Start a virtual timer. It counts down whenever this process is executing. */
setitimer (ITIMER_VIRTUAL, &timer, NULL);
}