示例#1
0
unsigned TimerStartKernelTimer (TTimer *pThis, unsigned nDelay, TKernelTimerHandler *pHandler, void *pParam, void *pContext)
{
	assert (pThis != 0);

	EnterCritical ();

	unsigned hTimer;
	for (hTimer = 0; hTimer < KERNEL_TIMERS; hTimer++)
	{
		if (pThis->m_KernelTimer[hTimer].m_pHandler == 0)
		{
			break;
		}
	}

	if (hTimer >= KERNEL_TIMERS)
	{
		LeaveCritical ();

		LoggerWrite (LoggerGet (), "timer", LogPanic, "System limit of kernel timers exceeded");

		return 0;
	}

	assert (pHandler != 0);
	pThis->m_KernelTimer[hTimer].m_pHandler    = pHandler;
	pThis->m_KernelTimer[hTimer].m_nElapsesAt  = pThis->m_nTicks+nDelay;
	pThis->m_KernelTimer[hTimer].m_pParam      = pParam;
	pThis->m_KernelTimer[hTimer].m_pContext    = pContext;

	LeaveCritical ();

	return hTimer+1;
}
示例#2
0
void *PageAlloc(void)
{
	EnterCritical ();

#ifdef MEM_DEBUG
	if (++s_PageBucket.nCount > s_PageBucket.nMaxCount)
	{
		s_PageBucket.nMaxCount = s_PageBucket.nCount;
	}
#endif

	TFreePage *pFreePage;
	if ((pFreePage = s_PageBucket.pFreeList) != 0)
	{
		s_PageBucket.pFreeList = pFreePage->pNext;
		pFreePage->nMagic = 0;
	}
	else
	{
		pFreePage = (TFreePage *) s_pNextPage;
		
		s_pNextPage += PAGE_SIZE;

		if (s_pNextPage > s_pPageLimit)
		{
			LeaveCritical ();

			return 0;		// TODO: system should panic here
		}
	}

	LeaveCritical ();
	
	return pFreePage;
}
示例#3
0
void NativeSoundMix::DoPlay()
{
	while ( true ) {
		// Wait for a buffer done event
		WaitForSingleObject(playEvent, 10*1000);
		EnterCritical();
		if ( !hWaveOut || !playEvent ) {
			LeaveCritical();
			return;
		}

		// If a buffer is done, fill it...
		int nSent = 0;
		for ( int i = 0; i < nBuffers; i++ ) {
			WAVEHDR* hdr = waveHDR+i;
			if ( (hdr->dwFlags & WHDR_DONE) || !(hdr->dwFlags & WHDR_PREPARED) ) {
				if ( buffer[i] )
				{
	 				BuildAndWrite( &waveHdr[i], false );
				}
// 				SendBuffer(i);
				nSent++;
			}
		}

		if ( nSent > nBuffers-2 ) 
			EnlargeBuffers();

		if ( nSilent > nBuffers ) 
			CloseDevice();
		LeaveCritical();
	}
}
示例#4
0
void *malloc (unsigned long ulSize)
{
  printf("++++++++++++++++++++++ WE SHOULD NOT BE HERE (alloc.c) ++++++++++++++++++++++\r\n");
	assert (s_pNextBlock != 0);

	EnterCritical ();

	TBlockBucket *pBucket;
	for (pBucket = s_BlockBucket; pBucket->nSize > 0; pBucket++)
	{
		if (ulSize <= pBucket->nSize)
		{
			ulSize = pBucket->nSize;

#ifdef MEM_DEBUG
			if (++pBucket->nCount > pBucket->nMaxCount)
			{
				pBucket->nMaxCount = pBucket->nCount;
			}
#endif

			break;
		}
	}

	TBlockHeader *pBlockHeader;
	if (   pBucket->nSize > 0
	    && (pBlockHeader = pBucket->pFreeList) != 0)
	{
		assert (pBlockHeader->nMagic == BLOCK_MAGIC);
		pBucket->pFreeList = pBlockHeader->pNext;
	}
	else
	{
		pBlockHeader = (TBlockHeader *) s_pNextBlock;

		s_pNextBlock += (sizeof (TBlockHeader) + ulSize + BLOCK_ALIGN-1) & ~ALIGN_MASK;
		if (s_pNextBlock > s_pBlockLimit)
		{
			LeaveCritical ();

			return 0;		// TODO: system should panic here
		}

		pBlockHeader->nMagic = BLOCK_MAGIC;
		pBlockHeader->nSize = (unsigned) ulSize;
	}

	LeaveCritical ();

	pBlockHeader->pNext = 0;

	void *pResult = pBlockHeader->Data;
	assert (((unsigned long) pResult & ALIGN_MASK) == 0);

	return pResult;
}
示例#5
0
void* MemoryAllocate(unsigned long ulSize)
{
	EnterCritical ();

	TBlockBucket *pBucket;
	for (pBucket = s_BlockBucket; pBucket->nSize > 0; pBucket++)
	{
		if (ulSize <= pBucket->nSize)
		{
			ulSize = pBucket->nSize;

#ifdef MEM_DEBUG
			if (++pBucket->nCount > pBucket->nMaxCount)
			{
				pBucket->nMaxCount = pBucket->nCount;
			}
#endif

			break;
		}
	}

	TBlockHeader *pBlockHeader;
	if (   pBucket->nSize > 0
	    && (pBlockHeader = pBucket->pFreeList) != 0)
	{
		pBucket->pFreeList = pBlockHeader->pNext;
	}
	else
	{
		pBlockHeader = (TBlockHeader *) s_pNextBlock;

		s_pNextBlock += (sizeof (TBlockHeader) + ulSize + BLOCK_ALIGN-1) & ~ALIGN_MASK;
		if (s_pNextBlock > s_pBlockLimit)
		{
			LeaveCritical ();

			return 0;		// TODO: system should panic here
		}
	
		pBlockHeader->nMagic = BLOCK_MAGIC;
		pBlockHeader->nSize = (unsigned) ulSize;
	}
	
	LeaveCritical ();

	pBlockHeader->pNext = 0;

	void *pResult = pBlockHeader->Data;

	return pResult;
}
示例#6
0
int CSoftSerialDevice::BufStat (void) const
{
	EnterCritical ();

	if (m_nInPtr != m_nOutPtr)
	{
		LeaveCritical ();

		return 1;
	}

	LeaveCritical ();

	return 0;
}
示例#7
0
TString *TimerGetTimeString (TTimer *pThis)
{
	assert (pThis != 0);

	EnterCritical ();

	unsigned nTime = pThis->m_nTime;
	unsigned nTicks = pThis->m_nTicks;

	LeaveCritical ();

	if (nTicks == 0)
	{
		return 0;
	}

	unsigned nSecond = nTime % 60;
	nTime /= 60;
	unsigned nMinute = nTime % 60;
	nTime /= 60;
	unsigned nHours = nTime;

	nTicks %= HZ;
#if (HZ != 100)
	nTicks = nTicks * 100 / HZ;
#endif

	TString *pString = malloc (sizeof (TString));
	assert (pString != 0);
	String (pString);

	StringFormat (pString, "%02u:%02u:%02u.%02lu", nHours, nMinute, nSecond, nTicks);

	return pString;
}
示例#8
0
void free (void *pBlock)
{
	assert (pBlock != 0);
	TBlockHeader *pBlockHeader = (TBlockHeader *) ((unsigned long) pBlock - sizeof (TBlockHeader));
	assert (pBlockHeader->nMagic == BLOCK_MAGIC);

	for (TBlockBucket *pBucket = s_BlockBucket; pBucket->nSize > 0; pBucket++)
	{
		if (pBlockHeader->nSize == pBucket->nSize)
		{
			EnterCritical ();

			pBlockHeader->pNext = pBucket->pFreeList;
			pBucket->pFreeList = pBlockHeader;

#ifdef MEM_DEBUG
			pBucket->nCount--;
#endif

			LeaveCritical ();

			break;
		}
	}
}
示例#9
0
char CSoftSerialDevice::OutBuf (void)
{
	EnterCritical ();

	if (m_nInPtr == m_nOutPtr)
	{
		LeaveCritical ();

		return 0;
	}

	char chChar = m_Buffer[m_nOutPtr];

	m_nOutPtr = (m_nOutPtr+1) & BUFMASK;

	LeaveCritical ();

	return chChar;
}
示例#10
0
void CDWHCIDevice::DisableChannelInterrupt (unsigned nChannel)
{
	CDWHCIRegister AllChanInterruptMask (DWHCI_HOST_ALLCHAN_INT_MASK);

	EnterCritical ();

	AllChanInterruptMask.Read ();
	AllChanInterruptMask.And (~(1 << nChannel));
	AllChanInterruptMask.Write ();

	LeaveCritical ();
}
示例#11
0
void CDWHCIDevice::FreeChannel (unsigned nChannel)
{
	assert (nChannel < m_nChannels);
	unsigned nChannelMask = 1 << nChannel; 
	
	EnterCritical ();
	
	assert (m_nChannelAllocated & nChannelMask);
	m_nChannelAllocated &= ~nChannelMask;
	
	LeaveCritical ();
}
示例#12
0
void NativeSoundMix::CloseNativeDevice()
{
	if ( !isOpen ) return;

	SetEvent(playEvent);	// signal the thread to quit

	EnterCritical(); // do this after we kill the timer proc to prevent a deadlock

	if ( !isOpen ) {
		LeaveCritical();
		return;	// It is possible for timeKillEvent to call CloseDevice
	}
	isOpen = false;
	
	HWAVEOUT hW = hWaveOut;
	hWaveOut = 0;	// flag that we are shutting down so PollSound will bail

	int res = waveOutReset(hW);
	FLASHASSERT(res == MMSYSERR_NOERROR);

	for ( int i = 0; i < nBuffers; i++ ) {
		int limit = 50;
		while ( limit-- ) {
			int res = waveOutUnprepareHeader(hW, waveHDR+i, sizeof(WAVEHDR));
			if ( res != WAVERR_STILLPLAYING ) {
				FLASHASSERT(res == MMSYSERR_NOERROR);
				break;
			}
			Sleep(1);
		}
	}

	res = waveOutClose(hW);
	FLASHASSERT(res == MMSYSERR_NOERROR);

	CloseHandle(playThread);
	playThread = 0;
	LeaveCritical();
}
示例#13
0
void CSoftSerialDevice::InBuf (char chChar)
{
	EnterCritical ();

	if (((m_nInPtr+1) & BUFMASK) != m_nOutPtr)
	{
		m_Buffer[m_nInPtr] = chChar;

		m_nInPtr = (m_nInPtr+1) & BUFMASK;
	}

	LeaveCritical ();
}
示例#14
0
unsigned CDWHCIDevice::AllocateChannel (void)
{
	EnterCritical ();

	unsigned nChannelMask = 1;
	for (unsigned nChannel = 0; nChannel < m_nChannels; nChannel++)
	{
		if (!(m_nChannelAllocated & nChannelMask))
		{
			m_nChannelAllocated |= nChannelMask;

			LeaveCritical ();
			
			return nChannel;
		}
		
		nChannelMask <<= 1;
	}
	
	LeaveCritical ();
	
	return DWHCI_MAX_CHANNELS;
}
示例#15
0
void PageFree(void *pPage)
{
	TFreePage *pFreePage = (TFreePage *) pPage;
	
	EnterCritical ();

	pFreePage->nMagic = FREEPAGE_MAGIC;

	pFreePage->pNext = s_PageBucket.pFreeList;
	s_PageBucket.pFreeList = pFreePage;

#ifdef MEM_DEBUG
	s_PageBucket.nCount--;
#endif

	LeaveCritical ();
}
示例#16
0
void MemoryFree(void *pBlock)
{
	TBlockHeader *pBlockHeader = (TBlockHeader *) ((unsigned long) pBlock - sizeof (TBlockHeader));

	for (TBlockBucket *pBucket = s_BlockBucket; pBucket->nSize > 0; pBucket++)
	{
		if (pBlockHeader->nSize == pBucket->nSize)
		{
			EnterCritical ();

			pBlockHeader->pNext = pBucket->pFreeList;
			pBucket->pFreeList = pBlockHeader;

#ifdef MEM_DEBUG
			pBucket->nCount--;
#endif

			LeaveCritical ();

			break;
		}
	}
}
示例#17
0
void TimerPollKernelTimers (TTimer *pThis)
{
	assert (pThis != 0);

	EnterCritical ();

	for (unsigned hTimer = 0; hTimer < KERNEL_TIMERS; hTimer++)
	{
		volatile TKernelTimer *pTimer = &pThis->m_KernelTimer[hTimer];

		TKernelTimerHandler *pHandler = pTimer->m_pHandler;
		if (pHandler != 0)
		{
			if ((int) (pTimer->m_nElapsesAt - pThis->m_nTicks) <= 0)
			{
				pTimer->m_pHandler = 0;

				(*pHandler) (hTimer+1, pTimer->m_pParam, pTimer->m_pContext);
			}
		}
	}

	LeaveCritical ();
}