LBe*_*nny 7 javascript c++ multithreading xul xpcom
在12.0以下的XULRunner版本中,它可以工作,但是当我尝试将其移植到12.0或更高版本时,它会使应用程序崩溃.主要原因是在sdk v12或更新的开发人员中删除代理对象到xpcom组件并建议通过使用nsRunnable/nsIRunnable包装对象来替换它,并通过函数NS_DispatchToMainThread将调用路由到主线程(单击此处)
我创建了db连接器,它通过回调与主线程异步和通信.使用: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);
}
Run Code Online (Sandbox Code Playgroud)
//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);
}
Run Code Online (Sandbox Code Playgroud)
//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();
}
Run Code Online (Sandbox Code Playgroud)
//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();
};
Run Code Online (Sandbox Code Playgroud)
//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;
}
Run Code Online (Sandbox Code Playgroud)
// *.js
nsDBService.query("SELECT * FROM t", function(phase, code, mes) {
//some UI actions or others db queries
});
Run Code Online (Sandbox Code Playgroud)
代码执行时应用程序冻结和崩溃如下所示:
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
Run Code Online (Sandbox Code Playgroud)
如果代码执行看起来像这样,一切都很好:
nsDBService::Query //main thread ok
NS_DispatchToMainThread //main thread
nsResultCallback::Run //main thread
nsIDBCallback::OnInfo //main thread ok
Run Code Online (Sandbox Code Playgroud)
题:
当从NS_DispatchToMainThread调用nsIDBCallback并从其他线程调用NS_DispatchToMainThread然后主应用程序线程,然后执行失败,我错过了什么,不明白?或者后台任务的另一种方法是什么?
无法重现,因为您没有提供独立的完整示例,因此请添加一些注释:
我注意到的第一件事是 的跨线程访问std::vector。您在评论中写了一些有关互斥锁的内容,所以这可能没问题。
肯定错误的是存储原始指针nsIDBCallback。XPCOM 对象是引用计数的。因此,一旦您的方法返回,如果没有其他引用,Query底层对象可能会被删除,从而在向量中留下一个悬空指针。delete我想这就是这里正在发生的事情!您需要保持对象处于活动状态,直到线程完成它,最好将其放入某处nsCOMPtr<nsIDBCallback>,例如放在nsCOMPArray<nsIDBCallback>.
PS:事实证明这是一个有点老的问题,我错过了......很抱歉延迟回答它:p