Merge of work from 1.8.x.

This commit is contained in:
Marcelo Roberto Jimenez 2010-10-04 12:04:38 -03:00
parent b9eeb89250
commit 458a9416c6
10 changed files with 369 additions and 383 deletions

View File

@ -5,6 +5,7 @@
#include "UpnpGlobal.h"
#include "ixml.h"
/*!
@ -20,6 +21,13 @@
*/
#ifdef DEBUG
void IxmlPrintf(
/*! [in] The file name, usually __FILE__. */
const char *DbgFileName,
/*! [in] The line number, usually __LINE__ or a variable that got the
* __LINE__ at the appropriate place. */
int DbgLineNo,
/*! [in] The function name. */
const char *FunctionName,
/*! [in] Printf like format specification. */
const char* FmtStr,
/*! [in] Printf like Variable number of arguments that will go in the debug
@ -27,7 +35,7 @@ void IxmlPrintf(
...)
#if (__GNUC__ >= 3)
/* This enables printf like format checking by the compiler */
__attribute__((format (__printf__, 1, 2)))
__attribute__((format (__printf__, 4, 5)))
#endif
;
#else /* DEBUG */
@ -37,5 +45,23 @@ static UPNP_INLINE void IxmlPrintf(
#endif /* DEBUG */
/*!
* \brief Print the node names and values of a XML tree.
*/
#ifdef DEBUG
void printNodes(
/*! [in] The root of the tree to print. */
IXML_Node *tmpRoot,
/*! [in] The depth to print. */
int depth);
#else
static UPNP_INLINE void printNodes(
IXML_Node *tmpRoot,
int depth)
{
}
#endif
#endif /* IXMLDEBUG_H */

View File

@ -444,17 +444,17 @@ int ixmlDocument_createElementNSEx(
IXML_Element **rtElement)
{
IXML_Element *newElement = NULL;
int errCode = IXML_SUCCESS;
int ret = IXML_SUCCESS;
int line = 0;
if (doc == NULL || namespaceURI == NULL || qualifiedName == NULL) {
line = __LINE__;
errCode = IXML_INVALID_PARAMETER;
ret = IXML_INVALID_PARAMETER;
goto ErrorHandler;
}
errCode = ixmlDocument_createElementEx(doc, qualifiedName, &newElement);
if (errCode != IXML_SUCCESS) {
ret = ixmlDocument_createElementEx(doc, qualifiedName, &newElement);
if (ret != IXML_SUCCESS) {
line = __LINE__;
goto ErrorHandler;
}
@ -464,16 +464,16 @@ int ixmlDocument_createElementNSEx(
line = __LINE__;
ixmlElement_free(newElement);
newElement = NULL;
errCode = IXML_INSUFFICIENT_MEMORY;
ret = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
// set the localName and prefix
errCode = ixmlNode_setNodeName((IXML_Node *)newElement, qualifiedName);
if (errCode != IXML_SUCCESS) {
ret = ixmlNode_setNodeName((IXML_Node *)newElement, qualifiedName);
if (ret != IXML_SUCCESS) {
line = __LINE__;
ixmlElement_free(newElement);
newElement = NULL;
errCode = IXML_INSUFFICIENT_MEMORY;
ret = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
@ -481,12 +481,11 @@ int ixmlDocument_createElementNSEx(
ErrorHandler:
*rtElement = newElement;
if (errCode != IXML_SUCCESS) {
IxmlPrintf("(%s::ixmlDocument_createElementNSEx): Error %d, line %d\n",
__FILE__, errCode, line);
if (ret != IXML_SUCCESS) {
IxmlPrintf(__FILE__, line, "ixmlDocument_createElementNSEx", "Error %d\n", ret);
}
return errCode;
return ret;
}

View File

@ -181,9 +181,9 @@ static void ixmlPrintDomTreeRecursive(
break;
default:
IxmlPrintf("(%s::ixmlPrintDomTreeRecursive) line %d: "
IxmlPrintf(__FILE__, __LINE__, "ixmlPrintDomTreeRecursive",
"Warning, unknown node type %d\n",
__FILE__, __LINE__, ixmlNode_getNodeType(nodeptr));
ixmlNode_getNodeType(nodeptr));
break;
}
}
@ -253,9 +253,9 @@ static void ixmlPrintDomTree(
break;
default:
IxmlPrintf("(%s::ixmlPrintDomTree) line %d: "
IxmlPrintf(__FILE__, __LINE__, "ixmlPrintDomTree",
"Warning, unknown node type %d\n",
__FILE__, __LINE__, ixmlNode_getNodeType(nodeptr));
ixmlNode_getNodeType(nodeptr));
break;
}
}
@ -324,9 +324,9 @@ static void ixmlDomTreetoString(
break;
default:
IxmlPrintf("(%s::ixmlDomTreetoString) line %d: "
IxmlPrintf(__FILE__, __LINE__, "ixmlPrintDomTreeRecursive",
"Warning, unknown node type %d\n",
__FILE__, __LINE__, ixmlNode_getNodeType(nodeptr));
ixmlNode_getNodeType(nodeptr));
break;
}
}

View File

@ -17,15 +17,54 @@
#ifdef DEBUG
void IxmlPrintf(
const char *DbgFileName,
int DbgLineNo,
const char *FunctionName,
const char *FmtStr,
...)
{
va_list ArgList;
va_start(ArgList, FmtStr);
vfprintf(stdout, FmtStr, ArgList);
fflush(stdout);
va_end(ArgList);
FILE *fp = stdout;
fprintf(fp, "(%s::%s), line %d", DbgFileName, FunctionName, DbgLineNo);
if (FmtStr) {
fprintf(fp, ": ");
va_start(ArgList, FmtStr);
vfprintf(fp, FmtStr, ArgList);
fflush(fp);
va_end(ArgList);
} else {
fprintf(fp, "\n");
}
}
void printNodes(IXML_Node *tmpRoot, int depth)
{
int i;
IXML_NodeList *NodeList1;
IXML_Node *ChildNode1;
unsigned short NodeType;
const DOMString NodeValue;
const DOMString NodeName;
NodeList1 = ixmlNode_getChildNodes(tmpRoot);
for (i = 0; i < 100; ++i) {
ChildNode1 = ixmlNodeList_item(NodeList1, i);
if (ChildNode1 == NULL) {
break;
}
printNodes(ChildNode1, depth+1);
NodeType = ixmlNode_getNodeType(ChildNode1);
NodeValue = ixmlNode_getNodeValue(ChildNode1);
NodeName = ixmlNode_getNodeName(ChildNode1);
IxmlPrintf(__FILE__, __LINE__, "printNodes",
"DEPTH-%2d-IXML_Node Type %d, "
"IXML_Node Name: %s, IXML_Node Value: %s\n",
depth, NodeType, NodeName, NodeValue);
}
}
#endif

View File

@ -542,8 +542,7 @@ static int Parser_UTF8ToInt(
*len = 0;
ret = -1;
}
IxmlPrintf("(%s::Parser_UTF8ToInt): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_UTF8ToInt", "Error %d\n", ret);
return ret;
}
}
@ -1305,8 +1304,7 @@ fail_entity:
ExitFunction:
if (ret == -1 || (g_error_char && ret == g_error_char)) {
IxmlPrintf("(%s::Parser_getChar): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_getChar", "Error %d\n", ret);
}
return ret;
@ -1373,8 +1371,7 @@ static int Parser_copyToken(
ExitFunction:
if (ret != IXML_SUCCESS) {
IxmlPrintf("(%s::Parser_copyToken): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_copyToken", "Error %d\n", ret);
}
return ret;
@ -1921,8 +1918,7 @@ static int Parser_xmlNamespace(
ExitFunction:
if (ret != IXML_SUCCESS && ret != IXML_FILE_DONE) {
IxmlPrintf("(%s::Parser_xmlNamespace): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_xmlNamespace", "Error %d\n", ret);
}
return ret;
@ -2251,8 +2247,7 @@ static int Parser_processContent(
ExitFunction:
if (ret != IXML_SUCCESS) {
IxmlPrintf("(%s::Parser_processContent): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_processContent", "Error %d\n", ret);
}
return ret;
@ -2324,8 +2319,7 @@ static int Parser_processETag(
ExitFunction:
if (ret != IXML_SUCCESS) {
IxmlPrintf("(%s::Parser_processETag): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_processETag", "Error %d\n", ret);
}
return ret;
@ -2580,8 +2574,7 @@ static int Parser_processAttribute(
ExitFunction:
if (ret != IXML_SUCCESS && ret != IXML_FILE_DONE) {
IxmlPrintf("(%s::Parser_processAttribute): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_processAttribute", "Error %d\n", ret);
}
return ret;
@ -2684,8 +2677,7 @@ static int Parser_getNextNode(
ExitFunction:
if (ret != IXML_SUCCESS && ret != IXML_FILE_DONE) {
IxmlPrintf("(%s::Parser_getNextNode): Error %d, line %d\n",
__FILE__, ret, line);
IxmlPrintf(__FILE__, line, "Parser_getNextNode", "Error %d\n", ret);
}
return ret;

View File

@ -29,14 +29,24 @@
*
******************************************************************************/
#ifndef FREE_LIST_H
#define FREE_LIST_H
/*!
* \file
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "ithread.h"
#include <errno.h>
/****************************************************************************

View File

@ -29,21 +29,32 @@
*
******************************************************************************/
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
/*!
* \file
*/
#include "FreeList.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EOUTOFMEM (-7 & 1<<29)
#define FREELISTSIZE 100
#define LIST_SUCCESS 1
#define LIST_FAIL 0
/****************************************************************************
* Name: free_routine
*
@ -52,6 +63,7 @@ extern "C" {
*****************************************************************************/
typedef void (*free_function)(void *arg);
/****************************************************************************
* Name: cmp_routine
*
@ -61,6 +73,7 @@ typedef void (*free_function)(void *arg);
*****************************************************************************/
typedef int (*cmp_routine)(void *itemA,void *itemB);
/****************************************************************************
* Name: ListNode
*
@ -75,6 +88,7 @@ typedef struct LISTNODE
void *item;
} ListNode;
/****************************************************************************
* Name: LinkedList
*
@ -104,6 +118,7 @@ typedef struct LINKEDLIST
cmp_routine cmp_func; /* compare function to use */
} LinkedList;
/****************************************************************************
* Function: ListInit
*
@ -119,6 +134,7 @@ typedef struct LINKEDLIST
*****************************************************************************/
int ListInit(LinkedList *list,cmp_routine cmp_func, free_function free_func);
/****************************************************************************
* Function: ListAddHead
*
@ -135,6 +151,7 @@ int ListInit(LinkedList *list,cmp_routine cmp_func, free_function free_func);
*****************************************************************************/
ListNode *ListAddHead(LinkedList *list, void *item);
/****************************************************************************
* Function: ListAddTail
*
@ -151,6 +168,7 @@ ListNode *ListAddHead(LinkedList *list, void *item);
*****************************************************************************/
ListNode *ListAddTail(LinkedList *list, void *item);
/****************************************************************************
* Function: ListAddAfter
*
@ -205,6 +223,7 @@ ListNode *ListAddBefore(LinkedList *list,void *item, ListNode *anode);
*****************************************************************************/
void *ListDelNode(LinkedList *list,ListNode *dnode, int freeItem);
/****************************************************************************
* Function: ListDestroy
*
@ -240,6 +259,7 @@ int ListDestroy(LinkedList *list, int freeItem);
*****************************************************************************/
ListNode* ListHead(LinkedList *list);
/****************************************************************************
* Function: ListTail
*
@ -256,6 +276,7 @@ ListNode* ListHead(LinkedList *list);
*****************************************************************************/
ListNode* ListTail(LinkedList *list);
/****************************************************************************
* Function: ListNext
*
@ -272,6 +293,7 @@ ListNode* ListTail(LinkedList *list);
*****************************************************************************/
ListNode* ListNext(LinkedList *list, ListNode * node);
/****************************************************************************
* Function: ListPrev
*
@ -288,6 +310,7 @@ ListNode* ListNext(LinkedList *list, ListNode * node);
*****************************************************************************/
ListNode* ListPrev(LinkedList *list, ListNode * node);
/****************************************************************************
* Function: ListFind
*
@ -307,6 +330,7 @@ ListNode* ListPrev(LinkedList *list, ListNode * node);
*****************************************************************************/
ListNode* ListFind(LinkedList *list, ListNode *start, void * item);
/****************************************************************************
* Function: ListSize
*

View File

@ -29,160 +29,139 @@
*
******************************************************************************/
#ifndef TIMERTHREAD_H
#define TIMERTHREAD_H
/*!
* \file
*/
#include "FreeList.h"
#include "ithread.h"
#include "LinkedList.h"
#include "FreeList.h"
#include "ThreadPool.h"
#ifdef __cplusplus
extern "C" {
#endif
#define INVALID_EVENT_ID (-10 & 1<<29)
/* Timeout Types */
/* absolute means in seconds from Jan 1, 1970 */
/* relative means in seconds from current time */
typedef enum timeoutType {ABS_SEC,REL_SEC} TimeoutType;
/****************************************************************************
* Name: TimerThread
*
* Description:
* A timer thread similar to the one in the Upnp SDK that allows
* the scheduling of a job to run at a specified time in the future
* Because the timer thread uses the thread pool there is no
* gurantee of timing, only approximate timing.
* Uses ThreadPool, Mutex, Condition, Thread
*
*
*****************************************************************************/
/*!
* A timer thread similar to the one in the Upnp SDK that allows
* the scheduling of a job to run at a specified time in the future.
*
* Because the timer thread uses the thread pool there is no
* gurantee of timing, only approximate timing.
*
* Uses ThreadPool, Mutex, Condition, Thread.
*/
typedef struct TIMERTHREAD
{
ithread_mutex_t mutex;
ithread_cond_t condition;
int lastEventId;
LinkedList eventQ;
int shutdown;
FreeList freeEvents;
ThreadPool *tp;
ithread_mutex_t mutex;
ithread_cond_t condition;
int lastEventId;
LinkedList eventQ;
int shutdown;
FreeList freeEvents;
ThreadPool *tp;
} TimerThread;
/****************************************************************************
* Name: TimerEvent
*
* Description:
*
* Struct to contain information for a timer event.
* Internal to the TimerThread
*
*****************************************************************************/
/*!
* Struct to contain information for a timer event.
*
* Internal to the TimerThread.
*/
typedef struct TIMEREVENT
{
ThreadPoolJob job;
time_t eventTime; /* absolute time for event in seconds since Jan 1, 1970 */
Duration persistent; /* long term or short term job */
int id;
ThreadPoolJob job;
/*! [in] Absolute time for event in seconds since Jan 1, 1970. */
time_t eventTime;
/*! [in] Long term or short term job. */
Duration persistent;
int id;
} TimerEvent;
/************************************************************************
* Function: TimerThreadInit
*
* Description:
* Initializes and starts timer thread.
/*!
* \brief Initializes and starts timer thread.
*
* Parameters:
* timer - valid timer thread pointer.
* tp - valid thread pool to use. Must be
* started. Must be valid for lifetime
* of timer. Timer must be shutdown
* BEFORE thread pool.
* Return:
* 0 on success, nonzero on failure
* Returns error from ThreadPoolAddPersistent on failure.
*
************************************************************************/
int TimerThreadInit(TimerThread *timer,
ThreadPool *tp);
* \return 0 on success, nonzero on failure. Returns error from
* ThreadPoolAddPersistent on failure.
*/
int TimerThreadInit(
/*! [in] Valid timer thread pointer. */
TimerThread *timer,
/*! [in] Valid thread pool to use. Must be started. Must be valid for
* lifetime of timer. Timer must be shutdown BEFORE thread pool. */
ThreadPool *tp);
/************************************************************************
* Function: TimerThreadSchedule
*
* Description:
* Schedules an event to run at a specified time.
/*!
* \brief 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.
* job-> valid Thread pool job with following fields
* func - function to schedule
* arg - argument to function
* priority - priority of job.
*
* id - id of timer event. (out, can be null)
* Return:
* 0 on success, nonzero on failure
* EOUTOFMEM if not enough memory to schedule job.
*
************************************************************************/
int TimerThreadSchedule(TimerThread* timer,
time_t time,
TimeoutType type,
ThreadPoolJob *job,
Duration duration,
int *id);
* \return 0 on success, nonzero on failure, EOUTOFMEM if not enough memory
* to schedule job.
*/
int TimerThreadSchedule(
/*! [in] Valid timer thread pointer. */
TimerThread* timer,
/*! [in] time of event. Either in absolute seconds, or relative
* seconds in the future. */
time_t time,
/*! [in] either ABS_SEC, or REL_SEC. If REL_SEC, then the event
* will be scheduled at the current time + REL_SEC. */
TimeoutType type,
/*! [in] Valid Thread pool job with following fields. */
ThreadPoolJob *job,
/*! [in] . */
Duration duration,
/*! [in] Id of timer event. (out, can be null). */
int *id);
/************************************************************************
* Function: TimerThreadRemove
*
* Description:
* Removes an event from the timer Q.
* Events can only be removed
* before they have been placed in the
* thread pool.
*
* Parameters:
* timer - valid timer thread pointer.
* id - id of event to remove.
* ThreadPoolJob *out - space for thread pool job.
* Return:
* 0 on success,
* INVALID_EVENT_ID on failure
*
************************************************************************/
int TimerThreadRemove(TimerThread *timer,
int id,
ThreadPoolJob *out);
/************************************************************************
* Function: TimerThreadShutdown
*
* Description:
* Shutdown the timer thread
* Events scheduled in the future will NOT be run.
* Timer thread should be shutdown BEFORE it's associated
* thread pool.
* Returns:
* returns 0 if succesfull,
* nonzero otherwise.
* Always returns 0.
***********************************************************************/
int TimerThreadShutdown(TimerThread *timer);
/*!
* \brief Removes an event from the timer Q.
*
* Events can only be removed before they have been placed in the thread pool.
*
* \return 0 on success, INVALID_EVENT_ID on failure.
*/
int TimerThreadRemove(
/*! [in] Valid timer thread pointer. */
TimerThread *timer,
/*! [in] Id of event to remove. */
int id,
/*! [in] Space for thread pool job. */
ThreadPoolJob *out);
/*!
* \brief Shutdown the timer thread.
*
* Events scheduled in the future will NOT be run.
*
* Timer thread should be shutdown BEFORE it's associated thread pool.
*
* \return 0 if succesfull, nonzero otherwise. Always returns 0.
*/
int TimerThreadShutdown(
/*! [in] Valid timer thread pointer. */
TimerThread *timer);
#ifdef __cplusplus
}

View File

@ -1,45 +1,55 @@
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2003 Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither name of Intel Corporation nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////
/**************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************/
#include "LinkedList.h"
#ifndef WIN32
#ifdef WIN32
/* Do not #include <sys/param.h> */
#else
#include <sys/param.h>
#endif
#if (defined(BSD) && BSD >= 199306) || defined(__OSX__) || defined(__APPLE__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <assert.h>
static int
freeListNode( ListNode * node,
LinkedList * list )

View File

@ -1,68 +1,69 @@
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2003 Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither name of Intel Corporation nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/*!
* \file
*/
#include "TimerThread.h"
#include <assert.h>
/****************************************************************************
* Function: FreeTimerEvent
*
* Description:
* Deallocates a dynamically allocated TimerEvent.
* Parameters:
* TimerEvent *event - must be allocated with CreateTimerEvent
*****************************************************************************/
static void
FreeTimerEvent( TimerThread * timer,
TimerEvent * event )
/*!
* \brief Deallocates a dynamically allocated TimerEvent.
*/
static void FreeTimerEvent(
/*! [in] Valid timer thread pointer. */
TimerThread *timer,
/*! [in] Must be allocated with CreateTimerEvent*/
TimerEvent *event)
{
assert(timer != NULL);
assert( timer != NULL );
FreeListFree( &timer->freeEvents, event );
FreeListFree(&timer->freeEvents, event);
}
/****************************************************************************
* Function: TimerThreadWorker
/*!
* \brief Implements timer thread.
*
* Description:
* Implements timer thread.
* Waits for next event to occur and schedules
* associated job into threadpool.
* Internal Only.
* Parameters:
* void * arg -> is cast to TimerThread *
*****************************************************************************/
static void *
TimerThreadWorker( void *arg )
* Waits for next event to occur and schedules associated job into threadpool.
*/
static void *TimerThreadWorker(
/*! [in] arg is cast to (TimerThread *). */
void *arg)
{
TimerThread *timer = ( TimerThread * ) arg;
ListNode *head = NULL;
@ -81,19 +82,14 @@ TimerThreadWorker( void *arg )
while( 1 )
{
//mutex should always be locked at top of loop
//Check for shutdown
if( timer->shutdown )
{
timer->shutdown = 0;
ithread_cond_signal( &timer->condition );
ithread_mutex_unlock( &timer->mutex );
return NULL;
}
nextEvent = NULL;
@ -102,7 +98,6 @@ TimerThreadWorker( void *arg )
if( timer->eventQ.size > 0 )
{
head = ListHead( &timer->eventQ );
nextEvent = ( TimerEvent * ) head->item;
nextEventTime = nextEvent->eventTime;
}
@ -110,54 +105,42 @@ TimerThreadWorker( void *arg )
currentTime = time( NULL );
//If time has elapsed, schedule job
if( ( nextEvent != NULL ) && ( currentTime >= nextEventTime ) )
{
if( nextEvent->persistent ) {
ThreadPoolAddPersistent( timer->tp, &nextEvent->job,
&tempId );
} else {
ThreadPoolAdd( timer->tp, &nextEvent->job, &tempId );
}
ListDelNode( &timer->eventQ, head, 0 );
FreeTimerEvent( timer, nextEvent );
continue;
}
if( nextEvent != NULL ) {
timeToWait.tv_nsec = 0;
timeToWait.tv_sec = nextEvent->eventTime;
ithread_cond_timedwait( &timer->condition, &timer->mutex,
&timeToWait );
} else {
ithread_cond_wait( &timer->condition, &timer->mutex );
}
}
}
/****************************************************************************
* Function: CalculateEventTime
/*!
* \brief Calculates the appropriate timeout in absolute seconds
* since Jan 1, 1970.
*
* Description:
* Calculates the appropriate timeout in absolute seconds since
* Jan 1, 1970
* Internal Only.
* Parameters:
* time_t *timeout - timeout
*
*****************************************************************************/
static int
CalculateEventTime( time_t * timeout,
TimeoutType type )
* \return
*/
static int CalculateEventTime(
/*! [in] Timeout. */
time_t *timeout,
/*! [in] Timeout type. */
TimeoutType type)
{
time_t now;
@ -175,29 +158,22 @@ CalculateEventTime( time_t * timeout,
}
/****************************************************************************
* Function: CreateTimerEvent
/*!
* \brief Creates a Timer Event. (Dynamically allocated).
*
* Description:
* Creates a Timer Event. (Dynamically allocated)
* Internal to timer thread.
* Parameters:
* func - thread function to run.
* arg - argument to function.
* priority - priority of job.
* eventTime - the absoule time of the event
* in seconds from Jan, 1970
* id - id of job
*
* Returns:
* TimerEvent * on success, NULL on failure.
****************************************************************************/
static TimerEvent *
CreateTimerEvent( TimerThread * timer,
ThreadPoolJob * job,
Duration persistent,
time_t eventTime,
int id )
* \return (TimerEvent *) on success, NULL on failure.
*/
static TimerEvent *CreateTimerEvent(
/*! [in] Valid timer thread pointer. */
TimerThread *timer,
/*! [in] . */
ThreadPoolJob *job,
/*! [in] . */
Duration persistent,
/*! [in] The absoule time of the event in seconds from Jan, 1970. */
time_t eventTime,
/*! [in] Id of job. */
int id)
{
TimerEvent *temp = NULL;
@ -215,25 +191,8 @@ CreateTimerEvent( TimerThread * timer,
return temp;
}
/************************************************************************
* Function: TimerThreadInit
*
* Description:
* Initializes and starts timer thread.
*
* Parameters:
* timer - valid timer thread pointer.
* tp - valid thread pool to use. Must be
* started. Must be valid for lifetime
* of timer. Timer must be shutdown
* BEFORE thread pool.
* Return:
* 0 on success, nonzero on failure
* Returns error from ThreadPoolAddPersistent if failure.
************************************************************************/
int
TimerThreadInit( TimerThread * timer,
ThreadPool * tp )
int TimerThreadInit(TimerThread *timer, ThreadPool *tp)
{
int rc = 0;
@ -290,37 +249,14 @@ TimerThreadInit( TimerThread * timer,
}
/************************************************************************
* 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 TimerThreadSchedule(
TimerThread *timer,
time_t timeout,
TimeoutType type,
ThreadPoolJob *job,
Duration duration,
int *id)
{
int rc = EOUTOFMEM;
@ -394,28 +330,11 @@ TimerThreadSchedule( TimerThread * timer,
return rc;
}
/************************************************************************
* Function: TimerThreadRemove
*
* Description:
* Removes an event from the timer Q.
* Events can only be removed
* before they have been placed in the
* thread pool.
*
* Parameters:
* timer - valid timer thread pointer.
* id - id of event to remove.
* out - space for returned job (Can be NULL)
* Return:
* 0 on success.
* INVALID_EVENT_ID on error.
*
************************************************************************/
int
TimerThreadRemove( TimerThread * timer,
int id,
ThreadPoolJob * out )
int TimerThreadRemove(
TimerThread *timer,
int id,
ThreadPoolJob *out)
{
int rc = INVALID_EVENT_ID;
ListNode *tempNode = NULL;
@ -450,21 +369,8 @@ TimerThreadRemove( TimerThread * timer,
return rc;
}
/************************************************************************
* Function: TimerThreadShutdown
*
* Description:
* Shutdown the timer thread
* Events scheduled in the future will NOT be run.
* Timer thread should be shutdown BEFORE it's associated
* thread pool.
* Returns:
* returns 0 if succesfull,
* nonzero otherwise.
* Always returns 0.
***********************************************************************/
int
TimerThreadShutdown( TimerThread * timer )
int TimerThreadShutdown(TimerThread *timer)
{
ListNode *tempNode2 = NULL;
ListNode *tempNode = NULL;
@ -517,3 +423,4 @@ TimerThreadShutdown( TimerThread * timer )
return 0;
}