#include<linux/circ_buf.h> ... spin_lock(&producer_lock); unsignedlong head = buffer->head; unsignedlong tail = READ_ONCE(buffer->tail); // 有空间可写 if(CIRC_SPACE(head, tail, buffer->size) >= 1) { // got addr to store something // insert one item into the head position of buffer structitem* item = buffer[head]; produce_item(item); smp_store_release(buffer->head, (head + 1) & (buffer->size -1 )); // wake_up() will make sure that the head is committed before waking anyone up wake_up(consumer); } spin_unlock(&producer_lock);
消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
spin_lock(&consumer_lock); // read index before reading contents at that index unsignedlong head = smp_load_acquire(buffer->head); unsignedlong tail = buffer->tail; // 有数据可读 if(CIRC_CNT(head, tail, buffer->size) >= 1) { // extract one item from the buffer structitem* item = buffer[tail]; consume_ite(item); // finish reading descriptor before incrementing tail smp_store_release(buffer->tail, (tail + 1) & (buffer->size - 1)); } spin_unlock(&consumer_lock);