bool BCSPImplementation::connect(uint32 timeout)
{
    //wait for timeout or until we have link-establishment
    int32 timeoutTime = ms_clock() + timeout ;
    do
    {
        if (BCSPLinkEstablished(mStack))
            return true ;
        sleep(10) ;
    } while ( (int32)(timeoutTime-ms_clock()) > 0) ; 
    return false ;
}
void BCSPImplementation::wait(uint32 wakeupTime)
{
    //this function just puts the stack thread into idle until some
    //event (or timeout) wakes us up.
    int32 timeout = wakeupTime - ms_clock();
    if (timeout <= 0)
        return;

    // There appears to be a bug in the WriteFile
    // implementation under Win98.  Occasionally, the WriteFile never
    // sets the completion event.  Close down and then reopen the com port 
    // if the writefile hasn't completed after a certain period.
    if (mXMITBusy && (ms_clock() - mLastXmitTime > WRITEFILE_PERIOD)) 
    {
        BTRACE3(WINERR,"Cancelling IO at time %d (%d ms from %d)\n", ms_clock(), WRITEFILE_PERIOD, mLastXmitTime);
        mLastXmitTime = ms_clock() ;
        mXMITBusy = false;
        mRCVBusy = false;
        mFile->reopen();
        return;
    }
    //on with the real code...

    switch ( mFile->wait ( timeout , &mRCVBytesAvailable ) )
    {
    case UARTAbstraction::UART_XMITDONE : //This is the transmit-finished - all we need to do is reset the event
        mXMITBusy = false ; 
        BTRACE0(USER0,"TX DONE\n") ;
        break ; //allow xmit to go ahead 
    case UARTAbstraction::UART_RCVDONE: //This is a receive from the uart 
        mRCVBusy = false ;
        BTRACE0(USER0,"RX DONE\n") ;
        break ;
    case UARTAbstraction::CLIENT_TXREQ:
        BTRACE0(USER0,"TX REQUEST\n") ;
        break ;//write req - no action necessary
    case UARTAbstraction::CLIENT_DIEREQ: //the user has requested that we shutdown
        BTRACE0(USER0,"DIE REQUEST\n") ;
        break ;
    case UARTAbstraction::timedOut:
        BTRACE0(USER0,"WAIT TIMEOUT\n") ;
        break;
    case UARTAbstraction::UART_ERROR:
        onLinkFailUart();
        BTRACE0(USER0,"UART ERROR\n") ;
        break;
    default :
        BTRACE0(USER0,"BAD WAIT VALUE\n") ;
        break ;//error!
    }
}
Exemplo n.º 3
0
void fz_lock_debug_lock(fz_context *ctx, int lock)
{
	int i, idx;

	if (ctx->locks.lock != fz_lock_default)
		return;

	idx = find_context(ctx);
	if (idx < 0)
		return;

	if (fz_locks_debug[idx][lock] != 0)
	{
		fprintf(stderr, "Attempt to take lock %d when held already!\n", lock);
	}
	for (i = lock-1; i >= 0; i--)
	{
		if (fz_locks_debug[idx][i] != 0)
		{
			fprintf(stderr, "Lock ordering violation: Attempt to take lock %d when %d held already!\n", lock, i);
		}
	}
	fz_locks_debug[idx][lock] = 1;
#ifdef FITZ_DEBUG_LOCKING_TIMES
	fz_lock_taken[idx][lock] = ms_clock();
#endif
}
Exemplo n.º 4
0
static void dump_lock_times(void)
{
	int i, j;
	int prog_time = ms_clock() - fz_lock_program_start;

	for (j = 0; j < FZ_LOCK_MAX; j++)
	{
		int total = 0;
		for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
		{
			total += fz_lock_time[i][j];
		}
		printf("Lock %d held for %g seconds (%g%%)\n", j, total / 1000.0f, 100.0f*total/prog_time);
	}
	printf("Total program time %g seconds\n", prog_time / 1000.0f);
}
void BCSPImplementation::runStack()
{
    //here's where we actually run round in a loop calling the scheduler...
    while(!mStackMustDie)
    {
        uint32 timeNow = ms_clock() ;
        uint32 wakeupTime = scheduler(mStack,timeNow) ;
        //now see if we can do anything with the uart...
        //try sending ...
        if(!mXMITBusy) 
        {
            //copy stuff out of stack output buffer 
            mXMITBytesAvailable = numBytesInTransmitBuffer(mStack) ;
            if (mXMITBytesAvailable)
            {
                readFromTransmitBuffer(mStack,mXMITBuffer, mXMITBytesAvailable) ;
                if (mXMITBytesAvailable) 
                {
                    BTRACE0(IO,"-->") ;
                    for (unsigned int c = 0 ; c < mXMITBytesAvailable ; c++)
                        PLAINBTRACE1(IO," %02x",mXMITBuffer[c]) ;
                    PLAINBTRACE0(IO,"\n") ;
                }
            }
        }

        //now startup I/O  - non-zero indicates that the read or write has returned immediately
        if (!startIO() && isStackIdle(mStack))
                wait(wakeupTime) ;

        //now try loading the stack receive buffer
        if (mRCVBytesAvailable &&(mRCVBytesAvailable <= numFreeSlotsInReceiveBuffer(mStack)))
        {
            if (mRCVBytesAvailable) 
            {
                BTRACE0(IO,"<--") ;
                for (unsigned int c = 0 ; c < mRCVBytesAvailable ; c++) PLAINBTRACE1(IO," %02x",mRCVBuffer[c]) ;
                PLAINBTRACE0(IO,"\n") ;
            }
            writeToReceiveBuffer(mStack,mRCVBuffer,(uint8) mRCVBytesAvailable) ;
            mRCVBytesAvailable = 0 ;
        }
    }
    //we'll return if we get the stackmustdie signal
} 
uint8 BCSPImplementation::startIO()
{
    uint8 retval = 0 ;
    int  err ;
    if ((!mXMITBusy) && (mXMITBytesAvailable))
    {
        mXMITBusy = true ;
        mLastXmitTime = ms_clock() ;
        if ( mFile->write(mXMITBuffer,mXMITBytesAvailable,&mXMITbytesWritten) )
        {
            //function succeeded immediately
            mXMITBusy = false ;
            retval =1 ;
        }
        else if ( ( err = mFile->LastError() ) )
        {
            mXMITBusy = false ;
            BTRACE1(WINERR,"WriteFile error %d\n",err) ;
            BTRACE1(WINERR,"bytesavail = %d",mXMITBytesAvailable);
            BTRACE1(WINERR,"byteswritten = %d",mXMITbytesWritten);
        }
    }

    if ((!mRCVBusy)  && (!mRCVBytesAvailable))
    {        
        //Now try to read some chars from the uart.
        uint16 bytesToGet = numFreeSlotsInReceiveBuffer(mStack) ;
        mRCVBusy = true ;
        if ( mFile->read( mRCVBuffer , bytesToGet , &mRCVBytesAvailable ) )
        {
            mRCVBusy = false ;
            retval =1 ;
        }
        else if ( (err = mFile->LastError()) )
        {
            mRCVBusy = false ;
            BTRACE1(WINERR,"ReadFile error %d\n",err) ;
        }
    }

    return retval ; //indicate that we need to wait for some io to complete or not
}
Exemplo n.º 7
0
void fz_lock_debug_unlock(fz_context *ctx, int lock)
{
	int idx;

	if (ctx->locks.lock != fz_lock_default)
		return;

	idx = find_context(ctx);
	if (idx < 0)
		return;

	if (fz_locks_debug[idx][lock] == 0)
	{
		fprintf(stderr, "Attempt to release lock %d when not held!\n", lock);
	}
	fz_locks_debug[idx][lock] = 0;
#ifdef FITZ_DEBUG_LOCKING_TIMES
	fz_lock_time[idx][lock] += ms_clock() - fz_lock_taken[idx][lock];
#endif
}
Exemplo n.º 8
0
static int find_context(fz_context *ctx)
{
	int i;

	for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
	{
		if (fz_lock_debug_contexts[i] == ctx)
			return i;
		if (fz_lock_debug_contexts[i] == NULL)
		{
			int gottit = 0;
			/* We've not locked on this context before, so use
			 * this one for this new context. We might have other
			 * threads trying here too though so, so claim it
			 * atomically. No one has locked on this context
			 * before, so we are safe to take the ALLOC lock. */
			ctx->locks.lock(ctx->locks.user, FZ_LOCK_ALLOC);
			/* If it's still free, then claim it as ours,
			 * otherwise we'll keep hunting. */
			if (fz_lock_debug_contexts[i] == NULL)
			{
				gottit = 1;
				fz_lock_debug_contexts[i] = ctx;
#ifdef FITZ_DEBUG_LOCKING_TIMES
				if (fz_debug_locking_inited == 0)
				{
					fz_debug_locking_inited = 1;
					fz_lock_program_start = ms_clock();
					atexit(dump_lock_times);
				}
#endif
			}
			ctx->locks.unlock(ctx->locks.user, FZ_LOCK_ALLOC);
			if (gottit)
				return i;
		}
	}
	return -1;
}