https://www.liblfds.org/mediawiki/index.php?title=r7.1.0:Queue_(bounded,_single_producer,_single_consumer)&feed=atom&action=historyr7.1.0:Queue (bounded, single producer, single consumer) - Revision history2024-03-29T06:31:52ZRevision history for this page on the wikiMediaWiki 1.36.0https://www.liblfds.org/mediawiki/index.php?title=r7.1.0:Queue_(bounded,_single_producer,_single_consumer)&diff=786&oldid=prevAdmin: /* Example */2016-05-31T01:34:58Z<p><span dir="auto"><span class="autocomment">Example</span></span></p>
<p><b>New page</b></p><div>{{DISPLAYTITLE:Queue (bounded, single consumer, single producer)}}<br />
==Source Files==<br />
└───liblfds710<br />
├───inc<br />
│ └───liblfds710<br />
│ lfds710_queue_bounded_singleproducer_singleconsumer.h<br />
└───src<br />
└───lfds710_queue_bounded_singleproducer_singleconsumer<br />
lfds710_queue_bounded_singleproducer_singleconsumer_cleanup.c<br />
lfds710_queue_bounded_singleproducer_singleconsumer_dequeue.c<br />
lfds710_queue_bounded_singleproducer_singleconsumer_enqueue.c<br />
lfds710_queue_bounded_singleproducer_singleconsumer_init.c<br />
lfds710_queue_bounded_singleproducer_singleconsumer_internal.h<br />
lfds710_queue_bounded_singleproducer_singleconsumer_query.c<br />
<br />
==Enums==<br />
enum [[r7.1.0:enum lfds710_queue_bss_query|lfds710_queue_bss_query]];<br />
<br />
==Opaque Structures==<br />
struct [[r7.1.0:struct lfds710_queue_bss_element|lfds710_queue_bss_element]];<br />
struct [[r7.1.0:struct lfds710_queue_bss_state|lfds710_queue_bss_state]];<br />
<br />
==Macros==<br />
#define [[r7.1.0:macro LFDS710_QUEUE_BSS_GET_USER_STATE_FROM_STATE|LFDS710_QUEUE_BSS_GET_USER_STATE_FROM_STATE]]( queue_bbs_state )<br />
<br />
==Prototypes==<br />
void [[r7.1.0:function lfds710_queue_bss_init_valid_on_current_logical_core|lfds710_queue_bss_init_valid_on_current_logical_core]]( struct lfds710_queue_bss_state *qbsss, <br />
struct lfds710_queue_bss_element *element_array,<br />
lfds710_uint_t number_elements,<br />
void *user_state );<br />
<br />
void [[r7.1.0:function lfds710_queue_bss_cleanup|lfds710_queue_bss_cleanup]]( struct lfds710_queue_bss_state *qbsss,<br />
void (*element_cleanup_callback)(struct lfds710_queue_bss_state *qbsss, void *key, void *value) );<br />
<br />
int [[r7.1.0:function lfds710_queue_bss_enqueue|lfds710_queue_bss_enqueue]]( struct lfds710_queue_bss_state *qbsss,<br />
void *key,<br />
void *value );<br />
<br />
int [[r7.1.0:function lfds710_queue_bss_dequeue|lfds710_queue_bss_dequeue]]( struct lfds710_queue_bss_state *qbsss,<br />
void **key,<br />
void **value );<br />
<br />
void [[r7.1.0:function lfds710_queue_bss_query|lfds710_queue_bss_query]]( struct lfds710_queue_bss_state *qbsss,<br />
enum lfds710_queue_bss_query query_type,<br />
void *query_input,<br />
void *query_output );<br />
<br />
==Overview==<br />
This API implements a single producer, single consumer, bounded queue, where bounded means the maximum number of elements is set at init time and cannot later be changed.<br />
<br />
The user passes to ''lfds710_queue_bss_init'' an array of ''struct lfds710_queue_bss_element'' which forms the store used for the queue. The caller is responsible for all memory allocation and deallocation.<br />
<br />
The user is able to enqueue and dequeue a key and a value, both of which are void pointers. The key is not used in any way, and is available simply for convenience when moving key/value data pairs around a system.<br />
<br />
Enqueuing only fails if the queue is full. Dequeuing only fails if the queue is empty.<br />
<br />
The queue state structure is publicly visible so the caller can declare on the stack, use ''sizeof'' for memory allocation, and insert into his own structures.<br />
<br />
==Lock-free Specific Behaviour==<br />
The queue supports the concurrent operation of one reader thread and one writer thread. The reading thread can be any thread which runs on the same logical core that the first read occurred upon, and the same is true for the writing thread - any thread can write, so long as it is one thread at a time, and all the threads are running on the same logical core as the logical core used for the first write.<br />
<br />
It may seem to work if this is not adhered to, but it's pure chance. Don't do it, it'll all end in tears.<br />
<br />
(Note that the OS may in its wisdom move threads around, to other logical cores; this is okay - when this happes, the OS does extra magic to make sure everything continues to work as it should. In fact, this API could offer similar functions, to handover/pickup ownership of the read/write thread, but it's not been done).<br />
<br />
It is not guaranteed that by the time ''lfds710_queue_bss_enqueue'' returns that the newly enqueued element will be dequeueable. There can be a delay, which in theory could be infinite, but in practise is typically on the order of tens of microseconds. The order of reads will however always match the order of writes - so it really is a queue (FIFO), not a random in-out mixer (RIRO :-)<br />
<br />
==White Paper==<br />
There is no white paper for this data structure. It is generally well-known and indeed is present in the Linux kernel.<br />
<br />
==License==<br />
Standard ''liblfds'' license - there is no license. You are free to use this code in any way. Go forth and create wealth!<br />
<br />
If however for legal reasons a licence is required, the license of your choice will be granted, and license for convenience is hereby granted up front for a range of popular licenses : the MIT license, the BSD license, the Apache license, the GPL and LPGL (all versions thereof) and the Creative Commons licenses (all of them). Additionally, this library (which is to say, the source code, build files, documentation, everything) is placed in the public domain.<br />
<br />
==Example==<br />
#include <stdio.h><br />
#include <string.h><br />
#include "liblfds710.h"<br />
<br />
struct test_data<br />
{<br />
char<br />
name[64];<br />
};<br />
<br />
int main()<br />
{<br />
struct lfds710_queue_bss_element<br />
qbsse[10];<br />
<br />
struct lfds710_queue_bss_state<br />
qbsss;<br />
<br />
struct test_data<br />
td,<br />
*temp_td;<br />
<br />
lfds710_queue_bss_init_valid_on_current_logical_core( &qbsss, qbsse, 10, NULL );<br />
<br />
strcpy( td.name, "Bob The Skutter" );<br />
<br />
lfds710_queue_bss_enqueue( *qbsss, NULL, &td );<br />
<br />
lfds710_queue_bss_dequeue( &qbsss, NULL, &temp_td );<br />
<br />
printf( "skutter name = %s\n", temp_td->name );<br />
<br />
lfds710_queue_bss_cleanup( &qbsss, NULL );<br />
<br />
return( EXIT_SUCCESS );<br />
}<br />
<br />
==See Also==<br />
* [[r7.1.0:Release_7.1.0_Documentation|Release 7.1.0 Documentation]]</div>Admin