XPCOM C++多线程和JavaScript回调

7

背景

在XULRunner 12.0或更高版本中,它会崩溃应用程序,而在低于12.0的版本中它可以工作。主要原因是在SDK v12或更高版本中,开发人员删除了代理对象到xpcom组件,并建议通过使用nsRunnable/nsIRunnable封装对象并通过函数NS_DispatchToMainThread路由调用到主线程(点击这里)。

我正在开发什么?

我创建了一个与主线程通过回调通信的异步数据库连接器。使用:XULRunner v6,移植到XULRunner v17或更高版本。



    //nsIDBCallback.idl
    [scriptable, function, uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)]
    interface nsIDBCallback : nsISupports {
       void onInfo(in long phase, in long status, in string info);
    }




    //nsDBService.h, it is XPCOM component 
    class nsDBService : public nsIDBService, nsIRunnable
    {
    public:
       NS_DECL_ISUPPORTS
       NS_DECL_NSIRUNNABLE
       NS_DECL_NSIDBSERVICE

    private:
       std::vector<nsIThread*>     threads;
       std::vector<nsIDBCallback*> callbacks;
       std::vector<const char*>    sqls;

       nsIThread* makeNewThread();
       void       runOperationIfNotBussy();

    public:
       NS_IMETHODIMP Query(const char *sql, nsIDBCallback *callback);
    }




    //nsDBService.cpp
    // adding query and other data to buffers, 
    // it's thread safe, there are used mutex's
    NS_IMETHODIMP nsDBService::Query(const char *sql, nsIDBCallback *callback)
    {
       callbacks.push_back(callback);
       sqls     .push_back(sql);
       threads  .push_back( makeNewThread() );

       //run added operation if db driver is free, 
       //if driver is bussy then invocation is in buffer and need to wait
       runOperationIfNotBussy();

       return NS_OK;
    }

    void nsDBService::runOperationIfNotBussy() 
    {
       //some conditions, test's etc.

       //run first operation on list
       // RUNNING A THREAD, still ok
       if(...) threads.front()->Dispatch(this, nsIEventTarget::DISPATCH_NORMAL); 
    }

    //if this method is used by another thread+db query, 
    //then other operations can't run and need to wait
    //operations are stored and supported like fifo
    NS_IMETHODIMP nsDBService::Run(void)
    {
       //some other operations
       //real db operations in background

       int32_t phase = 3; //endphase
       int32_t code  = 0; //ok
       const char *msg = "OK";

       nsIDBCallback *callback = callbacks.pop();
       //wrapping callback function with runnable interface
       nsIRunnable   *runCallback = new nsResultCallback(callback, 
                                                         phase, 
                                                         code, 
                                                         msg);
       //routing event to main thread
       NS_DispatchToMainThread(runCallback, NS_DISPATCH_NORMAL); 

       runOperationIfNotBussy();
    }




    //nsResultCallback.h
    class nsResultCallback: public nsRunnable
    { 
    public:
       NS_DECL_ISUPPORTS
    public:
       NS_DECL_NSIRUNNABLE

    private:
       nsIDBCallback* callback;
       int32_t        resPhase;
       int32_t        resStatus;
       const char*    resMessage;

    public:
       nsResultCallback(nsIDBCallback* callback, 
                        int32_t phase, 
                        int32_t status, 
                        const std::string &message)   
          : callback(callback), 
            resPhase(phase), 
            resStatus(status), 
            resMessage(c_str_clone(message.c_str())) {};
       ~nsResultCallback();

    };




    //nsResultCallback.cpp
    NS_IMETHODIMP nsResultCallback::Run(void)
    {
       nsresult rv = NS_ERROR_FAILURE;
       try 
       {
          // APP HANDS AND CRUSH ! 
          if(this->callback) this->callback->OnInfo(resPhase, resStatus, resMessage);
       }
       catch(...) 
       {
          rv = NS_ERROR_UNEXPECTED;
          ERRF("nsBackpack::Run call method OnInfo from callback failed");
       }
       return rv;
    }

调用



    // *.js
    nsDBService.query("SELECT * FROM t", function(phase, code, mes) {
       //some UI actions or others db queries
    });

问题:

当代码执行如下时,应用程序会冻结和崩溃:



    nsDBService::Query //main thread ok
    nsDBService::runOperationIfNotBussy //main thread
    nsDBService::threads.front()->Dispatch //run bg thread
    nsDBService:Run //bg thread
    NS_DispatchToMainThread //main thread
    nsResultCallback::Run //main thread
    nsIDBCallback::OnInfo //main thread, crash

If code execution look like this, everything is ok:

                                                                                                       

nsDBService::Query //main thread ok
NS_DispatchToMainThread //main thread
nsResultCallback::Run //main thread
nsIDBCallback::OnInfo //main thread ok

问题:

当nsIDBCallback从NS_DispatchToMainThread被调用,并且NS_DispatchToMainThread是从主应用程序线程之外的其他线程调用时,执行会失败,我缺少什么,不理解?或者还有其他后台任务的方法吗?

1个回答

1

由于您没有提供一个自包含、完整的示例,因此我无法复现,以下是一些备注:

我注意到的第一件事是对 std::vector 的跨线程访问。您在注释中写了一些关于互斥锁的内容,所以这可能没问题。

肯定错误的是存储 nsIDBCallback 的裸指针。XPCOM 对象是引用计数的。因此,如果没有其他引用,当您的 Query 方法返回时,底层对象可能会被delete,使您的向量中留下一个悬空指针。我认为这是这里发生的情况!您需要保持对象在线程完成任务之前处于活动状态,最好将其放入某个地方的 nsCOMPtr<nsIDBCallback> 中,例如 nsCOMPArray<nsIDBCallback>

PS:事实证明,这是一个比较老的问题,我错过了它...非常抱歉回答晚了 :p


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接