IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 11, 2008, 5:10:15 PM (18 years ago)
Author:
Paul Price
Message:

Using a hash instead of an array for the tasks --- makes the lookup very simple.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psThread.c

    r18953 r19016  
    1414#include "psList.h"
    1515#include "psArray.h"
     16#include "psHash.h"
    1617#include "psString.h"
    1718#include "psThread.h"
    1819
    1920#define THREAD_WAIT 10000               // Microseconds to wait for threads
     21#define TASK_BUCKETS 8                  // Number of hash buckets for task list
    2022
    2123static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for locking threads
     
    2325static psList *done = NULL;             // queue of done jobs
    2426static psArray *pool = NULL;            // array of defined threads
    25 static psArray *tasks = NULL;           // queue of tasks
     27static psHash *tasks = NULL;            // List of defined tasks
     28
    2629
    2730/***** basic thread functions *****/
     
    9295
    9396        // find the corresponding task and run it
    94         for (int i = 0; i < tasks->n; i++) {
    95             psThreadTask *task = tasks->data[i];
    96             if (strcmp (job->type, task->type) != 0) {
    97                 continue;
    98             }
    99             psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
    100 
    101             bool status = task->function(job);
    102             return status;
    103         }
    104         // Programming error
    105         psAbort("Unable to find job %s", job->type);
     97        psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job
     98        psAssert(task, "Unable to find task %s", job->type);
     99        psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
     100        return task->function(job);
    106101    }
    107102
     
    164159
    165160    if (!tasks) {
    166         tasks = psArrayAllocEmpty(8);
    167     }
    168 
    169     psArrayAdd(tasks, 1, task);
    170     return true;
     161        tasks = psHashAlloc(TASK_BUCKETS);
     162    }
     163
     164    return psHashAdd(tasks, task->type, task);
     165}
     166
     167bool psThreadTaskDelete(const char *type)
     168{
     169    PS_ASSERT_STRING_NON_EMPTY(type, false);
     170
     171    return psHashRemove(tasks, type);
    171172}
    172173
     
    198199        self->busy = true;
    199200
    200         bool found = false;             // Found the job?
    201         for (int i = 0; i < tasks->n; i++) {
    202             psThreadTask *task = tasks->data[i]; // Task to do
    203             if (strcmp(job->type, task->type) != 0) {
    204                 continue;
    205             }
    206             found = true;
    207 
    208             psThreadUnlock();
    209             psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
    210 
    211             bool status = task->function(job); // Status of executing task
    212 
    213             // Put the completed job on the 'done' queue
    214             psThreadLock();
    215             if (!done) {
    216                 done = psListAlloc(NULL);
    217             }
    218             psListAdd(done, PS_LIST_TAIL, job);
    219             psFree(job);
    220 
    221             if (!status) {
    222                 self->fault = true;
    223             }
    224             self->busy = false;
    225             break;
    226         }
     201        psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job
    227202        psThreadUnlock();
    228         if (!found) {
    229             // Programming error
    230             psAbort("Unable to find job %s", job->type);
    231         }
     203
     204        psAssert(task, "Couldn't find thread task %s", job->type);
     205        psAssert(job->args->n == task->nArgs, "invalid number of arguments to %s", task->type);
     206        bool status = task->function(job); // Status of executing task
     207
     208        // Put the completed job on the 'done' queue
     209        psThreadLock();
     210        if (!done) {
     211            done = psListAlloc(NULL);
     212        }
     213        psListAdd(done, PS_LIST_TAIL, job);
     214        psFree(job);
     215
     216        if (!status) {
     217            self->fault = true;
     218        }
     219        self->busy = false;
     220        psThreadUnlock();
    232221    }
    233222}
Note: See TracChangeset for help on using the changeset viewer.