/*static char *SCCSID = "@(#)chkframe.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)chkframe.c 12.2 90/01/25";*/

#include <os2def.h>

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"

#include "frame.h"
#include "lw.h"
#include "fsdata.h"

#include "pr_debug.h"

/*
 ***	ClobberFrame - Clobber global frame before blocking
 *
 *	ClobberFrame cleans up the global frame so that if some other thread
 *	has blocked (without a MAYBLOCK), and unblocks while this thread is
 *	blocked, it will not have this thread's frame to mess with.
 *
 *	ENTRY pfilename - pointer to .c file of caller
 *	      (references (& sets to NULL) global pfrmHeap)
 *
 *	EXIT  none
 *
 */
void far
ClobberFrame (PSZ pfilename)
{
    /* check to assure that this is our frame */
    if (pfrmHeap->usID != GetSlot ()) {
	dprintf ("%s/ClobberFrame: pfrmHeap->usID != GetSlot (),\n", pfilename);
	dprintf ("  pfrmHeap=%p, usID=0x%x\n", pfrmHeap, pfrmHeap->usID);
	Int3 ();
    }

    /* clear out global frame so other thread's will gp fault */
    memset(pfrm,0,sizeof(FRAME));

    /* make global frame pointer (heap/stack) NULL, so other
     * threads will fault.  pfrmHeap had better been saved already.
     */
    pfrmHeap = NULL;
}


/*
 ***	CheckFrame - Check global frame after blocking
 *
 *	CheckFrame checks the global frame so that if some other thread
 *	block (without a MAYBLOCK) while this thread is blocked, if it left
 *	its frame around, we detect it here.
 *
 *	ENTRY pfrmSave - pointer to heap where this thread's frame has been
 *			 saved
 *	      pfilename - pointer to .c file of caller
 *	EXIT  none
 *
 */
void far
CheckFrame (PFRAME pfrmSave,
	    PSZ pfilename)
{
    /* see if anyone left a frame laying around */
    if (pfrmHeap != NULL) {
	dprintf ("%s/CheckFrame: pfrmHeap (%p) != NULL; usID = 0x%x\n",
		 pfilename, pfrmHeap, pfrmHeap->usID);
	Int3 ();
    }

    if (pfrmSave != pfrm) {
	/* make sure we still have our frame */
	if (pfrmSave->usID != GetSlot ()) {
	    dprintf ("%s/CheckFrame: pfrmSave->usID != GetSlot (),\n",
		     pfilename);
	    dprintf ("  pfrmSave=%p, usID=0x%x\n", pfrmSave, pfrmSave->usID);
	    Int3 ();
	}
    } else {
	if (pfrmSave->usID != 0) {
	    dprintf ("%s/CheckFrame: pfrmSave == pfrm && usID != 0\n",
		     pfilename);
	    dprintf ("  pfrmSave=%p, usID=0x%x\n", pfrmSave, pfrmSave->usID);
	    Int3 ();
	}
    }
}
