Changeset 3950


Ignore:
Timestamp:
Feb 6, 2012 8:27:28 AM (12 years ago)
Author:
ming
Message:

Closed #1450: Add support for SDL job queue to grow in size.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-videodev/sdl_dev.c

    r3949 r3950  
    9999}; 
    100100 
    101 #define MAX_JOBS 8 
     101#define INITIAL_MAX_JOBS 64 
     102#define JOB_QUEUE_INC_FACTOR 2 
    102103 
    103104typedef pj_status_t (*job_func_ptr)(void *data); 
     
    129130 
    130131typedef 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; 
    133136    pj_mutex_t     *mutex; 
    134137    pj_thread_t    *thread; 
    135138    pj_sem_t       *sem; 
    136139 
    137     int             head, tail; 
     140    unsigned        size; 
     141    unsigned        head, tail; 
    138142    pj_bool_t       is_full; 
    139143    pj_bool_t       is_quitting; 
     
    12341238        jb = jq->jobs[jq->head]; 
    12351239        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        } 
    12411273    } 
    12421274 
     
    12501282 
    12511283    job_queue *jq = PJ_POOL_ZALLOC_T(pool, job_queue); 
    1252     pj_sem_create(pool, "thread_sem", 0, MAX_JOBS + 1, &jq->sem); 
    12531284 
    12541285#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 
     
    12631294#endif /* PJ_DARWINOS */ 
    12641295 
    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++) { 
    12661303        pj_sem_create(pool, "job_sem", 0, 1, &jq->job_sem[i]); 
    12671304    } 
     
    12971334#else /* PJ_DARWINOS */ 
    12981335    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     } 
    13051336    jq->jobs[jq->tail] = &jb; 
    13061337    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) { 
    13091340        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    } 
    13151353#endif /* PJ_DARWINOS */ 
    13161354 
     
    13351373        jq->sem = NULL; 
    13361374    } 
    1337     for (i = 0; i < MAX_JOBS; i++) { 
     1375    for (i = 0; i < jq->size; i++) { 
    13381376        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        } 
    13391382    } 
    13401383    pj_mutex_destroy(jq->mutex); 
Note: See TracChangeset for help on using the changeset viewer.