Clean up nonblocking path

master
Nick Krichevsky 2019-09-26 01:06:04 -04:00
parent b9f8289ffd
commit 69b534dad7
1 changed files with 24 additions and 17 deletions

41
main.c
View File

@ -193,7 +193,7 @@ static int send_termination_messages(int num_threads) {
* @param num_threads The number of threads that are running. Termination signals will be sent to all threads from
* 1 through num_threads.
*
* @return int -1
* @return int -1 on error, 0 otherwise.
*/
static int handle_blocking_input(struct input *inputs, int num_inputs, int num_threads) {
int send_result = send_input_messages(inputs, num_inputs);
@ -321,6 +321,28 @@ static int cycle_send_input_messages(struct input *inputs, int num_inputs, int n
return 0;
}
/**
* Send all of the inputs and the termination messages to adder threads without blocking on sends.
*
* @param inputs An array of all of the inputs to send.
* @param num_inputs The number of items in the inputs array.
* @param num_threads The number of threads that are running. Termination signals will be sent to all threads from
* 1 through num_threads.
*
* @return int -1 on error, 0 otherwise.
*/
static int handle_nonblocking_input(struct input *inputs, int num_inputs, int num_threads) {
int send_result = cycle_send_input_messages(inputs, num_inputs, num_threads);
if (send_result != 0) {
printf("Failed to send input to a mailbox.\n");
return -1;
}
return 0;
}
/**
* Collect n messages from mailbox 0
*
@ -401,11 +423,7 @@ int main(int argc, char *argv[]) {
int send_result;
if (non_blocking) {
send_result = cycle_send_input_messages(inputs, num_inputs, num_adder_threads);
// TODO: Remove this once we fix non-blocking termination
if (send_result != 0) {
printf("Failed to send to mailbox.\n");
}
send_result = handle_nonblocking_input(inputs, num_inputs, num_adder_threads);
} else {
send_result = handle_blocking_input(inputs, num_inputs, num_adder_threads);
}
@ -417,17 +435,6 @@ int main(int argc, char *argv[]) {
return 1;
}
// TODO: Remove this when we fix non-blocking termination
if (non_blocking) {
send_result = send_termination_messages(num_adder_threads);
if (send_result != 0) {
printf("Failed to send termination message to mailbox.\n");
kill_threads(adder_threads, num_adder_threads);
free_mailboxes();
return 1;
}
}
int join_result = join_adder_threads(adder_threads, num_adder_threads);
if (join_result != 0) {
// If one of the joins failed, we have to tell the collection thread we're done.