From 18f80bd77863ee38d8c4e5d0c02e2d0275ce1fed Mon Sep 17 00:00:00 2001 From: Marcelo Roberto Jimenez Date: Tue, 16 Nov 2010 00:17:44 -0200 Subject: [PATCH] threadutil: Doxygenation and compiler warnings. (cherry picked from commit 7c524df1d91684abbfe710c606a69622de0dbd91) --- threadutil/inc/FreeList.h | 139 +++---- threadutil/inc/LinkedList.h | 454 +++++++++------------ threadutil/inc/ThreadPool.h | 675 ++++++++++++++------------------ threadutil/inc/TimerThread.h | 24 +- threadutil/src/FreeList.c | 230 ++++------- threadutil/src/LinkedList.c | 576 ++++++++------------------- threadutil/src/ThreadPool.c | 736 +++++++++++------------------------ threadutil/src/TimerThread.c | 94 ++--- 8 files changed, 1053 insertions(+), 1875 deletions(-) diff --git a/threadutil/inc/FreeList.h b/threadutil/inc/FreeList.h index ff5d5c7..aaf1e0f 100644 --- a/threadutil/inc/FreeList.h +++ b/threadutil/inc/FreeList.h @@ -29,118 +29,99 @@ * ******************************************************************************/ - #ifndef FREE_LIST_H #define FREE_LIST_H - /*! * \file */ - #ifdef __cplusplus extern "C" { #endif - #include "ithread.h" - #include -/**************************************************************************** - * 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 } diff --git a/threadutil/inc/LinkedList.h b/threadutil/inc/LinkedList.h index c0909e3..89f01ca 100644 --- a/threadutil/inc/LinkedList.h +++ b/threadutil/inc/LinkedList.h @@ -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 } diff --git a/threadutil/inc/ThreadPool.h b/threadutil/inc/ThreadPool.h index 0b1044f..43e0a46 100644 --- a/threadutil/inc/ThreadPool.h +++ b/threadutil/inc/ThreadPool.h @@ -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 - #ifdef WIN32 #include struct timezone @@ -63,82 +58,63 @@ #if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__) #include /* 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 */ diff --git a/threadutil/inc/TimerThread.h b/threadutil/inc/TimerThread.h index 367ff01..bdbd707 100644 --- a/threadutil/inc/TimerThread.h +++ b/threadutil/inc/TimerThread.h @@ -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 diff --git a/threadutil/src/FreeList.c b/threadutil/src/FreeList.c index 80c3ac9..2c6f3f1 100644 --- a/threadutil/src/FreeList.c +++ b/threadutil/src/FreeList.c @@ -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 #include -/**************************************************************************** - * 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; } + diff --git a/threadutil/src/LinkedList.c b/threadutil/src/LinkedList.c index bc3e4c7..9e76a37 100644 --- a/threadutil/src/LinkedList.c +++ b/threadutil/src/LinkedList.c @@ -29,519 +29,281 @@ * **************************************************************************/ - #include "LinkedList.h" - #ifdef WIN32 /* Do not #include */ #else #include #endif - #if (defined(BSD) && BSD >= 199306) || defined(__OSX__) || defined(__APPLE__) #include #else #include #endif - #include - -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; } diff --git a/threadutil/src/ThreadPool.c b/threadutil/src/ThreadPool.c index d183c75..81b7595 100644 --- a/threadutil/src/ThreadPool.c +++ b/threadutil/src/ThreadPool.c @@ -29,67 +29,58 @@ * ******************************************************************************/ - /*! * \file */ - #if !defined(WIN32) #include #endif #include "ThreadPool.h" - #include "FreeList.h" - #include #include #include #include /* for memset()*/ - -/**************************************************************************** - * Function: DiffMillis +/*! + * \brief Returns the difference in milliseconds between two timeval structures. * - * Description: - * Returns the difference in milliseconds between two - * timeval structures. - * Internal Only. - * Parameters: - * struct timeval *time1, - * struct timeval *time2, - * Returns: - * the difference in milliseconds, time1-time2. - *****************************************************************************/ -static unsigned long DiffMillis(struct timeval *time1, struct timeval *time2) + * \internal + * + * \return The difference in milliseconds, time1-time2. + */ +static long DiffMillis( + /*! . */ + struct timeval *time1, + /*! . */ + struct timeval *time2) { double temp = 0; - temp = time1->tv_sec - time2->tv_sec; + temp = (double)(time1->tv_sec - time2->tv_sec); /* convert to milliseconds */ temp *= 1000; /* convert microseconds to milliseconds and add to temp */ /* implicit flooring of unsigned long data type */ - temp += (time1->tv_usec - time2->tv_usec) / 1000; + temp += (double)((time1->tv_usec - time2->tv_usec) / 1000); - return temp; + return (long)temp; } #ifdef STATS -/**************************************************************************** - * Function: StatsInit +/*! + * \brief Initializes the statistics structure. * - * Description: - * Initializes the statistics structure. - * Internal Only. - * Parameters: - * ThreadPoolStats *stats must be valid non null stats structure - *****************************************************************************/ -static void StatsInit(ThreadPoolStats *stats) + * \internal + */ +static void StatsInit( + /*! Must be valid non null stats structure. */ + ThreadPoolStats *stats) { stats->totalIdleTime = 0; stats->totalJobsHQ = 0; @@ -109,42 +100,70 @@ static void StatsInit(ThreadPoolStats *stats) stats->maxThreads = 0; stats->totalThreads = 0; } -static void StatsAccountLQ(ThreadPool *tp, unsigned long diffTime) +/*! + * \brief + * + * \internal + */ +static void StatsAccountLQ( + /*! . */ + ThreadPool *tp, + /*! . */ + long diffTime) { tp->stats.totalJobsLQ++; - tp->stats.totalTimeLQ += diffTime; + tp->stats.totalTimeLQ += (double)diffTime; } -static void StatsAccountMQ(ThreadPool *tp, unsigned long diffTime) +/*! + * \brief + * + * \internal + */ +static void StatsAccountMQ( + /*! . */ + ThreadPool *tp, + /*! . */ + long diffTime) { tp->stats.totalJobsMQ++; - tp->stats.totalTimeMQ += diffTime; + tp->stats.totalTimeMQ += (double)diffTime; } -static void StatsAccountHQ(ThreadPool *tp, unsigned long diffTime) +/*! + * \brief + * + * \internal + */ +static void StatsAccountHQ( + /*! . */ + ThreadPool *tp, + /*! . */ + long diffTime) { tp->stats.totalJobsHQ++; - tp->stats.totalTimeHQ += diffTime; + tp->stats.totalTimeHQ += (double)diffTime; } -/**************************************************************************** - * Function: CalcWaitTime +/*! + * \brief Calculates the time the job has been waiting at the specified + * priority. * - * Description: - * Calculates the time the job has been waiting at the specified - * priority. Adds to the totalTime and totalJobs kept in the - * thread pool statistics structure. - * Internal Only. + * Adds to the totalTime and totalJobs kept in the thread pool statistics + * structure. * - * Parameters: - * ThreadPool *tp - * ThreadPriority p - * ThreadPoolJob *job - *****************************************************************************/ -static void CalcWaitTime(ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job) + * \internal + */ +static void CalcWaitTime( + /*! . */ + ThreadPool *tp, + /*! . */ + ThreadPriority p, + /*! . */ + ThreadPoolJob *job) { struct timeval now; - unsigned long diff; + long diff; assert(tp != NULL); assert(job != NULL); @@ -166,36 +185,40 @@ static void CalcWaitTime(ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job) } } -static time_t StatsTime(time_t *t) +/*! + * \brief + * + * \internal + */ +static time_t StatsTime( + /*! . */ + time_t *t) { struct timeval tv; gettimeofday(&tv, NULL); - if (t) { + if (t) *t = tv.tv_sec; - } return tv.tv_sec; } #else /* STATS */ static UPNP_INLINE void StatsInit(ThreadPoolStats *stats) {} -static UPNP_INLINE void StatsAccountLQ(ThreadPool *tp, unsigned long diffTime) {} -static UPNP_INLINE void StatsAccountMQ(ThreadPool *tp, unsigned long diffTime) {} -static UPNP_INLINE void StatsAccountHQ(ThreadPool *tp, unsigned long diffTime) {} +static UPNP_INLINE void StatsAccountLQ(ThreadPool *tp, long diffTime) {} +static UPNP_INLINE void StatsAccountMQ(ThreadPool *tp, long diffTime) {} +static UPNP_INLINE void StatsAccountHQ(ThreadPool *tp, long diffTime) {} static UPNP_INLINE void CalcWaitTime(ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job) {} static UPNP_INLINE time_t StatsTime(time_t *t) { return 0; } #endif /* STATS */ -/**************************************************************************** - * Function: CmpThreadPoolJob +/*! + * \brief Compares thread pool jobs. * - * Description: - * Compares thread pool jobs. - * Parameters: - * void * - job A - * void * - job B - *****************************************************************************/ -static int CmpThreadPoolJob(void *jobA, void *jobB) + * \internal + */ +static int CmpThreadPoolJob( + void *jobA, + void *jobB) { ThreadPoolJob *a = (ThreadPoolJob *)jobA; ThreadPoolJob *b = (ThreadPoolJob *)jobB; @@ -203,33 +226,33 @@ static int CmpThreadPoolJob(void *jobA, void *jobB) return a->jobId == b->jobId; } -/**************************************************************************** - * Function: FreeThreadPoolJob +/*! + * \brief Deallocates a dynamically allocated ThreadPoolJob. * - * Description: - * Deallocates a dynamically allocated ThreadPoolJob. - * Parameters: - * ThreadPoolJob *tpj - must be allocated with CreateThreadPoolJob - *****************************************************************************/ -static void FreeThreadPoolJob(ThreadPool *tp, ThreadPoolJob *tpj) + * \internal + */ +static void FreeThreadPoolJob( + /*! . */ + ThreadPool *tp, + /*! Must be allocated with CreateThreadPoolJob. */ + ThreadPoolJob *tpj) { FreeListFree(&tp->jobFreeList, tpj); } -/**************************************************************************** - * Function: SetPolicyType +/*! + * \brief Sets the scheduling policy of the current process. * - * Description: - * Sets the scheduling policy of the current process. - * Internal only. - * Parameters: - * PolicyType in - * Returns: - * 0 on success, nonzero on failure - * Returns result of GetLastError() on failure. + * \internal + * + * \return + * \li \c 0 on success. + * \li \c result of GetLastError() on failure. * - *****************************************************************************/ -static int SetPolicyType(PolicyType in) + */ +static int SetPolicyType( + /*! . */ + PolicyType in) { int retVal = 0; #ifdef __CYGWIN__ @@ -255,21 +278,18 @@ static int SetPolicyType(PolicyType in) return retVal; } -/**************************************************************************** - * Function: SetPriority +/*! + * \brief Sets the priority of the currently running thread. * - * Description: - * Sets the priority of the currently running thread. - * Internal only. - * Parameters: - * ThreadPriority priority - * Returns: - * 0 on success, nonzero on failure - * EINVAL invalid priority - * Returns result of GerLastError on failure. - * - *****************************************************************************/ -static int SetPriority(ThreadPriority priority) + * \internal + * + * \return + * \li \c 0 on success. + * \li \c EINVAL invalid priority or the result of GerLastError. + */ +static int SetPriority( + /*! . */ + ThreadPriority priority) { int retVal = 0; #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0 @@ -311,23 +331,23 @@ exit_function: return retVal; } -/**************************************************************************** - * Function: BumpPriority +/*! + * \brief Determines whether any jobs need to be bumped to a higher priority Q + * and bumps them. * - * Description: - * Determines whether any jobs - * need to be bumped to a higher priority Q and bumps them. + * tp->mutex must be locked. * - * tp->mutex must be locked. - * Internal Only. - * Parameters: - * ThreadPool *tp - *****************************************************************************/ -static void BumpPriority(ThreadPool *tp) + * \internal + * + * \return + */ +static void BumpPriority( + /*! . */ + ThreadPool *tp) { int done = 0; struct timeval now; - unsigned long diffTime = 0; + long diffTime = 0; ThreadPoolJob *tempJob = NULL; gettimeofday(&now, NULL); @@ -360,18 +380,17 @@ static void BumpPriority(ThreadPool *tp) } } -/**************************************************************************** - * Function: SetRelTimeout +/*! + * \brief Sets the fields of the passed in timespec to be relMillis + * milliseconds in the future. * - * Description: - * Sets the fields of the - * passed in timespec to be relMillis milliseconds in the future. - * Internal Only. - * Parameters: - * struct timespec *time - * int relMillis - milliseconds in the future - *****************************************************************************/ -static void SetRelTimeout( struct timespec *time, int relMillis ) + * \internal + */ +static void SetRelTimeout( + /*! . */ + struct timespec *time, + /*! milliseconds in the future. */ + int relMillis) { struct timeval now; int sec = relMillis / 1000; @@ -382,17 +401,13 @@ static void SetRelTimeout( struct timespec *time, int relMillis ) time->tv_nsec = (now.tv_usec / 1000 + milliSeconds) * 1000000; } -/**************************************************************************** - * Function: SetSeed +/*! + * \brief Sets seed for random number generator. Each thread sets the seed + * random number generator. * - * Description: - * Sets seed for random number generator. - * Each thread sets the seed random number generator. - * Internal Only. - * Parameters: - * - *****************************************************************************/ -static void SetSeed() + * \internal + */ +static void SetSeed(void) { struct timeval t; @@ -402,7 +417,7 @@ static void SetSeed() #elif defined(BSD) || defined(__OSX__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) srand((unsigned int)t.tv_usec + (unsigned int)ithread_get_current_thread_id()); #elif defined(__linux__) || defined(__sun) || defined(__CYGWIN__) || defined(__GLIBC__) - srand((unsigned int)t.tv_usec + ithread_get_current_thread_id()); + srand((unsigned int)t.tv_usec + (unsigned int)ithread_get_current_thread_id()); #else { volatile union { @@ -416,21 +431,18 @@ static void SetSeed() #endif } -/**************************************************************************** - * Function: WorkerThread +/*! + * \brief Implements a thread pool worker. Worker waits for a job to become + * available. Worker picks up persistent jobs first, high priority, + * med priority, then low priority. * - * Description: - * Implements a thread pool worker. - * Worker waits for a job to become available. - * Worker picks up persistent jobs first, high priority, med priority, - * then low priority. - * If worker remains idle for more than specified max, the worker - * is released. - * Internal Only. - * Parameters: - * void * arg -> is cast to ThreadPool * - *****************************************************************************/ -static void *WorkerThread(void *arg) + * If worker remains idle for more than specified max, the worker is released. + * + * \internal + */ +static void *WorkerThread( + /*! arg -> is cast to (ThreadPool *). */ + void *arg) { time_t start = 0; @@ -460,11 +472,9 @@ static void *WorkerThread(void *arg) job = NULL; } retCode = 0; - tp->stats.idleThreads++; - tp->stats.totalWorkTime += ( StatsTime( NULL ) - start ); // work time - StatsTime( &start ); // idle time - + tp->stats.totalWorkTime += (double)(StatsTime(NULL) - start); + StatsTime(&start); if (persistent == 0) { tp->stats.workerThreads--; } else if (persistent == 1) { @@ -496,7 +506,7 @@ static void *WorkerThread(void *arg) } tp->stats.idleThreads--; /* idle time */ - tp->stats.totalIdleTime += StatsTime(NULL) - start; + tp->stats.totalIdleTime += (double)(StatsTime(NULL) - start); /* work time */ StatsTime(&start); /* bump priority of starved jobs */ @@ -561,20 +571,20 @@ exit_function: return NULL; } -/**************************************************************************** - * Function: CreateThreadPoolJob +/*! + * \brief Creates a Thread Pool Job. (Dynamically allocated) * - * Description: - * Creates a Thread Pool Job. (Dynamically allocated) - * Internal to thread pool. - * Parameters: - * ThreadPoolJob *job - job is copied - * id - id of job + * \internal * - * Returns: - * ThreadPoolJob *on success, NULL on failure. - *****************************************************************************/ -static ThreadPoolJob *CreateThreadPoolJob(ThreadPoolJob *job, int id, ThreadPool *tp) + * \return ThreadPoolJob *on success, NULL on failure. + */ +static ThreadPoolJob *CreateThreadPoolJob( + /*! job is copied. */ + ThreadPoolJob *job, + /*! id of job. */ + int id, + /*! . */ + ThreadPool *tp) { ThreadPoolJob *newJob = NULL; @@ -588,23 +598,20 @@ static ThreadPoolJob *CreateThreadPoolJob(ThreadPoolJob *job, int id, ThreadPool return newJob; } -/**************************************************************************** - * Function: CreateWorker +/*! + * \brief Creates a worker thread, if the thread pool does not already have + * max threads. * - * Description: - * Creates a worker thread, if the thread pool - * does not already have max threads. - * Internal to thread pool. - * Parameters: - * ThreadPool *tp + * \internal * - * Returns: - * 0 on success, <0 on failure - * EMAXTHREADS if already max threads reached - * EAGAIN if system can not create thread - * - *****************************************************************************/ -static int CreateWorker(ThreadPool *tp) + * \return + * \li \c 0 on success, < 0 on failure. + * \li \c EMAXTHREADS if already max threads reached. + * \li \c EAGAIN if system can not create thread. + */ +static int CreateWorker( + /*! . */ + ThreadPool *tp) { ithread_t temp; int rc = 0; @@ -632,21 +639,17 @@ static int CreateWorker(ThreadPool *tp) return rc; } -/**************************************************************************** - * Function: AddWorker +/*! + * \brief Determines whether or not a thread should be added based on the + * jobsPerThread ratio. Adds a thread if appropriate. * - * Description: - * Determines whether or not a thread should be added - * based on the jobsPerThread ratio. - * Adds a thread if appropriate. - * Internal to Thread Pool. - * Parameters: - * ThreadPool* tp - * - *****************************************************************************/ -static void AddWorker(ThreadPool *tp) + * \internal + */ +static void AddWorker( + /*! . */ + ThreadPool *tp) { - int jobs = 0; + long jobs = 0; int threads = 0; jobs = tp->highJobQ.size + tp->lowJobQ.size + tp->medJobQ.size; @@ -661,38 +664,6 @@ static void AddWorker(ThreadPool *tp) } } -/**************************************************************************** - * Function: ThreadPoolInit - * - * Description: - * Initializes and starts ThreadPool. Must be called first. - * And only once for ThreadPool. - * Parameters: - * tp - must be valid, non null, pointer to ThreadPool. - * 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) { int retCode = 0; @@ -756,25 +727,6 @@ int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr) return retCode; } -/**************************************************************************** - * Function: ThreadPoolAddPersistent - * - * Description: - * Adds a long term 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 - * job-> valid ThreadPoolJob pointer with following fields - * func - ThreadFunction to run - * arg - argument to function. - * priority - priority of job. - * free_function - function to use when freeing argument - * 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) { int ret = 0; @@ -824,29 +776,11 @@ exit_function: return ret; } -/**************************************************************************** - * Function: ThreadPoolAdd - * - * 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. - * jobId - id of job - * duration - whether or not this is a persistent thread - * 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) { int rc = EOUTOFMEM; int tempId = -1; - int totalJobs; + long totalJobs; ThreadPoolJob *temp = NULL; if (!tp || !job) { @@ -857,7 +791,7 @@ int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId) totalJobs = tp->highJobQ.size + tp->lowJobQ.size + tp->medJobQ.size; if (totalJobs >= tp->attr.maxJobsTotal) { - fprintf(stderr, "total jobs = %d, too many jobs", totalJobs); + fprintf(stderr, "total jobs = %ld, too many jobs", totalJobs); goto exit_function; } if (!jobId) { @@ -900,22 +834,6 @@ exit_function: return rc; } -/**************************************************************************** - * Function: ThreadPoolRemove - * - * 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 - * ThreadPoolJob *out - space for removed job. - * Can be null if not needed. - * - * Returns: - * 0 on success. INVALID_JOB_ID on failure. - *****************************************************************************/ int ThreadPoolRemove(ThreadPool *tp, int jobId, ThreadPoolJob *out) { int ret = INVALID_JOB_ID; @@ -975,19 +893,6 @@ exit_function: return ret; } -/**************************************************************************** - * Function: ThreadPoolGetAttr - * - * 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) { if (!tp || !out) { @@ -1004,19 +909,6 @@ int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out) return 0; } -/**************************************************************************** - * Function: ThreadPoolSetAttr - * - * 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) { int retCode = 0; @@ -1062,20 +954,6 @@ int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr) return retCode; } -/**************************************************************************** - * Function: ThreadPoolShutdown - * - * 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) { ListNode *head = NULL; @@ -1158,17 +1036,6 @@ int ThreadPoolShutdown(ThreadPool *tp) return 0; } -/**************************************************************************** - * Function: TPAttrInit - * - * 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) { if (!attr) { @@ -1186,20 +1053,6 @@ int TPAttrInit(ThreadPoolAttr *attr) return 0; } -/**************************************************************************** - * Function: TPJobInit - * - * 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) { if (!job || !func) { @@ -1213,18 +1066,6 @@ int TPJobInit(ThreadPoolJob *job, start_routine func, void *arg) return 0; } -/**************************************************************************** - * Function: TPJobSetPriority - * - * Description: - * Sets the max threads for the thread pool attributes. - * Parameters: - * attr - must be valid thread pool attributes. - * maxThreads - value to set - * Returns: - * Returns 0 on success nonzero on failure. - * Returns EINVAL if invalid priority. - *****************************************************************************/ int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority) { if (!job) { @@ -1240,17 +1081,6 @@ int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority) } } -/**************************************************************************** - * Function: TPJobSetFreeFunction - * - * 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) { if(!job) { @@ -1261,17 +1091,6 @@ int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func) return 0; } -/**************************************************************************** - * Function: TPAttrSetMaxThreads - * - * 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) { if (!attr) { @@ -1282,17 +1101,6 @@ int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads) return 0; } -/**************************************************************************** - * Function: TPAttrSetMinThreads - * - * 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) { if (!attr) { @@ -1303,17 +1111,6 @@ int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads) return 0; } -/**************************************************************************** - * Function: TPAttrSetStackSize - * - * 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) { if (!attr) { @@ -1324,17 +1121,6 @@ int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize) return 0; } - -/**************************************************************************** - * Function: TPAttrSetIdleTime - * - * 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) { if (!attr) { @@ -1345,16 +1131,6 @@ int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime) return 0; } -/**************************************************************************** - * Function: TPAttrSetJobsPerThread - * - * Description: - * Sets the max thre - * Parameters: - * attr - must be valid thread pool attributes. - * Returns: - * Always returns 0. - *****************************************************************************/ int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread) { if (!attr) { @@ -1365,16 +1141,6 @@ int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread) return 0; } -/**************************************************************************** - * Function: TPAttrSetStarvationTime - * - * Description: - * Sets the starvation time for the thread pool attributes. - * Parameters: - * attr - must be valid thread pool attributes. - * Returns: - * Always returns 0. - *****************************************************************************/ int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime) { if (!attr) { @@ -1385,17 +1151,6 @@ int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime) return 0; } -/**************************************************************************** - * Function: TPAttrSetSchedPolicy - * - * 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) { if (!attr) { @@ -1406,17 +1161,6 @@ int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy) return 0; } -/**************************************************************************** - * Function: TPAttrSetMaxJobsTotal - * - * 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) { if (!attr) { @@ -1427,7 +1171,6 @@ int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal) return 0; } - #ifdef STATS void ThreadPoolPrintStats(ThreadPoolStats *stats) { @@ -1450,23 +1193,7 @@ void ThreadPoolPrintStats(ThreadPoolStats *stats) printf("Total Time spent Working in seconds: %f\n", stats->totalWorkTime); printf("Total Time spent Idle in seconds : %f\n", stats->totalIdleTime); } -#endif /* STATS */ - -/**************************************************************************** - * Function: ThreadPoolGetStats - * - * 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. - *****************************************************************************/ -#ifdef STATS int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats) { if (tp == NULL || stats == NULL) { @@ -1496,9 +1223,9 @@ int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats) } stats->totalThreads = tp->totalThreads; stats->persistentThreads = tp->persistentThreads; - stats->currentJobsHQ = ListSize( &tp->highJobQ ); - stats->currentJobsLQ = ListSize( &tp->lowJobQ ); - stats->currentJobsMQ = ListSize( &tp->medJobQ ); + stats->currentJobsHQ = (int)ListSize(&tp->highJobQ); + stats->currentJobsLQ = (int)ListSize(&tp->lowJobQ); + stats->currentJobsMQ = (int)ListSize(&tp->medJobQ); /* if not shutdown then release mutex */ if (!tp->shutdown) { ithread_mutex_unlock(&tp->mutex); @@ -1506,47 +1233,44 @@ int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats) return 0; } - #endif /* STATS */ - #ifdef WIN32 + #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 + #else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL + #endif -#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 -#else - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL -#endif + int gettimeofday(struct timeval *tv, struct timezone *tz) + { + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; -int gettimeofday(struct timeval *tv, struct timezone *tz) -{ - FILETIME ft; - unsigned __int64 tmpres = 0; - static int tzflag; + if (tv) { + GetSystemTimeAsFileTime(&ft); - if (tv) { - GetSystemTimeAsFileTime(&ft); + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; - - /*converting file time to unix epoch*/ - tmpres /= 10; /*convert into microseconds*/ - tmpres -= DELTA_EPOCH_IN_MICROSECS; - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } - if (tz) { - if (!tzflag) { - _tzset(); - tzflag++; + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + if (tz) { + if (!tzflag) { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; } - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } - return 0; -} + return 0; + } #endif /* WIN32 */ diff --git a/threadutil/src/TimerThread.c b/threadutil/src/TimerThread.c index a8803b5..b821de0 100644 --- a/threadutil/src/TimerThread.c +++ b/threadutil/src/TimerThread.c @@ -29,18 +29,14 @@ * ******************************************************************************/ - /*! * \file */ - #include "TimerThread.h" - #include - /*! * \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;