理解 Memory barrier(内存屏障)【转】 (7)

const unsigned char *buffer, unsigned int len)

{

unsigned int l;

 

len = min(len, fifo->size - fifo->in + fifo->out);

 

/*

* Ensure that we sample the fifo->out index -before- we

* start putting bytes into the kfifo.

*/

 

smp_mb();

 

/* first put the data starting from fifo->in to buffer end */

l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));

memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);

 

/* then put the rest (if any) at the beginning of the buffer */

memcpy(fifo->buffer, buffer + l, len - l);

 

/*

* Ensure that we add the bytes to the kfifo -before-

* we update the fifo->in index.

*/

 

smp_wmb();

 

fifo->in += len;

 

return len;

}

EXPORT_SYMBOL(__kfifo_put);

 

/**

* __kfifo_get - gets some data from the FIFO, no locking version

* @fifo: the fifo to be used.

* @buffer: where the data must be copied.

* @len: the size of the destination buffer.

*

* This function copies at most @len bytes from the FIFO into the

* @buffer and returns the number of copied bytes.

*

* Note that with only one concurrent reader and one concurrent

* writer, you don\'t need extra locking to use these functions.

*/

unsigned int __kfifo_get(struct kfifo *fifo,

unsigned char *buffer, unsigned int len)

{

unsigned int l;

 

len = min(len, fifo->in - fifo->out);

 

/*

* Ensure that we sample the fifo->in index -before- we

* start removing bytes from the kfifo.

*/

 

smp_rmb();

 

/* first get the data from fifo->out until the end of the buffer */

l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsdzs.html