【问题标题】:XPCOM C++ multithread and javascript callbackXPCOM C++ 多线程和 javascript 回调
【发布时间】:2013-01-16 16:56:25
【问题描述】:

背景

在低于 12.0 的 XULRunner 版本中它可以工作,但是当我尝试将它移植到 12.0 或更高版本时,它会导致应用程序崩溃。 主要原因是在 sdk v12 或更新版本的开发人员中删除了 xpcom 组件的代理对象并建议替换它 通过使用 nsRunnable/nsIRunnable 包装对象并通过函数 NS_DispatchToMainThread 将调用路由到主线程(单击here

我正在开发什么?

我创建了异步的数据库连接器,并通过回调与主线程通信。 使用: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

如果代码执行如下所示,则一切正常:

                                                                                                       

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

问题:

当从 NS_DispatchToMainThread 调用 nsIDBCallback 并从其他线程调用 NS_DispatchToMainThread 时,然后是主应用程序线程,然后执行失败,我错过了什么,不明白吗?或者后台任务的另一种方法是什么?

【问题讨论】:

    标签: javascript c++ multithreading xul xpcom


    【解决方案1】:

    无法重现,因为您没有提供一个独立的、完整的示例,因此请注意:

    首先注意到std::vector的跨线程访问。您在 cmets 中写了一些关于互斥锁的内容,所以这可能没问题。

    确实错误是存储指向nsIDBCallback 的原始指针。 XPCOM 对象被引用计数。因此,一旦您的 Query 方法返回,如果没有其他对它的引用,基础对象可能是 deleted,在您的向量中留下一个悬空指针。我认为这就是这里发生的事情! 您需要保持对象处于活动状态,直到线程完成它,最好将其放入 nsCOMPtr&lt;nsIDBCallback&gt; 某处,例如在nsCOMPArray&lt;nsIDBCallback&gt;

    PS:原来这是一个有点老的问题,我错过了......非常抱歉延迟回答它:p

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多