nh41435
小狐狸
小狐狸
  • UID32134
  • 注册日期2010-02-26
  • 最后登录2010-05-11
  • 发帖数6
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
阅读:3525回复:2

api 是不是不支持多线程调用啊?

楼主#
更多 发布于:2010-04-17 17:39
如方法:
NPN_InvokeDefault

在npruntime 例子中,
我在 CPlugin  类中 加了个方法

void CPlugin::testFunc()
{
NPVariant tempVar;
bool b = NPN_GetProperty(m_pNPInstance, m_pNPWindowObj, sIdentifier_testID, &tempVar);


       。。。

NPN_ReleaseVariantValue(&tempVar);
}
如果是CPlugin 本身线程调用, NPN_GetProperty 返回 true, tempVar 有值。


但是新建启动另外一个线程, 把CPlugin指针传过去,
调用 这个testFunc,  跟踪调试进去
NPN_GetProperty 返回 false, tempVar 这个也没取到值。。


在这篇文章里,
https://developer.mozilla.org/en/Gecko_ ... 0scripting
有一段文字

(Threading model)
This API is not designed to be thread safe. The threading model for this API is such that all calls through this API are synchronous and calls from a plugin to methods in this API must come from the thread on which the plugin was initiated, and likewise all calls to methods in this API by the browser are guaranteed to come from the same thread. Future revisions to this API might provide a mechanism for proxying calls from one thread to another to aid in using this API from other threads.

google 翻译如下:
此API不设计为线程安全的。此API的线程模型是这样的:所有通过此API调用是同步的,并要求从插件的方法在此API必须来自于该插件发起线,同样在这个API的所有调用由浏览器中的方法保证来自同一线程。这个API的未来版本可能会提供一个代理从一个线程调用另一个帮助其他线程在使用这个API的机制。



请问各位大侠, 如何解决....谢谢。。。
nh41435
小狐狸
小狐狸
  • UID32134
  • 注册日期2010-02-26
  • 最后登录2010-05-11
  • 发帖数6
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
1楼#
发布于:2010-04-17 17:39
已经解决
调用
NPN_PluginThreadAsyncCall
nh41435
小狐狸
小狐狸
  • UID32134
  • 注册日期2010-02-26
  • 最后登录2010-05-11
  • 发帖数6
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
2楼#
发布于:2010-04-17 17:39
具体实现
void
NPN_PluginThreadAsyncCall(NPP plugin, void (*func)(void *),  void *userData);



具体实现如下:

1)在 np_entry.cpp
方法NP_Initialize
添加  NPNFuncs.pluginthreadasynccall = pFuncs->pluginthreadasynccall;

2)在npn_gate.cpp
添加
void NP_LOADDS NPN_PluginThreadAsyncCall(NPP instance, void (*func) (void *), void *userData)
{
NPNFuncs.pluginthreadasynccall(instance,  func,  userData);
}

3)在plugin.h
添加全局方法
 void AsyncCallFunc(void* pParm);  
添加CPlugin方法
 void CallAsyncCallFunc();
  

4)在plugin.cpp
添加
void AsyncCallFunc(void* pParm)
{
CPlugin* pTemp = reinterpret_cast<CPlugin*>(pParm);  

if (pParm == 0)
{
return;
}

pParm->testFunc();

delete pTemp;
}

5)实现类方法
void CPlugin::CallAsyncCallFunc()
{
     NPN_PluginThreadAsyncCall(m_pNPInstance, &AsyncCallFunc, this);
}

6)在新建启动另外一个线程里, 把原来调用方法testFunc 改为调用CallAsyncCallFunc方法
, 则 NPN_GetProperty 返回 true, tempVar 有值
游客

返回顶部