Helixis 1.0
Task Programming API
Data Structures | Defines | Functions
sources/liblfds/queue/queue_internal.h File Reference
#include "liblfds/liblfds_internal.h"

Go to the source code of this file.

Data Structures

struct  queue_state
struct  queue_element

Defines

#define QUEUE_STATE_UNKNOWN   -1
#define QUEUE_STATE_EMPTY   0
#define QUEUE_STATE_ENQUEUE_OUT_OF_PLACE   1
#define QUEUE_STATE_ATTEMPT_DEQUEUE   2
#define QUEUE_POINTER   0
#define QUEUE_COUNTER   1
#define QUEUE_PAC_SIZE   2

Functions

int queue_internal_freelist_init_function (void **user_data, void *user_state)
void queue_internal_freelist_delete_function (void *user_data, void *user_state)
void queue_internal_new_element_from_freelist (struct queue_state *qs, struct queue_element *qe[2], void *user_data)
void queue_internal_guaranteed_new_element_from_freelist (struct queue_state *qs, struct queue_element *qe[2], void *user_data)
void queue_internal_init_element (struct queue_state *qs, struct queue_element *qe[2], struct freelist_element *fe, void *user_data)
void queue_internal_queue (struct queue_state *qs, struct queue_element *qe[2])
void queue_internal_validate (struct queue_state *qs, struct validation_info *vi, enum data_structure_validity *queue_validity, enum data_structure_validity *freelist_validity)

Define Documentation

#define QUEUE_COUNTER   1
#define QUEUE_PAC_SIZE   2
#define QUEUE_POINTER   0
#define QUEUE_STATE_ATTEMPT_DEQUEUE   2

Definition at line 12 of file queue_internal.h.

Referenced by queue_dequeue().

#define QUEUE_STATE_EMPTY   0

Definition at line 10 of file queue_internal.h.

Referenced by queue_dequeue().

#define QUEUE_STATE_ENQUEUE_OUT_OF_PLACE   1

Definition at line 11 of file queue_internal.h.

Referenced by queue_dequeue().

#define QUEUE_STATE_UNKNOWN   -1

Definition at line 9 of file queue_internal.h.

Referenced by queue_dequeue().


Function Documentation

void queue_internal_freelist_delete_function ( void *  user_data,
void *  user_state 
)

Definition at line 43 of file queue_delete.c.

References hlx_api::aligned_free_entry.

Referenced by queue_delete().

{
  gl_api.aligned_free_entry( user_data );

  return;
}
int queue_internal_freelist_init_function ( void **  user_data,
void *  user_state 
)

Definition at line 51 of file queue_new.c.

References ALIGN_DOUBLE_POINTER, and hlx_api::aligned_malloc_entry.

Referenced by queue_new().

{
  int
    rv = 0;

  *user_data = gl_api.aligned_malloc_entry( sizeof(struct queue_element), ALIGN_DOUBLE_POINTER );

  if( *user_data != 0 )
    rv = 1;

  return( rv );
}
void queue_internal_guaranteed_new_element_from_freelist ( struct queue_state qs,
struct queue_element qe[2],
void *  user_data 
)

Definition at line 93 of file queue_new.c.

References freelist_guaranteed_pop(), queue_state::fs, queue_internal_init_element(), and QUEUE_POINTER.

Referenced by queue_guaranteed_enqueue().

{
  struct freelist_element
    *fe;

  /* TRD : user_data can be any value in its range */

  qe[QUEUE_POINTER] = 0;

  freelist_guaranteed_pop( qs->fs, &fe );

  if( fe != 0 )
    queue_internal_init_element( qs, qe, fe, user_data );

  return;
}
void queue_internal_init_element ( struct queue_state qs,
struct queue_element qe[2],
struct freelist_element *  fe,
void *  user_data 
)
void queue_internal_new_element_from_freelist ( struct queue_state qs,
struct queue_element qe[2],
void *  user_data 
)

Definition at line 71 of file queue_new.c.

References freelist_pop(), queue_state::fs, queue_internal_init_element(), and QUEUE_POINTER.

Referenced by queue_enqueue(), and queue_new().

{
  struct freelist_element
    *fe;

  /* TRD : user_data can be any value in its range */

  qe[QUEUE_POINTER] = 0;

  freelist_pop( qs->fs, &fe );

  if( fe != 0 )
    queue_internal_init_element( qs, qe, fe, user_data );

  return;
}
void queue_internal_queue ( struct queue_state qs,
struct queue_element qe[2] 
)

Definition at line 52 of file queue_queue.c.

References ALIGN, ALIGN_DOUBLE_POINTER, and, hlx_api::atomic_dcas_entry, queue_state::enqueue, queue_element::next, QUEUE_COUNTER, QUEUE_PAC_SIZE, and QUEUE_POINTER.

Referenced by queue_enqueue(), and queue_guaranteed_enqueue().

{
  ALIGN(ALIGN_DOUBLE_POINTER) struct queue_element
    *enqueue[QUEUE_PAC_SIZE],
    *next[QUEUE_PAC_SIZE];

  unsigned char
    cas_result = 0;


  do
  {
    enqueue[QUEUE_POINTER] = qs->enqueue[QUEUE_POINTER];
    enqueue[QUEUE_COUNTER] = qs->enqueue[QUEUE_COUNTER];

    next[QUEUE_POINTER] = enqueue[QUEUE_POINTER]->next[QUEUE_POINTER];
    next[QUEUE_COUNTER] = enqueue[QUEUE_POINTER]->next[QUEUE_COUNTER];

    /* TRD : this if() ensures that the next we read, just above,
             really is from qs->enqueue (which we copied into enqueue)
    */

    if( qs->enqueue[QUEUE_POINTER] == enqueue[QUEUE_POINTER] and qs->enqueue[QUEUE_COUNTER] == enqueue[QUEUE_COUNTER] )
    {
      if( next[QUEUE_POINTER] == 0 )
      {
        qe[QUEUE_COUNTER] = next[QUEUE_COUNTER] + 1;
        cas_result = gl_api.atomic_dcas_entry( (volatile atom_t *) enqueue[QUEUE_POINTER]->next, (atom_t *) qe, (atom_t *) next );
      }
      else
      {
        next[QUEUE_COUNTER] = enqueue[QUEUE_COUNTER] + 1;
        gl_api.atomic_dcas_entry( (volatile atom_t *) qs->enqueue, (atom_t *) next, (atom_t *) enqueue );
      }
    }
  }
  while( cas_result == 0 );

  qe[QUEUE_COUNTER] = enqueue[QUEUE_COUNTER] + 1;
  gl_api.atomic_dcas_entry( (volatile atom_t *) qs->enqueue, (atom_t *) qe, (atom_t *) enqueue );

  return;
}
void queue_internal_validate ( struct queue_state qs,
struct validation_info vi,
enum data_structure_validity queue_validity,
enum data_structure_validity freelist_validity 
)

Definition at line 38 of file queue_query.c.

References and, queue_state::dequeue, freelist_query(), FREELIST_QUERY_ELEMENT_COUNT, FREELIST_QUERY_VALIDATE, queue_state::fs, validation_info::max_elements, validation_info::min_elements, queue_element::next, QUEUE_POINTER, VALIDITY_INVALID_ADDITIONAL_ELEMENTS, VALIDITY_INVALID_LOOP, VALIDITY_INVALID_MISSING_ELEMENTS, and VALIDITY_VALID.

Referenced by queue_query().

{
  struct queue_element
    *qe,
    *qe_slow,
    *qe_fast;

  atom_t
    element_count = 0,
    total_elements;

  struct validation_info
    freelist_vi;

  /* TRD : vi can be 0 */
  *queue_validity = VALIDITY_VALID;

  qe_slow = qe_fast = (struct queue_element *) qs->dequeue[QUEUE_POINTER];

  /* TRD : first, check for a loop
           we have two pointers
           both of which start at the dequeue end of the queue
           we enter a loop
           and on each iteration
           we advance one pointer by one element
           and the other by two

           we exit the loop when both pointers are 0
           (have reached the end of the queue)

           or

           if we fast pointer 'sees' the slow pointer
           which means we have a loop
  */

  if( qe_slow != 0 )
    do
    {
      qe_slow = qe_slow->next[QUEUE_POINTER];

      if( qe_fast != 0 )
        qe_fast = qe_fast->next[QUEUE_POINTER];

      if( qe_fast != 0 )
        qe_fast = qe_fast->next[QUEUE_POINTER];
    }
    while( qe_slow != 0 and qe_fast != qe_slow );

  if( qe_fast != 0 and qe_slow != 0 and qe_fast == qe_slow )
    *queue_validity = VALIDITY_INVALID_LOOP;

  /* TRD : now check for expected number of elements
           vi can be 0, in which case we do not check
           we know we don't have a loop from our earlier check
  */

  if( *queue_validity == VALIDITY_VALID and vi != 0 )
  {
    qe = (struct queue_element *) qs->dequeue[QUEUE_POINTER];

    while( qe != 0 )
    {
      element_count++;
      qe = (struct queue_element *) qe->next[QUEUE_POINTER];
    }

    /* TRD : remember there is a dummy element in the queue */
    element_count--;

    if( element_count < vi->min_elements )
      *queue_validity = VALIDITY_INVALID_MISSING_ELEMENTS;

    if( element_count > vi->max_elements )
      *queue_validity = VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
  }

  /* TRD : now we validate the freelist

           we may be able to check for the expected number of
           elements in the freelist

           if the caller has given us an expected min and max
           number of elements in the queue, then the total number
           of elements in the freelist, minus that min and max,
           gives us the expected number of elements in the
           freelist
  */

  if( vi != 0 )
  {
    freelist_query( qs->fs, FREELIST_QUERY_ELEMENT_COUNT, 0, (void *) &total_elements );

    /* TRD : remember there is a dummy element in the queue */
    total_elements--;

    freelist_vi.min_elements = total_elements - vi->max_elements;
    freelist_vi.max_elements = total_elements - vi->min_elements;

    freelist_query( qs->fs, FREELIST_QUERY_VALIDATE, (void *) &freelist_vi, (void *) freelist_validity );
  }

  if( vi == 0 )
    freelist_query( qs->fs, FREELIST_QUERY_VALIDATE, 0, (void *) freelist_validity );

  return;
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines