Changeset 4887


Ignore:
Timestamp:
Aug 13, 2014 9:14:53 AM (6 years ago)
Author:
nanang
Message:

Close #1779: Add APIs for external/native thread registration to pjsua2: Endpoint::libRegisterThread() & Endpoint::libIsThreadRegistered().

Location:
pjproject/trunk/pjsip
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h

    r4860 r4887  
    18841884 
    18851885/** 
    1886  * Register a thread to poll for events. This function should be 
    1887  * called by an external worker thread, and it will block polling 
    1888  * for events until the library is destroyed. 
    1889  * 
    1890  * @return              PJ_SUCCESS if things are working correctly 
    1891  *                      or an error polling cannot be done for some 
    1892  *                      reason. 
    1893  */ 
    1894 PJ_DECL(pj_status_t) pjsua_register_worker_thread(const char *name); 
    1895  
    1896  
    1897 /** 
    18981886 * Signal all worker threads to quit. This will only wait until internal 
    1899  * threads are done. For external threads, application must perform 
    1900  * its own waiting for the external threads to quit from 
    1901  * pjsua_register_worker_thread() function. 
     1887 * threads are done. 
    19021888 */ 
    19031889PJ_DECL(void) pjsua_stop_worker_threads(void); 
     1890 
    19041891 
    19051892/** 
  • pjproject/trunk/pjsip/include/pjsua2/endpoint.hpp

    r4704 r4887  
    2828#include <pjsua2/siptypes.hpp> 
    2929#include <list> 
     30#include <map> 
    3031 
    3132/** PJSUA2 API is inside pj namespace */ 
     
    722723 
    723724    /** 
    724      * Register a thread to poll for events. This function should be 
    725      * called by an external worker thread, and it will block polling 
    726      * for events until the library is destroyed. 
    727      */ 
    728     void libRegisterWorkerThread(const string &name) throw(Error); 
     725     * Register a thread that was created by external or native API to the 
     726     * library. Note that each time this function is called, it will allocate 
     727     * some memory to store the thread description, which will only be freed 
     728     * when the library is destroyed. 
     729     * 
     730     * @param name      The optional name to be assigned to the thread. 
     731     */ 
     732    void libRegisterThread(const string &name) throw(Error); 
     733 
     734    /** 
     735     * Check if this thread has been registered to the library. Note that 
     736     * this function is only applicable for library main & worker threads and 
     737     * external/native threads registered using libRegisterThread(). 
     738     */ 
     739    bool libIsThreadRegistered(); 
    729740 
    730741    /** 
     
    11731184    AudDevManager                audioDevMgr; 
    11741185    CodecInfoVector              codecInfoList; 
     1186    std::map<pj_thread_t*, pj_thread_desc*> threadDescMap; 
    11751187 
    11761188    /* Pending logging */ 
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c

    r4861 r4887  
    700700} 
    701701 
    702 PJ_DEF(pj_status_t) pjsua_register_worker_thread(const char *name) 
    703 { 
    704     pj_thread_desc desc; 
    705     pj_thread_t *thread; 
    706     pj_status_t status; 
    707  
    708     if (pjsua_var.thread_quit_flag) 
    709         return PJ_EGONE; 
    710  
    711     status = pj_thread_register(NULL, desc, &thread); 
    712     if (status != PJ_SUCCESS) 
    713         return status; 
    714  
    715     if (name) 
    716         PJ_LOG(4,(THIS_FILE, "Worker thread %s started", name)); 
    717  
    718     worker_thread(NULL); 
    719  
    720     if (name) 
    721         PJ_LOG(4,(THIS_FILE, "Worker thread %s stopped", name)); 
    722  
    723     return PJ_SUCCESS; 
    724 } 
    725702 
    726703PJ_DEF(void) pjsua_stop_worker_threads(void) 
  • pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp

    r4847 r4887  
    2727using namespace std; 
    2828 
    29 #include <pjsua2/account.hpp> 
    30 #include <pjsua2/call.hpp> 
     29#include <pjsua-lib/pjsua_internal.h>   /* For retrieving pjsua threads */ 
    3130 
    3231#define THIS_FILE               "endpoint.cpp" 
     
    12181217    PJSUA2_CHECK_EXPR( pjsua_create() ); 
    12191218    mainThread = pj_thread_this(); 
     1219     
     1220    /* Register library main thread */ 
     1221    threadDescMap[pj_thread_this()] = NULL; 
    12201222} 
    12211223 
     
    12781280    /* Init! */ 
    12791281    PJSUA2_CHECK_EXPR( pjsua_init(&ua_cfg, &log_cfg, &med_cfg) ); 
     1282 
     1283    /* Register worker threads */ 
     1284    int i = pjsua_var.ua_cfg.thread_cnt; 
     1285    while (i) { 
     1286        pj_thread_t *t = pjsua_var.thread[--i]; 
     1287        if (t) 
     1288            threadDescMap[t] = NULL; 
     1289    } 
     1290 
     1291    /* Register media endpoint worker thread */ 
     1292    pjmedia_endpt *medept = pjsua_get_pjmedia_endpt(); 
     1293    i = pjmedia_endpt_get_thread_count(medept); 
     1294    while (i) { 
     1295        pj_thread_t *t = pjmedia_endpt_get_thread(medept, --i); 
     1296        if (t) 
     1297            threadDescMap[t] = NULL; 
     1298    } 
    12801299} 
    12811300 
     
    12851304} 
    12861305 
    1287 void Endpoint::libRegisterWorkerThread(const string &name) throw(Error) 
    1288 { 
    1289     PJSUA2_CHECK_EXPR(pjsua_register_worker_thread(name.c_str())); 
     1306void Endpoint::libRegisterThread(const string &name) throw(Error) 
     1307{ 
     1308    pj_thread_t *thread; 
     1309    pj_thread_desc *desc; 
     1310    pj_status_t status; 
     1311 
     1312    desc = (pj_thread_desc*)malloc(sizeof(pj_thread_desc)); 
     1313    status = pj_thread_register(name.c_str(), *desc, &thread); 
     1314    if (status == PJ_SUCCESS) { 
     1315        threadDescMap[thread] = desc; 
     1316    } else { 
     1317        free(desc); 
     1318        PJSUA2_RAISE_ERROR(status); 
     1319    } 
     1320} 
     1321 
     1322bool Endpoint::libIsThreadRegistered() 
     1323{ 
     1324    if (pj_thread_is_registered()) { 
     1325        /* Recheck again if it exists in the thread description map */ 
     1326        return (threadDescMap.find(pj_thread_this()) != threadDescMap.end()); 
     1327    } 
     1328 
     1329    return false; 
    12901330} 
    12911331 
     
    13151355    } 
    13161356#endif 
     1357 
     1358    /* Clean up thread descriptors */ 
     1359    std::map<pj_thread_t*, pj_thread_desc*>::iterator i; 
     1360    for (i = threadDescMap.begin(); i != threadDescMap.end(); ++i) { 
     1361        pj_thread_desc* d = (*i).second; 
     1362        if (d != NULL) 
     1363            free(d); 
     1364    } 
     1365    threadDescMap.clear(); 
    13171366 
    13181367    PJSUA2_CHECK_RAISE_ERROR(status); 
Note: See TracChangeset for help on using the changeset viewer.