- Timestamp:
- Feb 6, 2012 8:27:28 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia-videodev/sdl_dev.c
r3949 r3950 99 99 }; 100 100 101 #define MAX_JOBS 8 101 #define INITIAL_MAX_JOBS 64 102 #define JOB_QUEUE_INC_FACTOR 2 102 103 103 104 typedef pj_status_t (*job_func_ptr)(void *data); … … 129 130 130 131 typedef struct job_queue { 131 job *jobs[MAX_JOBS]; 132 pj_sem_t *job_sem[MAX_JOBS]; 132 pj_pool_t *pool; 133 job **jobs; 134 pj_sem_t **job_sem; 135 pj_sem_t **old_sem; 133 136 pj_mutex_t *mutex; 134 137 pj_thread_t *thread; 135 138 pj_sem_t *sem; 136 139 137 int head, tail; 140 unsigned size; 141 unsigned head, tail; 138 142 pj_bool_t is_full; 139 143 pj_bool_t is_quitting; … … 1234 1238 jb = jq->jobs[jq->head]; 1235 1239 jb->retval = (*jb->func)(jb->data); 1236 pj_sem_post(jq->job_sem[jq->head]); 1237 pj_mutex_lock(jq->mutex); 1238 jq->head = (jq->head + 1) % MAX_JOBS; 1239 jq->is_full = PJ_FALSE; 1240 pj_mutex_unlock(jq->mutex); 1240 /* If job queue is full and we already finish all the pending 1241 * jobs, increase the size. 1242 */ 1243 if (jq->is_full && ((jq->head + 1) % jq->size == jq->tail)) { 1244 unsigned i, head; 1245 1246 if (jq->old_sem) { 1247 for (i = 0; i < jq->size / JOB_QUEUE_INC_FACTOR; i++) { 1248 pj_sem_destroy(jq->old_sem[i]); 1249 } 1250 } 1251 jq->old_sem = jq->job_sem; 1252 1253 /* Double the job queue size. */ 1254 jq->size *= JOB_QUEUE_INC_FACTOR; 1255 pj_sem_destroy(jq->sem); 1256 pj_sem_create(jq->pool, "thread_sem", 0, jq->size + 1, 1257 &jq->sem); 1258 jq->jobs = (job **)pj_pool_calloc(jq->pool, jq->size, 1259 sizeof(job *)); 1260 jq->job_sem = (pj_sem_t **) pj_pool_calloc(jq->pool, jq->size, 1261 sizeof(pj_sem_t *)); 1262 for (i = 0; i < jq->size; i++) { 1263 pj_sem_create(jq->pool, "job_sem", 0, 1, &jq->job_sem[i]); 1264 } 1265 jq->is_full = PJ_FALSE; 1266 head = jq->head; 1267 jq->head = jq->tail = 0; 1268 pj_sem_post(jq->old_sem[head]); 1269 } else { 1270 pj_sem_post(jq->job_sem[jq->head]); 1271 jq->head = (jq->head + 1) % jq->size; 1272 } 1241 1273 } 1242 1274 … … 1250 1282 1251 1283 job_queue *jq = PJ_POOL_ZALLOC_T(pool, job_queue); 1252 pj_sem_create(pool, "thread_sem", 0, MAX_JOBS + 1, &jq->sem);1253 1284 1254 1285 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 … … 1263 1294 #endif /* PJ_DARWINOS */ 1264 1295 1265 for (i = 0; i < MAX_JOBS; i++) { 1296 jq->pool = pool; 1297 jq->size = INITIAL_MAX_JOBS; 1298 pj_sem_create(pool, "thread_sem", 0, jq->size + 1, &jq->sem); 1299 jq->jobs = (job **)pj_pool_calloc(pool, jq->size, sizeof(job *)); 1300 jq->job_sem = (pj_sem_t **) pj_pool_calloc(pool, jq->size, 1301 sizeof(pj_sem_t *)); 1302 for (i = 0; i < jq->size; i++) { 1266 1303 pj_sem_create(pool, "job_sem", 0, 1, &jq->job_sem[i]); 1267 1304 } … … 1297 1334 #else /* PJ_DARWINOS */ 1298 1335 pj_mutex_lock(jq->mutex); 1299 if (jq->is_full) {1300 /* Sorry, the queue is full! */1301 pj_mutex_unlock(jq->mutex);1302 *retval = PJ_ETOOMANY;1303 return PJ_ETOOMANY;1304 }1305 1336 jq->jobs[jq->tail] = &jb; 1306 1337 tail = jq->tail; 1307 jq->tail = (jq->tail + 1) % MAX_JOBS;1308 if (jq->tail == jq->head) 1338 jq->tail = (jq->tail + 1) % jq->size; 1339 if (jq->tail == jq->head) { 1309 1340 jq->is_full = PJ_TRUE; 1310 pj_mutex_unlock(jq->mutex); 1311 1312 pj_sem_post(jq->sem); 1313 /* Wait until our posted job is completed. */ 1314 pj_sem_wait(jq->job_sem[tail]); 1341 PJ_LOG(4, (THIS_FILE, "SDL job queue is full, increasing " 1342 "the queue size.")); 1343 pj_sem_post(jq->sem); 1344 /* Wait until our posted job is completed. */ 1345 pj_sem_wait(jq->job_sem[tail]); 1346 pj_mutex_unlock(jq->mutex); 1347 } else { 1348 pj_mutex_unlock(jq->mutex); 1349 pj_sem_post(jq->sem); 1350 /* Wait until our posted job is completed. */ 1351 pj_sem_wait(jq->job_sem[tail]); 1352 } 1315 1353 #endif /* PJ_DARWINOS */ 1316 1354 … … 1335 1373 jq->sem = NULL; 1336 1374 } 1337 for (i = 0; i < MAX_JOBS; i++) {1375 for (i = 0; i < jq->size; i++) { 1338 1376 pj_sem_destroy(jq->job_sem[i]); 1377 } 1378 if (jq->old_sem) { 1379 for (i = 0; i < jq->size / JOB_QUEUE_INC_FACTOR; i++) { 1380 pj_sem_destroy(jq->old_sem[i]); 1381 } 1339 1382 } 1340 1383 pj_mutex_destroy(jq->mutex);
Note: See TracChangeset
for help on using the changeset viewer.