MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / put

Function put

examples/libc/ex2.c:41–56  ·  view source on GitHub ↗

Store an integer in the buffer */

Source from the content-addressed store, hash-verified

39
40/* Store an integer in the buffer */
41static 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

Callers 1

producerFunction · 0.70

Calls 4

pthread_mutex_lockFunction · 0.85
pthread_cond_waitFunction · 0.85
pthread_cond_signalFunction · 0.85
pthread_mutex_unlockFunction · 0.85

Tested by

no test coverage detected