Store an integer in the buffer */
| 39 | |
| 40 | /* Store an integer in the buffer */ |
| 41 | static void put(struct prodcons * b, int data) |
| 42 | { |
| 43 | pthread_mutex_lock(&b->lock); |
| 44 | /* Wait until buffer is not full */ |
| 45 | while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { |
| 46 | pthread_cond_wait(&b->notfull, &b->lock); |
| 47 | /* pthread_cond_wait reacquired b->lock before returning */ |
| 48 | } |
| 49 | /* Write the data and advance write pointer */ |
| 50 | b->buffer[b->writepos] = data; |
| 51 | b->writepos++; |
| 52 | if (b->writepos >= BUFFER_SIZE) b->writepos = 0; |
| 53 | /* Signal that the buffer is now not empty */ |
| 54 | pthread_cond_signal(&b->notempty); |
| 55 | pthread_mutex_unlock(&b->lock); |
| 56 | } |
| 57 | |
| 58 | /* Read and remove an integer from the buffer */ |
| 59 |
no test coverage detected