Improve threadutil

Remove "dereference NULL return" errors and implicit conversions to
double or enum types.
(cherry picked from commit 77c73884b81c887e24c2069d9528706c6caf3235)
This commit is contained in:
Fabrice 2012-03-09 22:28:02 +01:00 committed by Marcelo Roberto Jimenez
parent 783ebbc0ca
commit 2fc9200b85
5 changed files with 57 additions and 18 deletions

View File

@ -318,6 +318,13 @@ Version 1.8.0
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>
Optimisation of --disable-webserver

View File

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

View File

@ -2,6 +2,7 @@
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* 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.next = NULL;
return 0;
return retCode;
}
ListNode *ListAddHead(LinkedList *list, void *item)

View File

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

View File

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