/* Fetch a message from queue 'q' and store it in 'm' */ int mbox_recv(int q, msg_t * m) { mbox_t *box = &Q[q]; int sz; int i; lock_acquire(&box->l); while (box->count < 1) condition_wait(&box->l, &box->moreData); /* the size field can also be fragmented, so assemble it into m */ for (i = 0; i < MSG_T_HEADER_SIZE; i++) ((char *)m)[i] = box->buffer[(box->tail + i) % BUFFER_SIZE]; sz = MSG_SIZE(m); for(; i < sz; i++) ((char *)m)[i] = box->buffer[(box->tail + i) % BUFFER_SIZE]; box->tail = (box->tail + sz) % BUFFER_SIZE; box->count--; /* notfiy everyone so that a message will be sent if *any* process has a message that fits into the buffer */ condition_broadcast(&box->moreSpace); lock_release(&box->l); return sz; /*whatever - not a single caller checks the return value*/ } /* Insert 'm' into the mailbox 'q' */ int mbox_send(int q, msg_t * m) { mbox_t *box = &Q[q]; int sz = MSG_SIZE(m); int i; lock_acquire(&box->l); while (space_available(box) < sz) condition_wait(&box->l, &box->moreSpace); for (i = 0; i < sz; i++) box->buffer[(box->head + i) % BUFFER_SIZE] = ((char *)m)[i]; box->head = (box->head + sz) % BUFFER_SIZE; box->count++; condition_signal(&box->moreData); lock_release(&box->l); return sz; /*whatever - not a single caller checks the return value*/ }