threadutil: Doxygenation and compiler warnings.

(cherry picked from commit 7c524df1d91684abbfe710c606a69622de0dbd91)
This commit is contained in:
Marcelo Roberto Jimenez 2010-11-16 00:17:44 -02:00
parent 297d2ae877
commit 18f80bd778
8 changed files with 1053 additions and 1875 deletions

View File

@ -29,118 +29,99 @@
*
******************************************************************************/
#ifndef FREE_LIST_H
#define FREE_LIST_H
/*!
* \file
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "ithread.h"
#include <errno.h>
/****************************************************************************
* Name: FreeListNode
*
* Description:
* free list node. points to next free item.
* memory for node is borrowed from allocated items.
* Internal Use Only.
*****************************************************************************/
/*!
* Free list node. points to next free item.
* Memory for node is borrowed from allocated items.
* \internal
*/
typedef struct FREELISTNODE
{
struct FREELISTNODE *next;
} FreeListNode;
/****************************************************************************
* Name: FreeList
*
* Description:
* Stores head and size of free list, as well as mutex for protection.
* Internal Use Only.
*****************************************************************************/
/*!
* Stores head and size of free list, as well as mutex for protection.
* \internal
*/
typedef struct FREELIST
{
FreeListNode *head;
size_t element_size;
int maxFreeListLength;
int freeListLength;
}FreeList;
} FreeList;
/****************************************************************************
* Function: FreeListInit
/*!
* \brief Initializes Free List.
*
* Description:
* Initializes Free List. Must be called first.
* And only once for FreeList.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* size_t - size of elements to store in free list
* maxFreeListSize - max size that the free list can grow to
* before returning memory to O.S.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int FreeListInit(FreeList *free_list,
size_t elementSize,
int maxFreeListSize);
/****************************************************************************
* Function: FreeListAlloc
* Must be called first and only once for FreeList.
*
* Description:
* Allocates chunk of set size.
* If a free item is available in the list, returnes the stored item.
* Otherwise calls the O.S. to allocate memory.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* Returns:
* Non NULL on success. NULL on failure.
*****************************************************************************/
void * FreeListAlloc (FreeList *free_list);
* \return:
* \li \c 0 on success.
* \li \c EINVAL on failure.
*/
int FreeListInit(
/*! Must be valid, non null, pointer to a linked list. */
FreeList *free_list,
/*! Size of elements to store in free list. */
size_t elementSize,
/*! Max size that the free list can grow to before returning
* memory to O.S. */
int maxFreeListLength);
/****************************************************************************
* Function: FreeListFree
/*!
* \brief Allocates chunk of set size.
*
* Description:
* Returns an item to the Free List.
* If the free list is smaller than the max size than
* adds the item to the free list.
* Otherwise returns the item to the O.S.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int FreeListFree (FreeList *free_list,void * element);
/****************************************************************************
* Function: FreeListDestroy
* If a free item is available in the list, returnes the stored item,
* otherwise calls the O.S. to allocate memory.
*
* Description:
* Releases the resources stored with the free list.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int FreeListDestroy (FreeList *free_list);
* \return Non NULL on success. NULL on failure.
*/
void *FreeListAlloc(
/*! Must be valid, non null, pointer to a linked list. */
FreeList *free_list);
/*!
* \brief Returns an item to the Free List.
*
* If the free list is smaller than the max size then adds the item to the
* free list, otherwise returns the item to the O.S.
*
* \return:
* \li \c 0 on success.
* \li \c EINVAL on failure.
*/
int FreeListFree(
/*! Must be valid, non null, pointer to a free list. */
FreeList *free_list,
/*! Must be a pointer allocated by FreeListAlloc. */
void *element);
/*!
* \brief Releases the resources stored with the free list.
*
* \return:
* \li \c 0 on success.
* \li \c EINVAL on failure.
*/
int FreeListDestroy(
/*! Must be valid, non null, pointer to a linked list. */
FreeList *free_list);
#ifdef __cplusplus
}

View File

@ -29,323 +29,257 @@
*
******************************************************************************/
#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
*
* Description:
* Function for freeing list items
*****************************************************************************/
/*! Function for freeing list items. */
typedef void (*free_function)(void *arg);
/****************************************************************************
* Name: cmp_routine
*
* Description:
* Function for comparing list items
* Returns 1 if itemA==itemB
*****************************************************************************/
/*! Function for comparing list items. Returns 1 if itemA==itemB */
typedef int (*cmp_routine)(void *itemA,void *itemB);
/****************************************************************************
* Name: ListNode
*
* Description:
* linked list node. stores generic item and pointers to next and prev.
* Internal Use Only.
*****************************************************************************/
/*! Linked list node. Stores generic item and pointers to next and prev.
* \internal
*/
typedef struct LISTNODE
{
struct LISTNODE *prev;
struct LISTNODE *next;
void *item;
struct LISTNODE *prev;
struct LISTNODE *next;
void *item;
} ListNode;
/****************************************************************************
* Name: LinkedList
/*!
* Linked list (no protection).
*
* Description:
* linked list (no protection). Internal Use Only.
* Because this is for internal use, parameters are NOT checked for
* validity.
* The first item of the list is stored at node: head->next
* The last item of the list is stored at node: tail->prev
* If head->next=tail, then list is empty.
* To iterate through the list:
* Because this is for internal use, parameters are NOT checked for validity.
* The first item of the list is stored at node: head->next
* The last item of the list is stored at node: tail->prev
* If head->next=tail, then list is empty.
* To iterate through the list:
*
* LinkedList g;
* ListNode *temp = NULL;
* for (temp = ListHead(g);temp!=NULL;temp = ListNext(g,temp))
* {
* }
* LinkedList g;
* ListNode *temp = NULL;
* for (temp = ListHead(g);temp!=NULL;temp = ListNext(g,temp)) {
* }
*
*****************************************************************************/
* \internal
*/
typedef struct LINKEDLIST
{
ListNode head; /* head, first item is stored at: head->next */
ListNode tail; /* tail, last item is stored at: tail->prev */
long size; /* size of list */
FreeList freeNodeList; /* free list to use */
free_function free_func; /* free function to use */
cmp_routine cmp_func; /* compare function to use */
/*! head, first item is stored at: head->next */
ListNode head;
/*! tail, last item is stored at: tail->prev */
ListNode tail;
/*! size of list */
long size;
/*! free list to use */
FreeList freeNodeList;
/*! free function to use */
free_function free_func;
/*! compare function to use */
cmp_routine cmp_func;
} LinkedList;
/*!
* \brief Initializes LinkedList. Must be called first and only once for List.
*
* \return
* \li \c 0 on success.
* \li \c EOUTOFMEM on failure.
*/
int ListInit(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Function used to compare items. (May be NULL). */
cmp_routine cmp_func,
/*! Function used to free items. (May be NULL). */
free_function free_func);
/****************************************************************************
* Function: ListInit
/*!
* \brief Adds a node to the head of the list. Node gets immediately after
* list head.
*
* Description:
* Initializes LinkedList. Must be called first.
* And only once for List.
* Parameters:
* list - must be valid, non null, pointer to a linked list.
* cmp_func - function used to compare items. (May be NULL)
* free_func - function used to free items. (May be NULL)
* Returns:
* 0 on success, EOUTOFMEM on failure.
*****************************************************************************/
int ListInit(LinkedList *list,cmp_routine cmp_func, free_function free_func);
/****************************************************************************
* Function: ListAddHead
*
* Description:
* Adds a node to the head of the list.
* Node gets immediately after list.head.
* 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 *ListAddHead(LinkedList *list, void *item);
/****************************************************************************
* 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);
* \return The pointer to the ListNode on success, NULL on failure.
*/
ListNode *ListAddHead(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Item to be added. */
void *item);
/****************************************************************************
* Function: ListAddAfter
/*!
* \brief Adds a node to the tail of the list. Node gets added immediately
* before list.tail.
*
* Description:
* Adds a node after the specified node.
* Node gets added immediately after bnode.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* void * item - item to be added
* ListNode * bnode - node to add after
* Returns:
* The pointer to the ListNode on success, NULL on failure.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *ListAddAfter(LinkedList *list, void *item, ListNode *bnode);
/****************************************************************************
* Function: ListAddBefore
* Precondition: The list has been initialized.
*
* Description:
* Adds a node before the specified node.
* Node gets added immediately before anode.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode * anode - node to add the in front of.
* void * item - item to be added
* Returns:
* The pointer to the ListNode on success, NULL on failure.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *ListAddBefore(LinkedList *list,void *item, ListNode *anode);
* \return The pointer to the ListNode on success, NULL on failure.
*/
ListNode *ListAddTail(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Item to be added. */
void *item);
/****************************************************************************
* Function: ListDelNode
/*!
* \brief Adds a node after the specified node. Node gets added immediately
* after bnode.
*
* Description:
* Removes a node from the list
* The memory for the node is freed.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode *dnode - done to delete.
* freeItem - if !0 then item is freed using free function.
* if 0 (or free function is NULL) then item is not freed
* Returns:
* The pointer to the item stored in the node or NULL if the item is freed.
* Precondition:
* The list has been initialized.
*****************************************************************************/
void *ListDelNode(LinkedList *list,ListNode *dnode, int freeItem);
/****************************************************************************
* Function: ListDestroy
* Precondition: The list has been initialized.
*
* Description:
* Removes all memory associated with list nodes.
* Does not free LinkedList *list.
* \return The pointer to the ListNode on success, NULL on failure.
*/
ListNode *ListAddAfter(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Item to be added. */
void *item,
/*! Node to add after. */
ListNode *bnode);
/*!
* \brief Adds a node before the specified node. Node gets added immediately
* before anode.
*
* Precondition: The list has been initialized.
*
* \return The pointer to the ListNode on success, NULL on failure.
*/
ListNode *ListAddBefore(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Item to be added. */
void *item,
/*! Node to add in front of. */
ListNode *anode);
/*!
* \brief Removes a node from the list. The memory for the node is freed.
*
* Precondition: The list has been initialized.
*
* \return The pointer to the item stored in the node or NULL if the item
* is freed.
*/
void *ListDelNode(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Node to delete. */
ListNode *dnode,
/*! if !0 then item is freed using free function. If 0 (or free
* function is NULL) then item is not freed. */
int freeItem);
/*!
* \brief Removes all memory associated with list nodes. Does not free
* LinkedList *list.
*
* Precondition: The list has been initialized.
*
* \return 0 on success, EINVAL on failure.
*/
int ListDestroy(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! if !0 then item is freed using free function. If 0 (or free
* function is NULL) then item is not freed. */
int freeItem);
/*!
* \brief Returns the head of the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* freeItem - if !0 then items are freed using the free_function.
* if 0 (or free function is NULL) then items are not freed.
* Returns:
* 0 on success. Always returns 0.
* Precondition:
* The list has been initialized.
*****************************************************************************/
int ListDestroy(LinkedList *list, int freeItem);
/****************************************************************************
* Function: ListHead
* Precondition: The list has been initialized.
*
* Description:
* Returns the head of the list.
* \return The head of the list. NULL if list is empty.
*/
ListNode *ListHead(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list);
/*!
* \brief Returns the tail of the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The head of the list. NULL if list is empty.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode* ListHead(LinkedList *list);
/****************************************************************************
* Function: ListTail
* Precondition: The list has been initialized.
*
* Description:
* Returns the tail of the list.
* \return The tail of the list. NULL if list is empty.
*/
ListNode *ListTail(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list);
/*!
* \brief Returns the next item in the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The tail of the list. NULL if list is empty.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode* ListTail(LinkedList *list);
/****************************************************************************
* Function: ListNext
* Precondition: The list has been initialized.
*
* Description:
* Returns the next item in the list.
* \return The next item in the list. NULL if there are no more items in list.
*/
ListNode *ListNext(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Node from the list. */
ListNode *node);
/*!
* \brief Returns the previous item in the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The next item in the list. NULL if there are no more items in list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode* ListNext(LinkedList *list, ListNode * node);
/****************************************************************************
* Function: ListPrev
* Precondition: The list has been initialized.
*
* Description:
* Returns the previous item in the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The previous item in the list. NULL if there are no more items in list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode* ListPrev(LinkedList *list, ListNode * node);
* \return The previous item in the list. NULL if there are no more items in list.
*/
ListNode *ListPrev(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! Node from the list. */
ListNode *node);
/****************************************************************************
* Function: ListFind
/*!
* \brief Finds the specified item in the list.
*
* Description:
* Finds the specified item in the list.
* Uses the compare function specified in ListInit. If compare function
* is NULL then compares items as pointers.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode *start - the node to start from, NULL if to start from
* beginning.
* void * item - the item to search for.
* Returns:
* The node containing the item. NULL if no node contains the item.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode* ListFind(LinkedList *list, ListNode *start, void * item);
/****************************************************************************
* Function: ListSize
* Uses the compare function specified in ListInit. If compare function
* is NULL then compares items as pointers.
*
* Description:
* Returns the size of the list.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* Returns:
* The number of items in the list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
int ListSize(LinkedList* list);
* Precondition: The list has been initialized.
*
* \return The node containing the item. NULL if no node contains the item.
*/
ListNode* ListFind(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList *list,
/*! The node to start from, NULL if to start from beginning. */
ListNode *start,
/*! The item to search for. */
void *item);
/*!
* \brief Returns the size of the list.
*
* Precondition: The list has been initialized.
*
* \return The number of items in the list.
*/
long ListSize(
/*! Must be valid, non null, pointer to a linked list. */
LinkedList* list);
#ifdef __cplusplus
}

View File

@ -29,26 +29,21 @@
*
******************************************************************************/
#ifndef THREADPOOL_H
#define THREADPOOL_H
/*!
* \file
*/
#include "FreeList.h"
#include "ithread.h"
#include "LinkedList.h"
#include "UpnpInet.h"
#include "UpnpGlobal.h" /* for UPNP_INLINE, EXPORT_SPEC */
#include <errno.h>
#ifdef WIN32
#include <time.h>
struct timezone
@ -63,82 +58,63 @@
#if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__)
#include <sys/resource.h> /* for setpriority() */
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*! Size of job free list */
#define JOBFREELISTSIZE 100
#define INFINITE_THREADS -1
#define EMAXTHREADS (-8 & 1<<29)
/*! Invalid Policy */
#define INVALID_POLICY (-9 & 1<<29)
/*! Invalid JOB Id */
#define INVALID_JOB_ID (-2 & 1<<29)
typedef enum duration {
SHORT_TERM,
PERSISTENT
} Duration;
typedef enum priority {
LOW_PRIORITY,
MED_PRIORITY,
HIGH_PRIORITY
} ThreadPriority;
/*! default priority used by TPJobInit */
#define DEFAULT_PRIORITY MED_PRIORITY
/*! default minimum used by TPAttrInit */
#define DEFAULT_MIN_THREADS 1
/*! default max used by TPAttrInit */
#define DEFAULT_MAX_THREADS 10
/*! default stack size used by TPAttrInit */
#define DEFAULT_STACK_SIZE 0
/*! default jobs per thread used by TPAttrInit */
#define DEFAULT_JOBS_PER_THREAD 10
/*! default starvation time used by TPAttrInit */
#define DEFAULT_STARVATION_TIME 500
/*! default idle time used by TPAttrInit */
#define DEFAULT_IDLE_TIME 10 * 1000
/*! default free routine used TPJobInit */
#define DEFAULT_FREE_ROUTINE NULL
/*! default max jobs used TPAttrInit */
#define DEFAULT_MAX_JOBS_TOTAL 100
/*!
* \brief Statistics.
*
@ -146,71 +122,43 @@ typedef enum priority {
*/
#define STATS 1
#ifdef _DEBUG
#define DEBUG 1
#endif
typedef int PolicyType;
#define DEFAULT_POLICY SCHED_OTHER
/****************************************************************************
* Name: free_routine
*
* Description:
* Function for freeing a thread argument
*****************************************************************************/
/*! Function for freeing a thread argument. */
typedef void (*free_routine)(void *arg);
/****************************************************************************
* Name: ThreadPoolAttr
*
* Description:
* Attributes for thread pool. Used to set and change parameters of
* thread pool
*****************************************************************************/
/*! Attributes for thread pool. Used to set and change parameters of thread
* pool. */
typedef struct THREADPOOLATTR
{
/* minThreads, ThreadPool will always maintain at least this many threads */
/*! ThreadPool will always maintain at least this many threads. */
int minThreads;
/* maxThreads, ThreadPool will never have more than this number of threads */
/*! ThreadPool will never have more than this number of threads. */
int maxThreads;
/* stackSize (in bytes), this is the minimum stack size allocated for each
* thread */
/*! This is the minimum stack size allocated for each thread. */
size_t stackSize;
/* maxIdleTime (in milliseconds) this is the maximum time a thread will
* remain idle before dying */
/*! This is the maximum time a thread will
* remain idle before dying (in milliseconds). */
int maxIdleTime;
/* jobs per thread to maintain */
/*! Jobs per thread to maintain. */
int jobsPerThread;
/* maximum number of jobs that can be queued totally. */
/*! Maximum number of jobs that can be queued totally. */
int maxJobsTotal;
/* the time a low priority or med priority job waits before getting bumped
* up a priority (in milliseconds) */
/*! the time a low priority or med priority job waits before getting
* bumped up a priority (in milliseconds). */
int starvationTime;
/* scheduling policy to use */
/*! scheduling policy to use. */
PolicyType schedPolicy;
} ThreadPoolAttr;
/****************************************************************************
* Name: ThreadPool
*
* Description:
* Internal ThreadPool Job
*****************************************************************************/
/*! Internal ThreadPool Job. */
typedef struct THREADPOOLJOB
{
start_routine func;
@ -221,13 +169,7 @@ typedef struct THREADPOOLJOB
int jobId;
} ThreadPoolJob;
/****************************************************************************
* Name: ThreadPoolStats
*
* Description:
* Structure to hold statistics
*****************************************************************************/
/*! Structure to hold statistics. */
typedef struct TPOOLSTATS
{
double totalTimeHQ;
@ -251,7 +193,6 @@ typedef struct TPOOLSTATS
int currentJobsMQ;
} ThreadPoolStats;
/*!
* \brief A thread pool similar to the thread pool in the UPnP SDK.
*
@ -269,374 +210,324 @@ typedef struct TPOOLSTATS
*/
typedef struct THREADPOOL
{
ithread_mutex_t mutex; /* mutex to protect job qs */
ithread_cond_t condition; /* condition variable to signal Q */
ithread_cond_t start_and_shutdown; /* condition variable for start and stop */
int lastJobId; /* ids for jobs */
int shutdown; /* whether or not we are shutting down */
int totalThreads; /* total number of threads */
int busyThreads; /* number of threads that are currently executing jobs */
int persistentThreads; /* number of persistent threads */
FreeList jobFreeList; /* free list of jobs */
LinkedList lowJobQ; /* low priority job Q */
LinkedList medJobQ; /* med priority job Q */
LinkedList highJobQ; /* high priority job Q */
ThreadPoolJob *persistentJob; /* persistent job */
ThreadPoolAttr attr; /* thread pool attributes */
/* statistics */
/*! Mutex to protect job qs. */
ithread_mutex_t mutex;
/*! Condition variable to signal Q. */
ithread_cond_t condition;
/*! Condition variable for start and stop. */
ithread_cond_t start_and_shutdown;
/*! ids for jobs */
int lastJobId;
/*! whether or not we are shutting down */
int shutdown;
/*! total number of threads */
int totalThreads;
/*! number of threads that are currently executing jobs */
int busyThreads;
/*! number of persistent threads */
int persistentThreads;
/*! free list of jobs */
FreeList jobFreeList;
/*! low priority job Q */
LinkedList lowJobQ;
/*! med priority job Q */
LinkedList medJobQ;
/*! high priority job Q */
LinkedList highJobQ;
/*! persistent job */
ThreadPoolJob *persistentJob;
/*! thread pool attributes */
ThreadPoolAttr attr;
/*! statistics */
ThreadPoolStats stats;
} ThreadPool;
/*!
* \brief Initializes and starts ThreadPool. Must be called first and
* only once for ThreadPool.
*
* \return
* \li \c 0 on success.
* \li \c EAGAIN if not enough system resources to create minimum threads.
* \li \c INVALID_POLICY if schedPolicy can't be set.
* \li \c EMAXTHREADS if minimum threads is greater than maximum threads.
*/
int ThreadPoolInit(
/*! Must be valid, non null, pointer to ThreadPool. */
ThreadPool *tp,
/*! Can be null. if not null then attr contains the following fields:
* \li \c minWorkerThreads - minimum number of worker threads thread
* pool will never have less than this number of threads.
* \li \c maxWorkerThreads - maximum number of worker threads thread
* pool will never have more than this number of threads.
* \li \c maxIdleTime - maximum time that a worker thread will spend
* idle. If a worker is idle longer than this time and there are more
* than the min number of workers running, then the worker thread
* exits.
* \li \c jobsPerThread - ratio of jobs to thread to try and maintain
* if a job is scheduled and the number of jobs per thread is greater
* than this number,and if less than the maximum number of workers are
* running then a new thread is started to help out with efficiency.
* \li \c schedPolicy - scheduling policy to try and set (OS dependent).
*/
ThreadPoolAttr *attr);
/****************************************************************************
* Function: ThreadPoolInit
/*!
* \brief Adds a persistent job to the thread pool.
*
* Description:
* Initializes and starts ThreadPool. Must be called first.
* And only once for ThreadPool.
* Parameters:
* tp - must be valid, non null, pointer to ThreadPool.
* attr - can be null
*
* if not null then attr contains the following fields:
*
* minWorkerThreads - minimum number of worker threads
* thread pool will never have less than this
* number of threads.
* maxWorkerThreads - maximum number of worker threads
* thread pool will never have more than this
* number of threads.
* maxIdleTime - maximum time that a worker thread will spend
* idle. If a worker is idle longer than this
* time and there are more than the min
* number of workers running, than the
* worker thread exits.
* jobsPerThread - ratio of jobs to thread to try and maintain
* if a job is scheduled and the number of jobs per
* thread is greater than this number,and
* if less than the maximum number of
* workers are running then a new thread is
* started to help out with efficiency.
* schedPolicy - scheduling policy to try and set (OS dependent)
* Returns:
* 0 on success, nonzero on failure.
* EAGAIN if not enough system resources to create minimum threads.
* INVALID_POLICY if schedPolicy can't be set
* EMAXTHREADS if minimum threads is greater than maximum threads
*****************************************************************************/
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr);
/****************************************************************************
* Function: ThreadPoolAddPersistent
*
* Description:
* Adds a persistent job to the thread pool.
* Job will be run as soon as possible.
* Call will block until job is scheduled.
* Parameters:
* tp - valid thread pool pointer
* ThreadPoolJob - valid thread pool job with the following fields:
*
* func - ThreadFunction to run
* arg - argument to function.
* priority - priority of job.
* Job will be run as soon as possible. Call will block until job is scheduled.
*
* Returns:
* 0 on success, nonzero on failure
* EOUTOFMEM not enough memory to add job.
* EMAXTHREADS not enough threads to add persistent job.
*****************************************************************************/
int ThreadPoolAddPersistent(ThreadPool*tp, ThreadPoolJob *job, int *jobId);
* \return
* \li \c 0 on success.
* \li \c EOUTOFMEM not enough memory to add job.
* \li \c EMAXTHREADS not enough threads to add persistent job.
*/
int ThreadPoolAddPersistent(
/*! Valid thread pool pointer. */
ThreadPool*tp,
/*! Valid thread pool job. */
ThreadPoolJob *job,
/*! . */
int *jobId);
/****************************************************************************
* Function: ThreadPoolGetAttr
/*!
* \brief Gets the current set of attributes associated with the thread pool.
*
* Description:
* Gets the current set of attributes
* associated with the thread pool.
* Parameters:
* tp - valid thread pool pointer
* out - non null pointer to store attributes
* Returns:
* 0 on success, nonzero on failure
* Always returns 0.
*****************************************************************************/
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out);
* \return
* \li \c 0 on success, nonzero on failure.
*/
int ThreadPoolGetAttr(
/*! valid thread pool pointer. */
ThreadPool *tp,
/*! non null pointer to store attributes. */
ThreadPoolAttr *out);
/****************************************************************************
* Function: ThreadPoolSetAttr
/*!
* \brief Sets the attributes for the thread pool.
* Only affects future calculations.
*
* Description:
* Sets the attributes for the thread pool.
* Only affects future calculations.
* Parameters:
* tp - valid thread pool pointer
* attr - pointer to attributes, null sets attributes to default.
* Returns:
* 0 on success, nonzero on failure
* Returns INVALID_POLICY if policy can not be set.
*****************************************************************************/
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr);
* \return
* \li \c 0 on success, nonzero on failure.
* \li \c INVALID_POLICY if policy can not be set.
*/
int ThreadPoolSetAttr(
/*! valid thread pool pointer. */
ThreadPool *tp,
/*! pointer to attributes, null sets attributes to default. */
ThreadPoolAttr *attr);
/****************************************************************************
* Function: ThreadPoolAdd
/*!
* \brief Adds a job to the thread pool. Job will be run as soon as possible.
*
* Description:
* Adds a job to the thread pool.
* Job will be run as soon as possible.
* Parameters:
* tp - valid thread pool pointer
* func - ThreadFunction to run
* arg - argument to function.
* priority - priority of job.
* poolid - id of job
* free_function - function to use when freeing argument
* Returns:
* 0 on success, nonzero on failure
* EOUTOFMEM if not enough memory to add job.
*****************************************************************************/
int ThreadPoolAdd (ThreadPool*tp, ThreadPoolJob *job, int *jobId);
* \return
* \li \c 0 on success, nonzero on failure.
* \li \c EOUTOFMEM if not enough memory to add job.
*/
int ThreadPoolAdd(
/*! valid thread pool pointer. */
ThreadPool*tp,
/*! . */
ThreadPoolJob *job,
/*! id of job. */
int *jobId);
/****************************************************************************
* Function: ThreadPoolRemove
/*!
* \brief Removes a job from the thread pool. Can only remove jobs which
* are not currently running.
*
* Description:
* Removes a job from the thread pool.
* Can only remove jobs which are not
* currently running.
* Parameters:
* tp - valid thread pool pointer
* jobid - id of job
* out - space for removed job.
* Returns:
* 0 on success, nonzero on failure.
* INVALID_JOB_ID if job not found.
*****************************************************************************/
int ThreadPoolRemove(ThreadPool *tp, int jobId, ThreadPoolJob *out);
* \return
* \li \c 0 on success, nonzero on failure.
* \li \c INVALID_JOB_ID if job not found.
*/
int ThreadPoolRemove(
/*! valid thread pool pointer. */
ThreadPool *tp,
/*! id of job. */
int jobId,
/*! space for removed job. */
ThreadPoolJob *out);
/****************************************************************************
* Function: ThreadPoolShutdown
/*!
* \brief Shuts the thread pool down. Waits for all threads to finish.
* May block indefinitely if jobs do not exit.
*
* Description:
* Shuts the thread pool down.
* Waits for all threads to finish.
* May block indefinitely if jobs do not
* exit.
* Parameters:
* tp - must be valid tp
* Returns:
* 0 on success, nonzero on failure
* Always returns 0.
*****************************************************************************/
int ThreadPoolShutdown(ThreadPool *tp);
* \return 0 on success, nonzero on failure
*/
int ThreadPoolShutdown(
/*! must be valid tp. */
ThreadPool *tp);
/****************************************************************************
* Function: TPJobInit
/*!
* \brief Initializes thread pool job. Sets the priority to default defined
* in ThreadPool.h. Sets the free_routine to default defined in ThreadPool.h.
*
* Description:
* Initializes thread pool job.
* Sets the priority to default defined in ThreadPool.h.
* Sets the free_routine to default defined in ThreadPool.h
* Parameters:
* ThreadPoolJob *job - must be valid thread pool attributes.
* start_routine func - function to run, must be valid
* void * arg - argument to pass to function.
* Returns:
* Always returns 0.
*****************************************************************************/
int TPJobInit(ThreadPoolJob *job, start_routine func, void *arg);
* \return Always returns 0.
*/
int TPJobInit(
/*! must be valid thread pool attributes. */
ThreadPoolJob *job,
/*! function to run, must be valid. */
start_routine func,
/*! argument to pass to function. */
void *arg);
/****************************************************************************
* Function: TPJobSetPriority
/*!
* \brief Sets the max threads for the thread pool attributes.
*
* Description:
* Sets the max threads for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* maxThreads - value to set
* Returns:
* Always returns 0.
*****************************************************************************/
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority);
* \return Always returns 0.
*/
int TPJobSetPriority(
/*! must be valid thread pool attributes. */
ThreadPoolJob *job,
/*! value to set. */
ThreadPriority priority);
/****************************************************************************
* Function: TPJobSetFreeFunction
/*!
* \brief Sets the max threads for the thread pool attributes.
*
* Description:
* Sets the max threads for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* maxThreads - value to set
* Returns:
* Always returns 0.
*****************************************************************************/
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func);
* \return Always returns 0.
*/
int TPJobSetFreeFunction(
/*! must be valid thread pool attributes. */
ThreadPoolJob *job,
/*! value to set. */
free_routine func);
/****************************************************************************
* Function: TPAttrInit
/*!
* \brief Initializes thread pool attributes. Sets values to defaults defined
* in ThreadPool.h.
*
* Description:
* Initializes thread pool attributes.
* Sets values to defaults defined in ThreadPool.h.
* Parameters:
* attr - must be valid thread pool attributes.
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrInit(ThreadPoolAttr *attr);
* \return Always returns 0.
*/
int TPAttrInit(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr);
/****************************************************************************
* Function: TPAttrSetMaxThreads
/*!
* \brief Sets the max threads for the thread pool attributes.
*
* Description:
* Sets the max threads for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* maxThreads - value to set
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads);
* \return Always returns 0.
*/
int TPAttrSetMaxThreads(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! value to set. */
int maxThreads);
/****************************************************************************
* Function: TPAttrSetMinThreads
/*!
* \brief Sets the min threads for the thread pool attributes.
*
* Description:
* Sets the min threads for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* minThreads - value to set
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads);
* \return Always returns 0.
*/
int TPAttrSetMinThreads(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! value to set. */
int minThreads);
/****************************************************************************
* Function: TPAttrSetStackSize
/*!
* \brief Sets the stack size for the thread pool attributes.
*
* Description:
* Sets the stack size for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* stackSize - value to set
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize);
* \return Always returns 0.
*/
int TPAttrSetStackSize(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! value to set. */
size_t stackSize);
/****************************************************************************
* Function: TPAttrSetIdleTime
/*!
* \brief Sets the idle time for the thread pool attributes.
*
* Description:
* Sets the idle time for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime);
* \return Always returns 0.
*/
int TPAttrSetIdleTime(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! . */
int idleTime);
/****************************************************************************
* Function: TPAttrSetJobsPerThread
/*!
* \brief Sets the jobs per thread ratio
*
* Description:
* Sets the jobs per thread ratio
* Parameters:
* attr - must be valid thread pool attributes.
* jobsPerThread - number of jobs per thread to maintain
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread);
* \return Always returns 0.
*/
int TPAttrSetJobsPerThread(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! number of jobs per thread to maintain. */
int jobsPerThread);
/****************************************************************************
* Function: TPAttrSetStarvationTime
/*!
* \brief Sets the starvation time for the thread pool attributes.
*
* Description:
* Sets the starvation time for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* int starvationTime - milliseconds
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime);
* \return Always returns 0.
*/
int TPAttrSetStarvationTime(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! milliseconds. */
int starvationTime);
/****************************************************************************
* Function: TPAttrSetSchedPolicy
/*!
* \brief Sets the scheduling policy for the thread pool attributes.
*
* Description:
* Sets the scheduling policy for the thread pool attributes.
* Parameters:
* attr - must be valid thread pool attributes.
* PolicyType schedPolicy - must be a valid policy type.
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy);
* \return Always returns 0.
*/
int TPAttrSetSchedPolicy(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! must be a valid policy type. */
PolicyType schedPolicy);
/****************************************************************************
* Function: TPAttrSetMaxJobsTotal
/*!
* \brief Sets the maximum number jobs that can be qeued totally.
*
* Description:
* Sets the maximum number jobs that can be qeued totally.
* Parameters:
* attr - must be valid thread pool attributes.
* maxJobsTotal - maximum number of jobs
* Returns:
* Always returns 0.
*****************************************************************************/
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal);
* \return Always returns 0.
*/
int TPAttrSetMaxJobsTotal(
/*! must be valid thread pool attributes. */
ThreadPoolAttr *attr,
/*! maximum number of jobs. */
int maxJobsTotal);
/****************************************************************************
* Function: ThreadPoolGetStats
/*!
* \brief Returns various statistics about the thread pool.
*
* Description:
* Returns various statistics about the
* thread pool.
* Only valid if STATS has been defined.
* Parameters:
* ThreadPool *tp - valid initialized threadpool
* ThreadPoolStats *stats - valid stats, out parameter
* Returns:
* Always returns 0.
*****************************************************************************/
* Only valid if STATS has been defined.
*
* \return Always returns 0.
*/
#ifdef STATS
EXPORT_SPEC int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats);
EXPORT_SPEC void ThreadPoolPrintStats(ThreadPoolStats *stats);
EXPORT_SPEC int ThreadPoolGetStats(
/*! Valid initialized threadpool. */
ThreadPool *tp,
/*! Valid stats, out parameter. */
ThreadPoolStats *stats);
#else
static UPNP_INLINE int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats) {}
static UPNP_INLINE void ThreadPoolPrintStats(ThreadPoolStats *stats) {}
static UPNP_INLINE int ThreadPoolGetStats(
/*! Valid initialized threadpool. */
ThreadPool *tp,
/*! Valid stats, out parameter. */
ThreadPoolStats *stats) {}
#endif
/*!
* \brief
*/
#ifdef STATS
EXPORT_SPEC void ThreadPoolPrintStats(
/*! . */
ThreadPoolStats *stats);
#else
static UPNP_INLINE void ThreadPoolPrintStats(
/*! . */
ThreadPoolStats *stats) {}
#endif
#ifdef __cplusplus
}
#endif
#endif /* THREADPOOL_H */

View File

@ -29,35 +29,31 @@
*
******************************************************************************/
#ifndef TIMERTHREAD_H
#define TIMERTHREAD_H
/*!
* \file
*/
#include "FreeList.h"
#include "ithread.h"
#include "LinkedList.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;
/*! Timeout Types. */
typedef enum timeoutType {
/*! seconds from Jan 1, 1970. */
ABS_SEC,
/*! seconds from current time. */
REL_SEC
} TimeoutType;
/*!
* A timer thread similar to the one in the Upnp SDK that allows
@ -79,7 +75,6 @@ typedef struct TIMERTHREAD
ThreadPool *tp;
} TimerThread;
/*!
* Struct to contain information for a timer event.
*
@ -95,7 +90,6 @@ typedef struct TIMEREVENT
int id;
} TimerEvent;
/*!
* \brief Initializes and starts timer thread.
*
@ -109,7 +103,6 @@ int TimerThreadInit(
* lifetime of timer. Timer must be shutdown BEFORE thread pool. */
ThreadPool *tp);
/*!
* \brief Schedules an event to run at a specified time.
*
@ -132,7 +125,6 @@ int TimerThreadSchedule(
/*! [in] Id of timer event. (out, can be null). */
int *id);
/*!
* \brief Removes an event from the timer Q.
*
@ -148,7 +140,6 @@ int TimerThreadRemove(
/*! [in] Space for thread pool job. */
ThreadPoolJob *out);
/*!
* \brief Shutdown the timer thread.
*
@ -162,7 +153,6 @@ int TimerThreadShutdown(
/*! [in] Valid timer thread pointer. */
TimerThread *timer);
#ifdef __cplusplus
}
#endif

View File

@ -1,177 +1,111 @@
///////////////////////////////////////////////////////////////////////////
//
// 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 "FreeList.h"
#include <assert.h>
#include <stdlib.h>
/****************************************************************************
* Function: FreeListInit
*
* Description:
* Initializes Free List. Must be called first.
* And only once for FreeList.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* size_t - size of elements to store in free list
* maxFreeListSize - max size that the free list can grow to
* before returning memory to O.S.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int
FreeListInit( FreeList * free_list,
size_t elementSize,
int maxFreeListLength )
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
{
assert( free_list != NULL );
assert(free_list != NULL);
if( free_list == NULL )
return EINVAL;
if (free_list == NULL)
return EINVAL;
free_list->element_size = elementSize;
free_list->maxFreeListLength = maxFreeListLength;
free_list->head = NULL;
free_list->freeListLength = 0;
free_list->element_size = elementSize;
free_list->maxFreeListLength = maxFreeListLength;
free_list->head = NULL;
free_list->freeListLength = 0;
return 0;
return 0;
}
/****************************************************************************
* Function: FreeListAlloc
*
* Description:
* Allocates chunk of set size.
* If a free item is available in the list, returnes the stored item.
* Otherwise calls the O.S. to allocate memory.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* Returns:
* Non NULL on success. NULL on failure.
*****************************************************************************/
void *
FreeListAlloc( FreeList * free_list )
void *FreeListAlloc(FreeList *free_list)
{
FreeListNode *ret = NULL;
FreeListNode *ret = NULL;
assert( free_list != NULL );
assert(free_list != NULL);
if( free_list == NULL )
return NULL;
if (free_list == NULL)
return NULL;
if( free_list->head ) {
ret = free_list->head;
free_list->head = free_list->head->next;
free_list->freeListLength--;
} else {
ret = malloc( free_list->element_size );
}
if (free_list->head) {
ret = free_list->head;
free_list->head = free_list->head->next;
free_list->freeListLength--;
} else {
ret = malloc(free_list->element_size);
}
return ret;
return ret;
}
/****************************************************************************
* Function: FreeListFree
*
* Description:
* Returns an item to the Free List.
* If the free list is smaller than the max size than
* adds the item to the free list.
* Otherwise returns the item to the O.S.
* Parameters:
* free_list - must be valid, non null, pointer to a free list.
* element - must be a pointer allocated by FreeListAlloc
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int
FreeListFree( FreeList * free_list,
void *element )
int FreeListFree(FreeList *free_list, void *element)
{
FreeListNode *temp = NULL;
FreeListNode *temp = NULL;
assert(free_list != NULL);
assert( free_list != NULL );
if (free_list == NULL)
return EINVAL;
if (element != NULL &&
free_list->freeListLength + 1 < free_list->maxFreeListLength) {
free_list->freeListLength++;
temp = (FreeListNode *)element;
temp->next = free_list->head;
free_list->head = temp;
} else {
free(element);
}
if( free_list == NULL )
return EINVAL;
if( ( element != NULL ) &&
( ( free_list->freeListLength + 1 ) <
free_list->maxFreeListLength ) ) {
free_list->freeListLength++;
temp = ( FreeListNode * ) element;
temp->next = free_list->head;
free_list->head = temp;
} else {
free( element );
}
return 0;
return 0;
}
/****************************************************************************
* Function: FreeListDestroy
*
* Description:
* Releases the resources stored with the free list.
* Parameters:
* free_list - must be valid, non null, pointer to a linked list.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
*****************************************************************************/
int
FreeListDestroy( FreeList * free_list )
int FreeListDestroy(FreeList *free_list)
{
FreeListNode *temp = NULL;
int i = 0;
FreeListNode *temp = NULL;
int i = 0;
assert( free_list != NULL );
assert(free_list != NULL);
if( free_list == NULL )
return EINVAL;
if (!free_list)
return EINVAL;
while (free_list->head) {
i++;
temp = free_list->head->next;
free(free_list->head);
free_list->head = temp;
}
free_list->freeListLength = 0;
while( free_list->head ) {
i++;
temp = free_list->head->next;
free( free_list->head );
free_list->head = temp;
}
free_list->freeListLength = 0;
return 0;
return 0;
}

View File

@ -29,519 +29,281 @@
*
**************************************************************************/
#include "LinkedList.h"
#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 )
static int freeListNode(ListNode *node, LinkedList *list)
{
assert( list != NULL );
assert(list != NULL);
return FreeListFree( &list->freeNodeList, node );
return FreeListFree(&list->freeNodeList, node);
}
/****************************************************************************
* Function: CreateListNode
*
* Description:
* Creates a list node. Dynamically.
/*!
* \brief Dynamically creates a list node.
*
* Parameters:
* void * item - the item to store
* Returns:
* The new node, NULL on failure.
*****************************************************************************/
static ListNode *
CreateListNode( void *item,
LinkedList * list )
*/
static ListNode *CreateListNode(
/*! the item to store. */
void *item,
/*! The list to add it to. */
LinkedList *list)
{
ListNode *temp = NULL;
ListNode *temp = NULL;
assert(list != NULL);
assert( list != NULL );
temp = (ListNode *)FreeListAlloc(&list->freeNodeList);
if (temp) {
temp->prev = NULL;
temp->next = NULL;
temp->item = item;
}
temp = ( ListNode * ) FreeListAlloc( &list->freeNodeList );
if( temp ) {
temp->prev = NULL;
temp->next = NULL;
temp->item = item;
}
return temp;
return temp;
}
/****************************************************************************
* Function: ListInit
*
* Description:
* Initializes LinkedList. Must be called first.
* And only once for List.
* Parameters:
* list - must be valid, non null, pointer to a linked list.
* cmp_func - function used to compare items. (May be NULL)
* free_func - function used to free items. (May be NULL)
* Returns:
* 0 on success, EOUTOFMEM on failure.
*****************************************************************************/
int
ListInit( LinkedList * list,
cmp_routine cmp_func,
free_function free_func )
int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
{
int retCode = 0;
int retCode = 0;
assert(list != NULL);
assert( list != NULL );
if (!list)
return EINVAL;
list->size = 0;
list->cmp_func = cmp_func;
list->free_func = free_func;
retCode = FreeListInit(&list->freeNodeList, sizeof(ListNode), FREELISTSIZE);
if( list == NULL )
return EINVAL;
assert(retCode == 0);
list->size = 0;
list->cmp_func = cmp_func;
list->free_func = free_func;
list->head.item = NULL;
list->head.next = &list->tail;
list->head.prev = NULL;
list->tail.item = NULL;
list->tail.prev = &list->head;
list->tail.next = NULL;
retCode =
FreeListInit( &list->freeNodeList, sizeof( ListNode ),
FREELISTSIZE );
assert( retCode == 0 );
list->head.item = NULL;
list->head.next = &list->tail;
list->head.prev = NULL;
list->tail.item = NULL;
list->tail.prev = &list->head;
list->tail.next = NULL;
return 0;
return 0;
}
/****************************************************************************
* Function: ListAddHead
*
* Description:
* Adds a node to the head of the list.
* Node gets immediately after list.head.
* 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 *
ListAddHead( LinkedList * list,
void *item )
ListNode *ListAddHead(LinkedList *list, void *item)
{
assert( list != NULL );
assert(list != NULL);
if( list == NULL )
return NULL;
if (list == NULL)
return NULL;
return ListAddAfter( list, item, &list->head );
return ListAddAfter(list, item, &list->head);
}
/****************************************************************************
* 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 )
ListNode *ListAddTail(LinkedList *list, void *item)
{
assert( list != NULL );
assert(list != NULL);
if( list == NULL )
return NULL;
if (!list)
return NULL;
return ListAddBefore( list, item, &list->tail );
return ListAddBefore(list, item, &list->tail);
}
/****************************************************************************
* Function: ListAddAfter
*
* Description:
* Adds a node after the specified node.
* Node gets added immediately after bnode.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* void * item - item to be added
* ListNode * bnode - node to add after
* Returns:
* The pointer to the ListNode on success, NULL on failure.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListAddAfter( LinkedList * list,
void *item,
ListNode * bnode )
ListNode *ListAddAfter(LinkedList *list, void *item, ListNode *bnode)
{
ListNode *newNode = NULL;
ListNode *newNode = NULL;
assert( list != NULL );
assert(list != NULL);
if( ( list == NULL ) || ( bnode == NULL ) )
return NULL;
if (!list || !bnode)
return NULL;
newNode = CreateListNode(item, list);
if (newNode) {
ListNode *temp = bnode->next;
newNode = CreateListNode( item, list );
if( newNode ) {
ListNode *temp = bnode->next;
bnode->next = newNode;
newNode->prev = bnode;
newNode->next = temp;
temp->prev = newNode;
list->size++;
bnode->next = newNode;
newNode->prev = bnode;
newNode->next = temp;
temp->prev = newNode;
list->size++;
return newNode;
}
return NULL;
return newNode;
}
return NULL;
}
/****************************************************************************
* Function: ListAddBefore
*
* Description:
* Adds a node before the specified node.
* Node gets added immediately before anode.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode * anode - node to add the in front of.
* void * item - item to be added
* Returns:
* The pointer to the ListNode on success, NULL on failure.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListAddBefore( LinkedList * list,
void *item,
ListNode * anode )
ListNode *ListAddBefore(LinkedList *list, void *item, ListNode *anode)
{
ListNode *newNode = NULL;
ListNode *newNode = NULL;
assert( list != NULL );
assert(list != NULL);
if( ( list == NULL ) || ( anode == NULL ) )
return NULL;
if (!list || !anode)
return NULL;
newNode = CreateListNode(item, list);
if (newNode) {
ListNode *temp = anode->prev;
newNode = CreateListNode( item, list );
anode->prev = newNode;
newNode->next = anode;
newNode->prev = temp;
temp->next = newNode;
list->size++;
if( newNode ) {
ListNode *temp = anode->prev;
return newNode;
}
anode->prev = newNode;
newNode->next = anode;
newNode->prev = temp;
temp->next = newNode;
list->size++;
return newNode;
}
return NULL;
return NULL;
}
/****************************************************************************
* Function: ListDelNode
*
* Description:
* Removes a node from the list
* The memory for the node is freed but the
* the memory for the items are not.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode *dnode - done to delete.
* Returns:
* The pointer to the item stored in node on success, NULL on failure.
* Precondition:
* The list has been initialized.
*****************************************************************************/
void *
ListDelNode( LinkedList * list,
ListNode * dnode,
int freeItem )
void *ListDelNode(LinkedList *list, ListNode *dnode, int freeItem)
{
void *temp;
void *temp;
assert( list != NULL );
assert( dnode != &list->head );
assert( dnode != &list->tail );
assert(list != NULL);
assert(dnode != &list->head);
assert(dnode != &list->tail);
if( ( list == NULL ) ||
( dnode == &list->head ) ||
( dnode == &list->tail ) || ( dnode == NULL ) ) {
return NULL;
}
if (!list || dnode == &list->head || dnode == &list->tail || !dnode)
return NULL;
temp = dnode->item;
dnode->prev->next = dnode->next;
dnode->next->prev = dnode->prev;
freeListNode(dnode, list);
list->size--;
if (freeItem && list->free_func) {
list->free_func(temp);
temp = NULL;
}
temp = dnode->item;
dnode->prev->next = dnode->next;
dnode->next->prev = dnode->prev;
freeListNode( dnode, list );
list->size--;
if( freeItem && list->free_func ) {
list->free_func( temp );
temp = NULL;
}
return temp;
return temp;
}
/****************************************************************************
* Function: ListDestroy
*
* Description:
* Removes all memory associated with list nodes.
* Does not free LinkedList *list.
* Items stored in the list are not freed, only nodes are.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* Returns:
* 0 on success. Nonzero on failure.
* Always returns 0.
* Precondition:
* The list has been initialized.
*****************************************************************************/
int
ListDestroy( LinkedList * list,
int freeItem )
int ListDestroy(LinkedList *list, int freeItem)
{
ListNode *dnode = NULL;
ListNode *temp = NULL;
ListNode *dnode = NULL;
ListNode *temp = NULL;
if( list == NULL )
return EINVAL;
if(!list)
return EINVAL;
for( dnode = list->head.next; dnode != &list->tail; ) {
temp = dnode->next;
ListDelNode( list, dnode, freeItem );
dnode = temp;
}
for (dnode = list->head.next; dnode != &list->tail; ) {
temp = dnode->next;
ListDelNode(list, dnode, freeItem);
dnode = temp;
}
list->size = 0;
FreeListDestroy(&list->freeNodeList);
list->size = 0;
FreeListDestroy( &list->freeNodeList );
return 0;
return 0;
}
/****************************************************************************
* Function: ListHead
*
* Description:
* Returns the head of the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The head of the list. NULL if list is empty.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListHead( LinkedList * list )
ListNode *ListHead(LinkedList *list)
{
assert( list != NULL );
assert(list != NULL);
if( list == NULL )
return NULL;
if (!list)
return NULL;
if( list->size == 0 )
return NULL;
else
return list->head.next;
if (!list->size)
return NULL;
else
return list->head.next;
}
/****************************************************************************
* Function: ListTail
*
* Description:
* Returns the tail of the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The tail of the list. NULL if list is empty.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListTail( LinkedList * list )
ListNode *ListTail(LinkedList *list)
{
assert( list != NULL );
assert(list != NULL);
if( list == NULL )
return NULL;
if (!list)
return NULL;
if( list->size == 0 )
return NULL;
else
return list->tail.prev;
if (!list->size)
return NULL;
else
return list->tail.prev;
}
/****************************************************************************
* Function: ListNext
*
* Description:
* Returns the next item in the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The next item in the list. NULL if there are no more items in list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListNext( LinkedList * list,
ListNode * node )
ListNode *ListNext(LinkedList *list, ListNode *node)
{
assert( list != NULL );
assert( node != NULL );
assert(list != NULL);
assert(node != NULL);
if( ( list == NULL ) || ( node == NULL ) )
return NULL;
if( node->next == &list->tail )
return NULL;
else
return node->next;
if (!list || !node)
return NULL;
if (node->next == &list->tail)
return NULL;
else
return node->next;
}
/****************************************************************************
* Function: ListPrev
*
* Description:
* Returns the previous item in the list.
*
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
*
* Returns:
* The previous item in the list. NULL if there are no more items in list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListPrev( LinkedList * list,
ListNode * node )
ListNode *ListPrev(LinkedList *list, ListNode *node)
{
assert( list != NULL );
assert( node != NULL );
assert(list != NULL);
assert(node != NULL);
if( ( list == NULL ) || ( node == NULL ) )
return NULL;
if (!list || !node)
return NULL;
if( node->prev == &list->head )
return NULL;
else
return node->prev;
if (node->prev == &list->head)
return NULL;
else
return node->prev;
}
/****************************************************************************
* Function: ListFind
*
* Description:
* Finds the specified item in the list.
* Uses the compare function specified in ListInit. If compare function
* is NULL then compares items as pointers.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* ListNode *start - the node to start from, NULL if to start from
* beginning.
* void * item - the item to search for.
* Returns:
* The node containing the item. NULL if no node contains the item.
* Precondition:
* The list has been initialized.
*****************************************************************************/
ListNode *
ListFind( LinkedList * list,
ListNode * start,
void *item )
ListNode *ListFind(LinkedList *list, ListNode *start, void *item)
{
ListNode *finger = NULL;
ListNode *finger = NULL;
if (!list)
return NULL;
if (!start)
start = &list->head;
if( list == NULL )
return NULL;
assert(start);
if( start == NULL )
start = &list->head;
finger = start->next;
assert( start );
assert(finger);
finger = start->next;
assert( finger );
while( finger != &list->tail ) {
if( list->cmp_func ) {
if( list->cmp_func( item, finger->item ) )
return finger;
} else {
if( item == finger->item )
return finger;
}
finger = finger->next;
}
return NULL;
while (finger != &list->tail) {
if (list->cmp_func) {
if (list->cmp_func(item, finger->item))
return finger;
} else {
if (item == finger->item)
return finger;
}
finger = finger->next;
}
return NULL;
}
/****************************************************************************
* Function: ListSize
*
* Description:
* Returns the size of the list.
* Parameters:
* LinkedList *list - must be valid, non null, pointer to a linked list.
* Returns:
* The number of items in the list.
* Precondition:
* The list has been initialized.
*****************************************************************************/
int
ListSize( LinkedList * list )
long ListSize(LinkedList *list)
{
assert( list != NULL );
assert(list != NULL);
if( list == NULL )
return EINVAL;
if (!list)
return EINVAL;
return list->size;
return list->size;
}

File diff suppressed because it is too large Load Diff

View File

@ -29,18 +29,14 @@
*
******************************************************************************/
/*!
* \file
*/
#include "TimerThread.h"
#include <assert.h>
/*!
* \brief Deallocates a dynamically allocated TimerEvent.
*/
@ -55,7 +51,6 @@ static void FreeTimerEvent(
FreeListFree(&timer->freeEvents, event);
}
/*!
* \brief Implements timer thread.
*
@ -67,46 +62,34 @@ static void *TimerThreadWorker(
{
TimerThread *timer = ( TimerThread * ) arg;
ListNode *head = NULL;
TimerEvent *nextEvent = NULL;
time_t currentTime = 0;
time_t nextEventTime = 0;
struct timespec timeToWait;
int tempId;
assert( timer != NULL );
ithread_mutex_lock( &timer->mutex );
while( 1 )
{
//mutex should always be locked at top of loop
//Check for shutdown
if( timer->shutdown )
{
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;
//Get the next event if possible
if( timer->eventQ.size > 0 )
{
/* Get the next event if possible. */
if (timer->eventQ.size > 0) {
head = ListHead( &timer->eventQ );
nextEvent = ( TimerEvent * ) head->item;
nextEventTime = nextEvent->eventTime;
}
currentTime = time( NULL );
//If time has elapsed, schedule job
if( ( nextEvent != NULL ) && ( currentTime >= nextEventTime ) )
{
currentTime = time(NULL);
/* If time has elapsed, schedule job. */
if (nextEvent && currentTime >= nextEventTime) {
if( nextEvent->persistent ) {
ThreadPoolAddPersistent( timer->tp, &nextEvent->job,
&tempId );
@ -117,8 +100,7 @@ static void *TimerThreadWorker(
FreeTimerEvent( timer, nextEvent );
continue;
}
if( nextEvent != NULL ) {
if (nextEvent) {
timeToWait.tv_nsec = 0;
timeToWait.tv_sec = nextEvent->eventTime;
ithread_cond_timedwait( &timer->condition, &timer->mutex,
@ -146,16 +128,15 @@ static int CalculateEventTime(
assert( timeout != NULL );
if( type == ABS_SEC )
if (type == ABS_SEC)
return 0;
else if( type == REL_SEC ) {
time( &now );
else /*if (type == REL_SEC) */{
time(&now);
( *timeout ) += now;
return 0;
}
return -1;
}
/*!
@ -246,10 +227,8 @@ int TimerThreadInit(TimerThread *timer, ThreadPool *tp)
}
return rc;
}
int TimerThreadSchedule(
TimerThread *timer,
time_t timeout,
@ -258,7 +237,6 @@ int TimerThreadSchedule(
Duration duration,
int *id)
{
int rc = EOUTOFMEM;
int found = 0;
int tempId = 0;
@ -291,35 +269,25 @@ int TimerThreadSchedule(
}
tempNode = ListHead( &timer->eventQ );
//add job to Q
//Q is ordered by eventTime
//with the head of the Q being the next event
/* 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 )
if( temp->eventTime >= timeout ) {
if (ListAddBefore( &timer->eventQ, newEvent, tempNode))
rc = 0;
found = 1;
break;
}
tempNode = ListNext( &timer->eventQ, tempNode );
}
//add to the end of Q
if( !found ) {
/* add to the end of Q. */
if (!found) {
if( ListAddTail( &timer->eventQ, newEvent ) != NULL )
rc = 0;
}
//signal change in Q
/* signal change in Q. */
if( rc == 0 ) {
ithread_cond_signal( &timer->condition );
} else {
FreeTimerEvent( timer, newEvent );
@ -330,7 +298,6 @@ int TimerThreadSchedule(
return rc;
}
int TimerThreadRemove(
TimerThread *timer,
int id,
@ -369,7 +336,6 @@ int TimerThreadRemove(
return rc;
}
int TimerThreadShutdown(TimerThread *timer)
{
ListNode *tempNode2 = NULL;
@ -386,9 +352,7 @@ int TimerThreadShutdown(TimerThread *timer)
timer->shutdown = 1;
tempNode = ListHead( &timer->eventQ );
//Delete nodes in Q
//call registered free function
//on argument
/* Delete nodes in Q. Call registered free function on argument. */
while( tempNode != NULL ) {
TimerEvent *temp = ( TimerEvent * ) tempNode->item;
@ -406,19 +370,17 @@ int TimerThreadShutdown(TimerThread *timer)
ithread_cond_broadcast( &timer->condition );
while( timer->shutdown ) //wait for timer thread to shutdown
{
while (timer->shutdown) {
/* wait for timer thread to shutdown. */
ithread_cond_wait( &timer->condition, &timer->mutex );
}
ithread_mutex_unlock(&timer->mutex);
ithread_mutex_unlock( &timer->mutex );
//destroy condition
while( ithread_cond_destroy( &timer->condition ) != 0 ) {
/* destroy condition. */
while(ithread_cond_destroy(&timer->condition) != 0) {
}
//destroy mutex
while( ithread_mutex_destroy( &timer->mutex ) != 0 ) {
/* destroy mutex. */
while (ithread_mutex_destroy(&timer->mutex) != 0) {
}
return 0;