Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflicts #6

Open
wants to merge 7 commits into
base: conflicts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
#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
Expand Down
33 changes: 17 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,35 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /** Self explanatory */
int count = 0;
int count_to = 0;


typedef struct {
int args[1];
int verbose;
int tick;
} arguments_t:
} arguments_t;


void errno_abort(char *message) {
void errno_abort(char* message) {
perror(message);
exit(EXIT_FAILURE);
}

void err_abort(int status, char *message);

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
arguments_t *arguments = state->input;
arguments_t *args = state->input;

switch (key) {
case 'v':
arguments->verbose = 1;
args->verbose = 1;
break;
case 't':
arguments->tick = (int)strtol(arg, NULL, 10);
args->tick = (int)strtol(arg, NULL, 10);
break;
case ARGP_KEY_ARG:
if (state->arg_num > 1)
argp_usage(state);
arguments->args[state->arg_num] = (int)strtol(arg, NULL, 10);
args->args[state->arg_num] = (int)strtol(arg, NULL, 10);
break;
case ARGP_KEY_END:
if (state->arg_num < 1)
Expand All @@ -96,7 +100,7 @@ void timer_callback(union sigval arg) {

error = pthread_mutex_lock(&mutex);
if (error != 0)
err_abort(error, "Callback locking");
errno_abort("Callback locking");

states_run();

Expand Down Expand Up @@ -143,7 +147,8 @@ void create_timer(int tick) {
}

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;

Expand All @@ -157,6 +162,7 @@ void statemachine_callback(void) {
states_get_state_count()); /** Switch to random next state */
}


int main(int argc, char **argv) {
int error;

Expand All @@ -175,12 +181,10 @@ int main(int argc, char **argv) {
arguments.verbose ? "yes" : "no", arguments.tick);

/** Initialize state machine */
states_add(state_probe, state_two_enter, state_two_run, state_two_ext,
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_add(state_probe, state_two_enter, state_two_run, state_two_exit,
state_second_e, SECOND_STATE_NAME);

states_set_callback(statemachine_callback);

Expand All @@ -192,8 +196,6 @@ int main(int argc, char **argv) {
create_timer(arguments.tick);

error = pthread_mutex_lock(&mutex);
if (error = 0)
err_abort(error, "Lock mutex");

while (count < count_to) {
/** Blocked thread can be awakened by a call to pthread_cond_signal */
Expand All @@ -209,11 +211,10 @@ int main(int argc, char **argv) {

printf("Finshed\n");

return;
}

void err_abort(int status, char *message) {
fprintf(stderr, "%s\n", message);
exit(status);
return 0;
}