Changeset 28140 for trunk/psLib/src/sys/psThread.c
- Timestamp:
- May 27, 2010, 11:14:33 AM (16 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psThread.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psThread.c
r28138 r28140 21 21 #define TASK_BUCKETS 8 // Number of hash buckets for task list 22 22 23 // Mutex covers: 24 // * pending queue 25 // * done queue 26 // * thread->busy states 23 27 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for locking threads 28 24 29 static psList *pending = NULL; // queue of pending jobs 25 30 static psList *done = NULL; // queue of done jobs … … 28 33 static psArray *tsd = NULL; // Thread-specific data 29 34 30 31 35 /***** basic thread functions *****/ 32 36 … … 121 125 } 122 126 123 psThreadLock();124 127 psThreadJob *job = psListGetAndRemove(pending, PS_LIST_HEAD); 125 psThreadUnlock();126 128 return job; 127 129 } … … 134 136 } 135 137 136 psThreadLock();137 138 psThreadJob *job = psListGetAndRemove(done, PS_LIST_HEAD); 138 psThreadUnlock();139 139 return job; 140 140 } … … 198 198 // we have to lock here so the job queue cannot be empty yet no threads busy 199 199 psThreadJob *job = NULL; // Job to process 200 psThreadLock(); 200 201 while ((job = psThreadJobGetPending()) == NULL) { 202 // Unlock while sleeping, then lock to read the pending queue again 203 psThreadUnlock(); 201 204 usleep(THREAD_WAIT); 205 psThreadLock(); 202 206 } 203 207 self->busy = true; 208 psThreadUnlock(); 204 209 205 210 psThreadTask *task = psHashLookup(tasks, job->type); // Task to execute job … … 215 220 psListAdd(done, PS_LIST_TAIL, job); 216 221 psFree(job); 217 psThreadUnlock();218 222 219 223 if (!status) { … … 221 225 } 222 226 self->busy = false; 227 psThreadUnlock(); 223 228 } 224 229 } … … 253 258 } 254 259 255 // Harvest jobs from the 260 // Harvest jobs from the done list 256 261 static void psThreadJobHarvest(void) 257 262 { … … 270 275 // Ensure everything is harvested, if requested 271 276 if (harvest) { 277 // No threads, no no need to lock 272 278 psThreadJobHarvest(); 273 279 } … … 284 290 } 285 291 292 psThreadLock(); 293 286 294 // Harvest jobs in the background, if requested 287 295 if (harvest) { … … 289 297 } 290 298 291 #if 0 // Thread state doesn't matter, only the pending job queue292 299 // are all threads idle? 293 300 for (int i = 0; i < pool->n; i++) { … … 298 305 } 299 306 } 300 #endif 301 302 psThreadLock(); 303 bool more = (pending && pending->head) ? true : false; // More to process? 304 psThreadUnlock(); 305 306 if (!more) { 307 // Nothing in the queue 307 308 if (!pending || !pending->head) { 309 // Nothing in the queue and nothing more to add 308 310 // Ensure everything is harvested, if requested 309 311 if (harvest) { 310 312 psThreadJobHarvest(); 311 313 } 314 psThreadUnlock(); 312 315 return true; 313 316 } 314 317 318 SLEEP: 319 psThreadUnlock(); 315 320 usleep(THREAD_WAIT); 316 321 }
Note:
See TracChangeset
for help on using the changeset viewer.
