Improve threadutil

Remove "dereference NULL return" errors and implicit conversions to
double or enum types.
This commit is contained in:
Fabrice 2012-03-09 22:28:02 +01:00
parent 72eecacf56
commit 77c73884b8
5 changed files with 57 additions and 18 deletions

View File

@ -2,6 +2,13 @@
Version 1.6.16 Version 1.6.16
******************************************************************************* *******************************************************************************
2012-03-09 Fabrice Fontaine <fabrice.fontaine(at)orange.com>
Improve threadutil
Remove "dereference NULL return" errors and implicit conversions to
double or enum types.
2012-03-09 Fabrice Fontaine <fabrice.fontaine(at)orange.com> 2012-03-09 Fabrice Fontaine <fabrice.fontaine(at)orange.com>
Optimisation of --disable-webserver Optimisation of --disable-webserver

View File

@ -2,6 +2,7 @@
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
* All rights reserved. * All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -98,7 +99,7 @@ typedef enum priority {
#define DEFAULT_MAX_THREADS 10 #define DEFAULT_MAX_THREADS 10
/*! default stack size used by TPAttrInit */ /*! default stack size used by TPAttrInit */
#define DEFAULT_STACK_SIZE 0 #define DEFAULT_STACK_SIZE 0u
/*! default jobs per thread used by TPAttrInit */ /*! default jobs per thread used by TPAttrInit */
#define DEFAULT_JOBS_PER_THREAD 10 #define DEFAULT_JOBS_PER_THREAD 10
@ -165,7 +166,7 @@ typedef struct THREADPOOLJOB
void *arg; void *arg;
free_routine free_func; free_routine free_func;
struct timeval requestTime; struct timeval requestTime;
int priority; ThreadPriority priority;
int jobId; int jobId;
} ThreadPoolJob; } ThreadPoolJob;

View File

@ -2,6 +2,7 @@
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
* All rights reserved. * All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -102,7 +103,7 @@ int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
list->tail.prev = &list->head; list->tail.prev = &list->head;
list->tail.next = NULL; list->tail.next = NULL;
return 0; return retCode;
} }
ListNode *ListAddHead(LinkedList *list, void *item) ListNode *ListAddHead(LinkedList *list, void *item)

View File

@ -2,6 +2,7 @@
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
* All rights reserved. * All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -59,15 +60,15 @@ static long DiffMillis(
/*! . */ /*! . */
struct timeval *time2) struct timeval *time2)
{ {
double temp = 0; double temp = 0.0;
temp = (double)(time1->tv_sec - time2->tv_sec); temp = (double)time1->tv_sec - (double)time2->tv_sec;
/* convert to milliseconds */ /* convert to milliseconds */
temp *= 1000; temp *= 1000.0;
/* convert microseconds to milliseconds and add to temp */ /* convert microseconds to milliseconds and add to temp */
/* implicit flooring of unsigned long data type */ /* implicit flooring of unsigned long data type */
temp += (double)((time1->tv_usec - time2->tv_usec) / 1000); temp += ((double)time1->tv_usec - (double)time2->tv_usec) / 1000.0;
return (long)temp; return (long)temp;
} }
@ -476,7 +477,7 @@ static void *WorkerThread(
} }
retCode = 0; retCode = 0;
tp->stats.idleThreads++; tp->stats.idleThreads++;
tp->stats.totalWorkTime += (double)(StatsTime(NULL) - start); tp->stats.totalWorkTime += (double)StatsTime(NULL) - (double)start;
StatsTime(&start); StatsTime(&start);
if (persistent == 0) { if (persistent == 0) {
tp->stats.workerThreads--; tp->stats.workerThreads--;
@ -509,7 +510,7 @@ static void *WorkerThread(
} }
tp->stats.idleThreads--; tp->stats.idleThreads--;
/* idle time */ /* idle time */
tp->stats.totalIdleTime += (double)(StatsTime(NULL) - start); tp->stats.totalIdleTime += (double)StatsTime(NULL) - (double)start;
/* work time */ /* work time */
StatsTime(&start); StatsTime(&start);
/* bump priority of starved jobs */ /* bump priority of starved jobs */
@ -531,16 +532,28 @@ static void *WorkerThread(
/* Pick the highest priority job */ /* Pick the highest priority job */
if (tp->highJobQ.size > 0) { if (tp->highJobQ.size > 0) {
head = ListHead(&tp->highJobQ); head = ListHead(&tp->highJobQ);
if (head == NULL) {
tp->stats.workerThreads--;
goto exit_function;
}
job = (ThreadPoolJob *) head->item; job = (ThreadPoolJob *) head->item;
CalcWaitTime(tp, HIGH_PRIORITY, job); CalcWaitTime(tp, HIGH_PRIORITY, job);
ListDelNode(&tp->highJobQ, head, 0); ListDelNode(&tp->highJobQ, head, 0);
} else if (tp->medJobQ.size > 0) { } else if (tp->medJobQ.size > 0) {
head = ListHead(&tp->medJobQ); head = ListHead(&tp->medJobQ);
if (head == NULL) {
tp->stats.workerThreads--;
goto exit_function;
}
job = (ThreadPoolJob *) head->item; job = (ThreadPoolJob *) head->item;
CalcWaitTime(tp, MED_PRIORITY, job); CalcWaitTime(tp, MED_PRIORITY, job);
ListDelNode(&tp->medJobQ, head, 0); ListDelNode(&tp->medJobQ, head, 0);
} else if (tp->lowJobQ.size > 0) { } else if (tp->lowJobQ.size > 0) {
head = ListHead(&tp->lowJobQ); head = ListHead(&tp->lowJobQ);
if (head == NULL) {
tp->stats.workerThreads--;
goto exit_function;
}
job = (ThreadPoolJob *) head->item; job = (ThreadPoolJob *) head->item;
CalcWaitTime(tp, LOW_PRIORITY, job); CalcWaitTime(tp, LOW_PRIORITY, job);
ListDelNode(&tp->lowJobQ, head, 0); ListDelNode(&tp->lowJobQ, head, 0);
@ -961,6 +974,10 @@ int ThreadPoolShutdown(ThreadPool *tp)
/* clean up high priority jobs */ /* clean up high priority jobs */
while (tp->highJobQ.size) { while (tp->highJobQ.size) {
head = ListHead(&tp->highJobQ); head = ListHead(&tp->highJobQ);
if (head == NULL) {
ithread_mutex_unlock(&tp->mutex);
return EINVAL;
}
temp = (ThreadPoolJob *)head->item; temp = (ThreadPoolJob *)head->item;
if (temp->free_func) if (temp->free_func)
temp->free_func(temp->arg); temp->free_func(temp->arg);
@ -971,6 +988,10 @@ int ThreadPoolShutdown(ThreadPool *tp)
/* clean up med priority jobs */ /* clean up med priority jobs */
while (tp->medJobQ.size) { while (tp->medJobQ.size) {
head = ListHead(&tp->medJobQ); head = ListHead(&tp->medJobQ);
if (head == NULL) {
ithread_mutex_unlock(&tp->mutex);
return EINVAL;
}
temp = (ThreadPoolJob *)head->item; temp = (ThreadPoolJob *)head->item;
if (temp->free_func) if (temp->free_func)
temp->free_func(temp->arg); temp->free_func(temp->arg);
@ -981,6 +1002,10 @@ int ThreadPoolShutdown(ThreadPool *tp)
/* clean up low priority jobs */ /* clean up low priority jobs */
while (tp->lowJobQ.size) { while (tp->lowJobQ.size) {
head = ListHead(&tp->lowJobQ); head = ListHead(&tp->lowJobQ);
if (head == NULL) {
ithread_mutex_unlock(&tp->mutex);
return EINVAL;
}
temp = (ThreadPoolJob *)head->item; temp = (ThreadPoolJob *)head->item;
if (temp->free_func) if (temp->free_func)
temp->free_func(temp->arg); temp->free_func(temp->arg);
@ -1047,9 +1072,9 @@ int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
{ {
if (!job) if (!job)
return EINVAL; return EINVAL;
if (priority == LOW_PRIORITY || if (priority == (ThreadPriority)LOW_PRIORITY ||
priority == MED_PRIORITY || priority == (ThreadPriority)MED_PRIORITY ||
priority == HIGH_PRIORITY) { priority == (ThreadPriority)HIGH_PRIORITY) {
job->priority = priority; job->priority = priority;
return 0; return 0;
} else { } else {
@ -1170,17 +1195,17 @@ int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
*stats = tp->stats; *stats = tp->stats;
if (stats->totalJobsHQ > 0) if (stats->totalJobsHQ > 0)
stats->avgWaitHQ = stats->totalTimeHQ / stats->totalJobsHQ; stats->avgWaitHQ = stats->totalTimeHQ / (double)stats->totalJobsHQ;
else else
stats->avgWaitHQ = 0; stats->avgWaitHQ = 0;
if (stats->totalJobsMQ > 0) if (stats->totalJobsMQ > 0)
stats->avgWaitMQ = stats->totalTimeMQ / stats->totalJobsMQ; stats->avgWaitMQ = stats->totalTimeMQ / (double)stats->totalJobsMQ;
else else
stats->avgWaitMQ = 0; stats->avgWaitMQ = 0.0;
if (stats->totalJobsLQ > 0) if (stats->totalJobsLQ > 0)
stats->avgWaitLQ = stats->totalTimeLQ / stats->totalJobsLQ; stats->avgWaitLQ = stats->totalTimeLQ / (double)stats->totalJobsLQ;
else else
stats->avgWaitLQ = 0; stats->avgWaitLQ = 0.0;
stats->totalThreads = tp->totalThreads; stats->totalThreads = tp->totalThreads;
stats->persistentThreads = tp->persistentThreads; stats->persistentThreads = tp->persistentThreads;
stats->currentJobsHQ = (int)ListSize(&tp->highJobQ); stats->currentJobsHQ = (int)ListSize(&tp->highJobQ);

View File

@ -2,6 +2,7 @@
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
* All rights reserved. * All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -84,6 +85,10 @@ static void *TimerThreadWorker(
/* Get the next event if possible. */ /* Get the next event if possible. */
if (timer->eventQ.size > 0) { if (timer->eventQ.size > 0) {
head = ListHead( &timer->eventQ ); head = ListHead( &timer->eventQ );
if (head == NULL) {
ithread_mutex_unlock( &timer->mutex );
return NULL;
}
nextEvent = ( TimerEvent * ) head->item; nextEvent = ( TimerEvent * ) head->item;
nextEventTime = nextEvent->eventTime; nextEventTime = nextEvent->eventTime;
} }
@ -128,7 +133,7 @@ static int CalculateEventTime(
assert( timeout != NULL ); assert( timeout != NULL );
if (type == ABS_SEC) if (type == (TimeoutType)ABS_SEC)
return 0; return 0;
else /*if (type == REL_SEC) */{ else /*if (type == REL_SEC) */{
time(&now); time(&now);