Changeset 19016 for trunk/psLib/src/sys/psThread.c
- Timestamp:
- Aug 11, 2008, 5:10:15 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psThread.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psThread.c
r18953 r19016 14 14 #include "psList.h" 15 15 #include "psArray.h" 16 #include "psHash.h" 16 17 #include "psString.h" 17 18 #include "psThread.h" 18 19 19 20 #define THREAD_WAIT 10000 // Microseconds to wait for threads 21 #define TASK_BUCKETS 8 // Number of hash buckets for task list 20 22 21 23 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for locking threads … … 23 25 static psList *done = NULL; // queue of done jobs 24 26 static psArray *pool = NULL; // array of defined threads 25 static psArray *tasks = NULL; // queue of tasks 27 static psHash *tasks = NULL; // List of defined tasks 28 26 29 27 30 /***** basic thread functions *****/ … … 92 95 93 96 // 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); 106 101 } 107 102 … … 164 159 165 160 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 167 bool psThreadTaskDelete(const char *type) 168 { 169 PS_ASSERT_STRING_NON_EMPTY(type, false); 170 171 return psHashRemove(tasks, type); 171 172 } 172 173 … … 198 199 self->busy = true; 199 200 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 227 202 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(); 232 221 } 233 222 }
Note:
See TracChangeset
for help on using the changeset viewer.
