forked from alxhoff/git-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_states.patch
More file actions
135 lines (118 loc) · 3.3 KB
/
main_states.patch
File metadata and controls
135 lines (118 loc) · 3.3 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
diff --cc include/main.h
index 4981f46,d63dc1d..0000000
--- a/include/main.h
+++ b/include/main.h
@@@ -43,8 -43,8 +43,9 @@@
#define SECOND_STATE_NAME "Count up twice"
#define THIRD_STATE_NAME "Count down once"
- #define NANOS_IN_SEC 100000000
- #define NANOS_IN_MSEC 1000000
++
+ #define NANOS_IN_SEC 1000000000
-#define NANOS_IN_MSEC 10000000
++
#define MSEC_IN_NANO(MSEC) MSEC *NANOS_IN_MSEC
#define DEFAULT_TICK 100
diff --cc src/main.c
index c815c6e,1186700..0000000
--- a/src/main.c
+++ b/src/main.c
@@@ -51,13 -51,13 +51,17 @@@ static struct argp_option options[] =
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; /** Condition variable */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /** Self explanatory */
int count = 0;
-int *count_too = 0;
++
+int count_to = 0;
+
++
+
typedef struct {
int args[1];
int verbose;
int tick;
- } arguments_t:
++
+ } arguments_t;
void errno_abort(char *message) {
perror(message);
@@@ -91,6 -91,6 +95,12 @@@ static error_t parse_opt(int key, char
static struct argp argp = {options, parse_opt, args_doc, doc};
++int err_abort(int status, char *message) {
++ fprintf(stderr, "%s\n", message);
++ exit(status);
++ return 0;
++}
++
void timer_callback(union sigval arg) {
int error;
@@@ -107,6 -107,6 +117,7 @@@
}
error = pthread_mutex_unlock(&mutex);
++
if (error != 0)
err_abort(error, "Callback unlocking");
}
@@@ -140,11 -140,11 +151,14 @@@ void create_timer(int tick)
timer_settime(our_timer, 0, &timer_specs, 0); /** Set timer interval */
if (error == -1)
errno_abort("Setting timer");
++
}
void statemachine_callback(void) {
- my_states_data **cur_data = states_get_data();
++
+ my_states_data *cur_data = states_get_data();
+
+
int diff = cur_data->cur_val - cur_data->prev_val;
count += diff;
@@@ -157,7 -157,7 +171,9 @@@
states_get_state_count()); /** Switch to random next state */
}
-int main(int argc, char argv) {
++
+int main(int argc, char **argv) {
++
int error;
srand(time(NULL)); /** Init random numbers */
@@@ -175,13 -175,13 +191,16 @@@
arguments.verbose ? "yes" : "no", arguments.tick);
/** Initialize state machine */
- states_add(state_probe, state_two_enter, state_two_run, state_two_ext,
++
++
+ states_add(timer_callback, NULL, state_one_run, NULL, state_first_x,
+ FIRST_STATE_NAME);
+ states_add(state_probe, state_two_enter, state_two_run, state_two_exit,
state_second_e, SECOND_STATE_NAME);
states_add(state_probe, NULL, state_three_run, NULL, state_third_e,
THIRD_STATE_NAME);
- states_add(state_probe, NULL, state_one_run, NULL, state_first_e,
- FIRST_STATE_NAME);
+
+
states_set_callback(statemachine_callback);
states_init();
@@@ -192,7 -192,7 +211,9 @@@
create_timer(arguments.tick);
error = pthread_mutex_lock(&mutex);
- if (error = 0)
- if (!error)
++
++ if (error!=0)
++
err_abort(error, "Lock mutex");
while (count < count_to) {
@@@ -209,11 -209,10 +230,5 @@@
printf("Finshed\n");
- return;
- return -1;
--}
--
- void err_abort(int status, char *message) {
-int err_abort(int status, char *message) {
-- fprintf(stderr, "%s\n", message);
-- exit(status);
+ return 0;
}