/* 
 * File:   BlockingQueue.h
 * Author: SeanHalle@yahoo.com
 *
 * Created on November 11, 2009, 12:51 PM
 */

#ifndef  _BLOCKINGQUEUE_H
#define	_BLOCKINGQUEUE_H

#include "VMS_impl/VMS_primitive_data_types.h"
#include "VMS_impl/Services_Offered_by_VMS/Memory_Handling/vmalloc.h"


#define TRUE  1
#define FALSE 0

#define LOCKED   1
#define UNLOCKED 0


//========== pThreads based queue ==========
/* It is the data that is shared so only need one mutex. */
typedef
struct
 { pthread_mutex_t  mutex_t;
   pthread_cond_t   cond_w_t;
   pthread_cond_t   cond_r_t;
   int              count;
   int              readPos;
   int              writePos;
   void*            data[1024];  //an array of pointers
   int w_empty;
   int w_full;
 }
PThdQueueStruc;

PThdQueueStruc*     makePThdQ();
void* readPThdQ( PThdQueueStruc *Q );
void writePThdQ( void *in, PThdQueueStruc *Q );


//========== CAS based queue ==========
typedef
struct
 { volatile int     insertLock;
   volatile int     extractLock;
   volatile void*  *insertPos;
   volatile void*  *extractPos;
   void*   startOfData[1024];  //data is pointers
   void*  *endOfData;          //set when make queue
 }
CASQueueStruc;

CASQueueStruc*  makeCASQ();
void* readCASQ( CASQueueStruc *Q );
void writeCASQ( void *in, CASQueueStruc *Q );


//========= non-atomic instr based queue ===========
typedef
struct
 { void*  *insertPos;
   void*  *extractPos;
   void*   startOfData[1024];  //data is pointers
   void*  *endOfData;          //set when make queue
 }
SRSWQueueStruc;

SRSWQueueStruc* makeSRSWQ();
void  freeSRSWQ( SRSWQueueStruc* Q );
void* readSRSWQ( SRSWQueueStruc *Q );
void  writeSRSWQ( void *in, SRSWQueueStruc *Q );


//========= non-atomic instr S R M W queue ===========
typedef
struct
 { int              lastQReadFrom;
   int              numInternalQs;
   int              internalQsSz;
   SRSWQueueStruc* *internalQs;
 }
SRMWQueueStruc;

SRMWQueueStruc* makeSRMWQ();
int addWriterToSRMWQ( SRMWQueueStruc *Q );
void* readSRMWQ( SRMWQueueStruc *Q );
void writeSRMWQ( void *in, SRMWQueueStruc *Q, int writerID );


#endif	/* _BLOCKINGQUEUE_H */

