예제 #1
0
/**
    \sa timeElement, timeElement.clockAlarm
*/
inline void Timer::NextTick ()
{
	_presentTime = (_presentTime + 1) & 0x7fff;  // Add one tick to clock.

	if (0 == getCount ()) return;		// No timers.

	// Check for expired timers.
	while (_presentTime == GET_TIMEOUT (0))
	{
		// Update the timed out timers... one timer each time through while loop.
		static_cast<p_timeElement>(_timeOutList[0])->updateTimeOut ();

		// Execute the CallBack function.
		static_cast<p_timeElement>(_timeOutList[0])->clockAlarm ();

		// Sort the list.
		for (uint8_t i=1; i < _timeOutList.GetCount (); i++)
			if (normalizeTimeOut (GET_TIMEOUT (i-1)) > normalizeTimeOut (GET_TIMEOUT (i)))
				_timeOutList.Swap (i-1, i);
			else
				break;
	}
}
예제 #2
0
/**
	\param pArg is a pointer to the timeElement that needs inserted into the list.
	\return the smallest index of timeElements having larger timeouts than pArg.
*/
uint8_t Timer::Search (const p_timeElement pArg)
{
	if (0 == _timeOutList.GetCount ())
		return 0;

	uint8_t i, __sreg = SREG;	// Prevent _presentTime changing.
	noInterrupts ();

	for (i=0; i<_timeOutList.GetCount (); i++)
		if (normalizeTimeOut (pArg->getTimeOut ()) < normalizeTimeOut (GET_TIMEOUT (i)))
			break;

	SREG = __sreg;			// Restore the processor's status register and interrupt mask.

	// Return the index of the first List element needing moved.
	return (i);
}
예제 #3
0
파일: threadpool.c 프로젝트: codesun/xetn
PRIVATE void Thread_getTask(Thread thr, LinkList list) {
	TaskQueue tsq = thr->pool->taskq + thr->id;
	size_t ilen = LinkList_length(list);
	size_t nget = 0;
	size_t olen = 0;
	
	TaskQueue_lock(tsq);
AGAIN:
	olen = TaskQueue_length(tsq);
	if(olen == 0) {
		struct timespec to;
		/* only wait for 200ms */
		GET_TIMEOUT(&to, 200);
		// TODO: wait only when the ntasks is equal to 0
		int ret = pthread_cond_timedwait(&thr->cond, &tsq->lock, &to);
		if(ret == ETIMEDOUT) {
			TaskQueue_unlock(tsq);
			return;
		} else {
			/* thread is waked by the master */
			/* check the available tasks again */
			goto AGAIN;
		}
	} else if(olen > 2 * ilen) {
		nget = olen;
	} else if(ilen > 2 * olen) {
		nget = 1;
	} else {
		nget = (ilen + olen) / 3;
		if(nget > olen) {
			nget = olen;
		}
	}
	uint32_t i;
	for(i = 0; i < nget; ++i) {
		LinkList_put(list, TaskQueue_get(tsq));
	}

	TaskQueue_unlock(tsq);
}