Ignore:
Timestamp:
Dec 5, 2013 3:03:36 AM (10 years ago)
Author:
bennylp
Message:

Re #1519: fixed threading issues on Python. On Python, only threads created by Python can call Python. This creates problem with calling callback from worker thread. The SIP worker thread can be disabled, but we have other worker threads such as the sound device that cannot be disabled. The solution in this patch is to create small framework to post a job to "main thread" during libHandleEvents(). The main thread is thread that calls libCreate().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/pjsua2/pjsip/include/pjsua2/endpoint.hpp

    r4672 r4678  
    2727#include <pjsua2/media.hpp> 
    2828#include <pjsua2/siptypes.hpp> 
     29#include <list> 
    2930 
    3031/** PJSUA2 API is inside pj namespace */ 
     
    184185     */ 
    185186    unsigned            threadCnt; 
     187 
     188    /** 
     189     * When this flag is non-zero, all callbacks that come from thread 
     190     * other than main thread will be posted to the main thread and 
     191     * to be executed by Endpoint::libHandleEvents() function. This 
     192     * includes the logging callback. Note that this will only work if 
     193     * threadCnt is set to zero and Endpoint::libHandleEvents() is 
     194     * performed by main thread. By default, the main thread is set 
     195     * from the thread that invoke Endpoint::libCreate() 
     196     * 
     197     * Default: false 
     198     */ 
     199    bool                mainThreadOnly; 
    186200 
    187201    /** 
     
    635649}; 
    636650 
     651/* This represents posted job */ 
     652struct PendingJob 
     653{ 
     654    /** Perform the job */ 
     655    virtual void execute(bool is_pending) = 0; 
     656 
     657    /** Virtual destructor */ 
     658    virtual ~PendingJob() {} 
     659}; 
     660 
    637661////////////////////////////////////////////////////////////////////////////// 
    638662 
     
    717741     * structure, because polling then will be done by these worker threads 
    718742     * instead. 
     743     * 
     744     * If EpConfig::UaConfig::mainThreadOnly is enabled and this function 
     745     * is called from the main thread (by default the main thread is thread 
     746     * that calls libCreate()), this function will also scan and run any 
     747     * pending jobs in the list. 
    719748     * 
    720749     * @param msec_timeout Maximum time to wait, in miliseconds. 
     
    765794 
    766795    /** 
     796     * Write a log entry. 
     797     * 
     798     * @param e                 The log entry. 
     799     */ 
     800    void utilLogWrite(LogEntry &e); 
     801 
     802    /** 
    767803     * This is a utility function to verify that valid SIP url is given. If the 
    768804     * URL is a valid SIP/SIPS scheme, PJ_SUCCESS will be returned. 
     
    814850     */ 
    815851    void utilTimerCancel(Token prmToken); 
     852 
     853    /** 
     854     * Utility to register a pending job to be executed by main thread. 
     855     * If EpConfig::UaConfig::mainThreadOnly is false, the job will be 
     856     * executed immediately. 
     857     * 
     858     * @param job               The job class. 
     859     */ 
     860    void utilAddPendingJob(PendingJob *job); 
    816861 
    817862    /** 
     
    11221167    { PJ_UNUSED_ARG(prm); } 
    11231168 
    1124  
    11251169private: 
    11261170    static Endpoint             *instance_;     // static instance 
     
    11291173    AudDevManager                audioDevMgr; 
    11301174    CodecInfoVector              codecInfoList; 
     1175 
     1176    /* Pending logging */ 
     1177    bool                         mainThreadOnly; 
     1178    void                        *mainThread; 
     1179    unsigned                     pendingJobSize; 
     1180    std::list<PendingJob*>       pendingJobs; 
     1181 
     1182    void performPendingJobs(); 
    11311183 
    11321184    /* Endpoint static callbacks */ 
Note: See TracChangeset for help on using the changeset viewer.