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 "UpnpGlobal.h"
#include "ixml.h"
/*! /*!
@@ -20,6 +21,13 @@
*/ */
#ifdef DEBUG #ifdef DEBUG
void IxmlPrintf( 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. */ /*! [in] Printf like format specification. */
const char* FmtStr, const char* FmtStr,
/*! [in] Printf like Variable number of arguments that will go in the debug /*! [in] Printf like Variable number of arguments that will go in the debug
@@ -27,7 +35,7 @@ void IxmlPrintf(
...) ...)
#if (__GNUC__ >= 3) #if (__GNUC__ >= 3)
/* This enables printf like format checking by the compiler */ /* This enables printf like format checking by the compiler */
__attribute__((format (__printf__, 1, 2))) __attribute__((format (__printf__, 4, 5)))
#endif #endif
; ;
#else /* DEBUG */ #else /* DEBUG */
@@ -37,5 +45,23 @@ static UPNP_INLINE void IxmlPrintf(
#endif /* DEBUG */ #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 */ #endif /* IXMLDEBUG_H */

View File

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

View File

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

View File

@@ -17,15 +17,54 @@
#ifdef DEBUG #ifdef DEBUG
void IxmlPrintf( void IxmlPrintf(
const char *DbgFileName,
int DbgLineNo,
const char *FunctionName,
const char *FmtStr, const char *FmtStr,
...) ...)
{ {
va_list ArgList; va_list ArgList;
va_start(ArgList, FmtStr); FILE *fp = stdout;
vfprintf(stdout, FmtStr, ArgList); fprintf(fp, "(%s::%s), line %d", DbgFileName, FunctionName, DbgLineNo);
fflush(stdout); if (FmtStr) {
va_end(ArgList); 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 #endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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