win-pvdrivers

view liblfds.6/src/ringbuffer/ringbuffer_get_and_put.c @ 1025:aa2e51f67f7c

Fix hibernate under Win8. Change debugprints.
author James Harper <james.harper@bendigoit.com.au>
date Tue Feb 19 15:14:53 2013 +1100 (2013-02-19)
parents 6300617040e0
children
line source
1 #include "ringbuffer_internal.h"
7 /****************************************************************************/
8 struct freelist_element *ringbuffer_get_read_element( struct ringbuffer_state *rs, struct freelist_element **fe )
9 {
10 assert( rs != NULL );
11 assert( fe != NULL );
13 queue_dequeue( rs->qs, (void **) fe );
15 return( *fe );
16 }
22 /****************************************************************************/
23 struct freelist_element *ringbuffer_get_write_element( struct ringbuffer_state *rs, struct freelist_element **fe, int *overwrite_flag )
24 {
25 assert( rs != NULL );
26 assert( fe != NULL );
27 // TRD : overwrite_flag can be NULL
29 /* TRD : we try to obtain an element from the freelist
30 if we can, we populate it and add it to the queue
32 if we cannot, then the ringbuffer is full
33 so instead we grab the current read element and
34 use that instead
36 dequeue may fail since the queue may be emptied
37 during our dequeue attempt
39 so what we actually do here is a loop, attempting
40 the freelist and if it fails then a dequeue, until
41 we obtain an element
43 once we have an element, we queue it
45 you may be wondering why this operation is in a loop
46 remember - these operations are lock-free; anything
47 can happen in between
49 so for example the pop could fail because the freelist
50 is empty; but by the time we go to get an element from
51 the queue, the whole queue has been emptied back into
52 the freelist!
54 if overwrite_flag is provided, we set it to 0 if we
55 obtained a new element from the freelist, 1 if we
56 stole an element from the queue
57 */
59 do
60 {
61 if( overwrite_flag != NULL )
62 *overwrite_flag = 0;
64 freelist_pop( rs->fs, fe );
66 if( *fe == NULL )
67 {
68 ringbuffer_get_read_element( rs, fe );
70 if( overwrite_flag != NULL and *fe != NULL )
71 *overwrite_flag = 1;
72 }
73 }
74 while( *fe == NULL );
76 return( *fe );
77 }
83 /****************************************************************************/
84 void ringbuffer_put_read_element( struct ringbuffer_state *rs, struct freelist_element *fe )
85 {
86 assert( rs != NULL );
87 assert( fe != NULL );
89 freelist_push( rs->fs, fe );
91 return;
92 }
98 /****************************************************************************/
99 void ringbuffer_put_write_element( struct ringbuffer_state *rs, struct freelist_element *fe )
100 {
101 assert( rs != NULL );
102 assert( fe != NULL );
104 queue_enqueue( rs->qs, fe );
106 return;
107 }