/*
 *  Copyright 2009 OpenSourceStewardshipFoundation.org
 *  Licensed under GNU General Public License version 2
 *
 * Author: seanhalle@yahoo.com
 *
 */

#ifndef _MeasVMS_H
#define	_MeasVMS_H

#include "VMS/Queue_impl/PrivateQueue.h"
#include "VMS/Hash_impl/PrivateHash.h"
#include "VMS/Histogram/Histogram.h"
#include "VMS/VMS.h"

/*This header defines everything specific to the MeasVMS semantic plug-in
 */
typedef struct _MeasVMSSemReq   MeasVMSSemReq;


/*Semantic layer-specific data sent inside a request from lib called in app
 * to request handler called in MasterLoop
 */
enum MeasVMSReqType
 {
   send_type = 1,
   send_from_to,
   receive_any,    //order and grouping matter -- send before receive
   receive_type,   // and receive_any first of the receives -- Handlers
   receive_from_to,// rely upon this ordering of enum
   transfer_to,
   transfer_out
 };

struct _MeasVMSSemReq
 { enum MeasVMSReqType    reqType;
   VirtProcr           *sendPr;
   VirtProcr           *receivePr;
   int32                msgType;
   void                *msg;
   MeasVMSSemReq         *nextReqInHashEntry;
 }
/* MeasVMSSemReq */;

typedef struct
 {
   PrivQueueStruc **readyVPQs;
   int              numVirtPr;
   int              numTimesCalled;
   int              firstOfTwo;
   int              nextCoreToGetNewPr;

   Histogram      **addrOfSuspHist;
   Histogram      **addrOfMasterHist;
 }
MeasVMSSemEnv;


typedef struct
 {
    Histogram *suspHist;
    Histogram *masterHist;
 }
HistCarrier;


//===========================================================================

//===========================================================================

void
MeasVMS__create_seed_procr_and_do_work( VirtProcrFnPtr fn, void *initData );

//=======================

void
MeasVMS__init();

void
MeasVMS__cleanup_after_shutdown();

//=======================

inline VirtProcr *
MeasVMS__create_procr_with( VirtProcrFnPtr fnPtr, void *initData,
                          VirtProcr *creatingPr );

void
MeasVMS__dissipate_procr( VirtProcr *procrToDissipate );

//=======================
void *
MeasVMS__malloc_size_to( int numBytes, VirtProcr *ownerPr );

void
MeasVMS__transfer_ownership_of_from_to( void *data, VirtProcr *oldOwnerPr,
                                                    VirtProcr *newOwnerPr );
                                                    
void
MeasVMS__add_ownership_by_to( VirtProcr *newOwnerPr, void *data );

void
MeasVMS__remove_ownership_by_from( VirtProcr *loserPr, void *dataLosing );

void
MeasVMS__transfer_ownership_to_outside( void *dataToTransferOwnershipOf );



//=======================
void
MeasVMS__send_of_type_to( VirtProcr *sendPr, void *msg, const int type,
                        VirtProcr *receivePr);

void
MeasVMS__send_from_to( void *msg, VirtProcr *sendPr, VirtProcr *receivePr);

void *
MeasVMS__receive_type_to( const int type, VirtProcr *receivePr );

void *
MeasVMS__receive_from_to( VirtProcr *sendPr, VirtProcr *receivePr );


//=======================

void
MeasVMS__free_semantic_request( MeasVMSSemReq *semReq );


//=========================  Internal use only  =============================
void
MeasVMS__Request_Handler( VirtProcr *requestingPr, void *_semEnv );

VirtProcr *
MeasVMS__schedule_virt_procr( void *_semEnv, int coreNum );


#endif	/* _MeasVMS_H */

