function lfds700_ringbuffer_query

From liblfds.org
Revision as of 02:02, 30 December 2015 by Admin (talk | contribs) (→‎Example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source Files

└───liblfds700
    ├───inc
    │   └───liblfds700
    │           lfds700_ringbuffer.h
    └───src
        └───lfds700_ringbuffer
                lfds700_ringbuffer_query.c

Enums

enum lfds700_ringbuffer_query
{
  LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_GET_COUNT,
  LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_VALIDATE
};

Opaque Structures

struct lfds700_ringbuffer_state;

Prototype

void lfds700_ringbuffer_query( struct lfds700_ringbuffer_state *rs,
                               enum lfds700_ringbuffer_query query_type,
                               void *query_input,
                               void *query_output );

Parameters

struct lfds700_ringbuffer_state *rs

A pointer to an initialized struct lfds700_ringbuffer_state.

enum lfds700_ringbuffer_query query_type

An enum lfds700_ringbuffer_query, which indicates which query to perform.

void *query_input

A pointer to input data for the query, where the data varies by query;
LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_GET_COUNT
query_input is NULL.
LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_VALIDATE
query_input can be NULL, or or can be a pointer to a struct lfds700_misc_validation_info, which specifies an expected min/max count, in which case validation also counts the number of unread elements and check they fall within the specified range.

void *query_output

A pointer to output store for the query, where the output varies by query;
LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_GET_COUNT
Points to a lfds700_pal_uint_t, which is set to the number of unread elements in the ringbuffer.
LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_VALIDATE
Points to an enum lfds700_misc_validity, which is set to indicate the result of the validation operation.

Notes

All SINGLETHREADED queries can only be safely performed if no read or write operations occur while the operation runs. If read or write operations do occur during the execution of a SINGLETHREADED query, it is theoretically possible for the query to enter an infinite loop. Do not do this.

Example

#include <stdio.h>
#include "liblfds700.h"

int main()
{
  eunm lfds700_misc_validity
    v;

  lfds700_pal_uint_t
    element_count;

  struct lfds700_misc_prng_state
    ps;

  struct lfds700_ringbuffer_element
    *re;

  struct lfds700_ringbuffer_state
    rs;

  char
    key[64] = "quarks",
    value[64] = "all the way down";

  lfds700_misc_library_init_valid_on_current_logical_core();

  lfds700_misc_prng_init( &ps );

  re = malloc( sizeof(struct lfds700_ringbuffer_element) * 11 );

  lfds700_ringbuffer_init_valid_on_current_logical_core( &rs, re, 11, NULL );

  lfds700_ringbuffer_write( &rs, (void *) key, (void *) value, NULL, NULL, NULL, &ps );

  lfds700_ringbuffer_query( &ss, LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_GET_COUNT, NULL, &element_count );

  // TRD : prints 1
  printf( "element count = %llu\n", (int long long unsigned) element_count );

  vi.min_elements = 1;
  vi.max_elements = 1;

  lfds700_ringbuffer_query( &ss, LFDS700_RINGBUFFER_QUERY_SINGLETHREADED_VALIDATE, (void *) &vi, (void *) &v );

  if( v == LFDS700_MISC_VALIDITY_VALID )
    printf( "Well thank goodness for that\n" );

  if( v != LFDS700_MISC_VALIDITY_VALID )
    printf( "Oh bugger\n" );

  lfds700_ringbuffer_cleanup( rs, NULL );

  free( re );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also