Add basic RecvMsg routine
parent
0b01c5f067
commit
c5735552a2
25
mailbox.c
25
mailbox.c
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue