Add basic RecvMsg routine

master
Nick Krichevsky 2019-09-22 15:05:12 -04:00
parent 0b01c5f067
commit c5735552a2
2 changed files with 26 additions and 0 deletions

View File

@ -77,5 +77,30 @@ int SendMsg(int dest, struct msg *message) {
// Signal that someone can recieve a message, now that it's stored.
sem_post(&dest_mailbox->recv_sem);
return 0;
}
/**
* Receive a message from the given mailbox.
*
* @param mailbox_index The index of the mailbox to receive from.
* @param message A pointer to an already allocated message struct to store the recieved message in.
* @return int -1 on error, 0 otherwise.
*/
int RecvMsg(int mailbox_index, struct msg *message) {
if (mailbox_index >= num_mailboxes || mailbox_index < 0) {
return -1;
}
struct mailbox *src_mailbox = &mailboxes[mailbox_index];
int wait_result = sem_wait(&src_mailbox->recv_sem);
if (wait_result != 0) {
return -1;
}
memcpy(message, &src_mailbox->stored_message, sizeof(struct msg));
// Signal that someone can send a message to this mailbox now that we've taken the message out.
sem_post(&src_mailbox->send_sem);
return 0;
}

View File

@ -30,5 +30,6 @@ int init_mailboxes(int n);
void free_mailboxes();
// God I hate the caps, but this is the required definition.
int SendMsg(int dest, struct msg *message);
int RecvMsg(int mailbox_index, struct msg *message);
#endif