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

#ifndef _VMSHW_H
#define	_VMSHW_H

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

/*This header defines everything specific to the VMSHW semantic plug-in
 */
typedef struct _VMSHWSemReq   VMSHWSemReq;


/*Semantic layer-specific data sent inside a request from lib called in app
 * to request handler called in MasterLoop
 */
enum VMSHWReqType
 {
   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 _VMSHWSemReq
 { enum VMSHWReqType    reqType;
   VirtProcr           *sendPr;
   VirtProcr           *receivePr;
   int32                msgType;
   void                *msg;
   VMSHWSemReq         *nextReqInHashEntry;
 }
/* VMSHWSemReq */;

typedef struct
 {
   PrivQueueStruc *readyVirtProcrQ;
   HashTable      *commHashTbl;
   int             numVirtPr;
 }
VMSHWSemEnv;


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

void
VMSHW__create_seed_procr_and_do_work( VirtProcrFnPtr fn, void *initData );

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

void
VMSHW__init();

void
VMSHW__shutdown();

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

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

void
VMSHW__dissipate_procr( VirtProcr *procrToDissipate );

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

void
VMSHW__transfer_ownership_of_from_to( void *data, VirtProcr *oldOwnerPr,
                                                    VirtProcr *newOwnerPr );
                                                    
void
VMSHW__add_ownership_by_to( VirtProcr *newOwnerPr, void *data );

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

void
VMSHW__transfer_ownership_to_outside( void *dataToTransferOwnershipOf );



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

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

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

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


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

void
VMSHW__free_semantic_request( VMSHWSemReq *semReq );


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

VirtProcr *
VMSHW__schedule_virt_procr( void *_semEnv );


#endif	/* _VMSHW_H */

