Program Listing for File MPSCInbox.h

↰ Return to documentation for file (ADS/concurrent/MPSCInbox.h)

/*--------------------------------------------------------------------------*/
/*---------------------------- File MPSCInbox.h ----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_MPSCInbox
 #define __ADS_MPSCInbox
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"
#include "ADS/concurrent/EventCount.h"

#include <atomic>
#include <utility>
#include <vector>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS MPSCInbox -----------------------------*/
/*--------------------------------------------------------------------------*/

template< class T >
class MPSCInbox {

/*--------------------------------------------------------------------------*/
 public:

 using Item = T;

 MPSCInbox( void ) = default;
 MPSCInbox( const MPSCInbox & ) = delete;
 MPSCInbox & operator=( const MPSCInbox & ) = delete;

 ~MPSCInbox();

 void push( T v );

 [[nodiscard]] std::vector< T > take( void );

 [[nodiscard]] std::vector< T > waitTake( void ) {
  std::vector< T > v;
  f_wait.await( [ & ] { v = take(); return( ! v.empty() ); } );
  return( v );
  }

 [[nodiscard]] bool empty( void ) const;

/*--------------------------------------------------------------------------*/
 private:

 struct Cell {
  T value;
  Cell * next;
  };

 std::atomic< Cell * > f_top{ nullptr };
 EventCount f_wait;

 };  // end( class MPSCInbox )

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/concurrent/impl/MPSCInbox.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* MPSCInbox.h included */

/*--------------------------------------------------------------------------*/
/*-------------------------- End File MPSCInbox.h --------------------------*/
/*--------------------------------------------------------------------------*/