Exemplo n.º 1
0
/****************************************************************************
 * Function: ListAddTail
 *
 *  Description:
 *      Adds a node to the tail of the list.
 *      Node gets added immediately before list.tail.
 *  Parameters:
 *      LinkedList *list  - must be valid, non null, pointer to a linked list.
 *      void * item - item to be added
 *  Returns:
 *      The pointer to the ListNode on success, NULL on failure.
 *  Precondition:
 *      The list has been initialized.
 *****************************************************************************/
ListNode *
ListAddTail( LinkedList * list,
             void *item )
{
    assert( list != NULL );

    if( list == NULL )
        return NULL;

    return ListAddBefore( list, item, &list->tail );
}
Exemplo n.º 2
0
Arquivo: list.c Projeto: blakawk/Aio4c
void ListAddFirst(List* list, Node* newNode) {
    if (newNode == NULL || newNode->data == NULL) {
        abort();
    }

    if (list->first == NULL) {
        list->first = newNode;
        list->last = newNode;
        newNode->prev = NULL;
        newNode->next = NULL;
    } else {
        ListAddBefore(list, list->first, newNode);
    }

    if (list->first != newNode || newNode->prev != NULL) {
        abort();
    }
}
Exemplo n.º 3
0
//Adds a timer queue item to a given timer queue
static void AddTimerToQueue(TimerQueue * newItem, ListHead * headPtr)
{
	TimerTime time = newItem->endTime;

	ListHeadInit(&newItem->list);

	//Find place to insert into queue
	TimerQueue * currPos;

	ListForEachEntry(currPos, headPtr, list)
	{
		//Is this time more than ours?
		if(time > currPos->endTime)
		{
			//Add before current pos
			ListAddBefore(&newItem->list, &currPos->list);
			return;
		}
	}

	//Add list to end of queue
	ListHeadAddLast(&newItem->list, headPtr);
}
Exemplo n.º 4
0
/************************************************************************
 * Function: TimerThreadSchedule
 * 
 *  Description:
 *     Schedules an event to run at a specified time.
 *
 *  Parameters:
 *             timer - valid timer thread pointer.
 *             time_t - time of event.
 *                      either in absolute seconds,
 *                      or relative seconds in the future.
 *             timeoutType - either ABS_SEC, or REL_SEC.
 *                           if REL_SEC, then the event
 *                           will be scheduled at the
 *                           current time + REL_SEC.
 *             
 *             func - function to schedule
 *             arg - argument to function
 *             priority - priority of job.
 *             id - id of timer event. (out)
 *  Return:
 *            0 on success, nonzero on failure
 *			  EOUTOFMEM if not enough memory to schedule job
 ************************************************************************/
int
TimerThreadSchedule( TimerThread * timer,
                     time_t timeout,
                     TimeoutType type,
                     ThreadPoolJob * job,
                     Duration duration,
                     int *id )
{

    int rc = EOUTOFMEM;
    int found = 0;
    int tempId = 0;

    ListNode *tempNode = NULL;
    TimerEvent *temp = NULL;
    TimerEvent *newEvent = NULL;

    assert( timer != NULL );
    assert( job != NULL );

    if( ( timer == NULL ) || ( job == NULL ) ) {
        return EINVAL;
    }

    CalculateEventTime( &timeout, type );
    ithread_mutex_lock( &timer->mutex );

    if( id == NULL )
        id = &tempId;

    ( *id ) = INVALID_EVENT_ID;

    newEvent = CreateTimerEvent( timer, job, duration, timeout,
                                 timer->lastEventId );

    if( newEvent == NULL ) {
        ithread_mutex_unlock( &timer->mutex );
        return rc;
    }

    tempNode = ListHead( &timer->eventQ );
    //add job to Q
    //Q is ordered by eventTime
    //with the head of the Q being the next event

    while( tempNode != NULL ) {
        temp = ( TimerEvent * ) tempNode->item;
        if( temp->eventTime >= timeout )
        {

            if( ListAddBefore( &timer->eventQ, newEvent, tempNode ) !=
                NULL )
                rc = 0;
            found = 1;
            break;

        }
        tempNode = ListNext( &timer->eventQ, tempNode );
    }

    //add to the end of Q
    if( !found ) {

        if( ListAddTail( &timer->eventQ, newEvent ) != NULL )
            rc = 0;

    }
    //signal change in Q
    if( rc == 0 ) {

        ithread_cond_signal( &timer->condition );
    } else {
        FreeTimerEvent( timer, newEvent );
    }
    ( *id ) = timer->lastEventId++;
    ithread_mutex_unlock( &timer->mutex );

    return rc;
}