diff options
author | dmichael@chromium.org <dmichael@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-08 18:55:16 +0000 |
---|---|---|
committer | dmichael@chromium.org <dmichael@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-08 18:55:16 +0000 |
commit | 77c3417c441feac4bdcbad88f5a6bbf7f77bfe8f (patch) | |
tree | de665a63fc4d8137139cb976a45a938fc02b0d72 /ppapi/proxy | |
parent | e92a3618d401cd78886f0a404aa28a7e7947bc37 (diff) | |
download | chromium_src-77c3417c441feac4bdcbad88f5a6bbf7f77bfe8f.zip chromium_src-77c3417c441feac4bdcbad88f5a6bbf7f77bfe8f.tar.gz chromium_src-77c3417c441feac4bdcbad88f5a6bbf7f77bfe8f.tar.bz2 |
PPAPI: Make CompletionCallbacks work right on background threads.
Now, TrackedCallback::Run will:
-Run the callback immediately if it is on the right thread.
-PostRun to the correct thread if it is not.
This was preceded by https://chromiumcodereview.appspot.com/10909244/, which removed ClearAndRun and does some other little cleanups to TrackedCallback to make it usable on background threads.
BUG=92909
Review URL: https://chromiumcodereview.appspot.com/10910099
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@166719 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/proxy')
-rw-r--r-- | ppapi/proxy/plugin_globals.cc | 4 | ||||
-rw-r--r-- | ppapi/proxy/plugin_globals.h | 1 | ||||
-rw-r--r-- | ppapi/proxy/ppb_message_loop_proxy.cc | 36 | ||||
-rw-r--r-- | ppapi/proxy/ppb_message_loop_proxy.h | 14 | ||||
-rw-r--r-- | ppapi/proxy/ppb_testing_proxy.cc | 6 |
5 files changed, 35 insertions, 26 deletions
diff --git a/ppapi/proxy/plugin_globals.cc b/ppapi/proxy/plugin_globals.cc index 4932d32..75369d0 100644 --- a/ppapi/proxy/plugin_globals.cc +++ b/ppapi/proxy/plugin_globals.cc @@ -105,6 +105,10 @@ void PluginGlobals::BroadcastLogWithSource(PP_Module /* module */, LogWithSource(0, level, source, value); } +MessageLoopShared* PluginGlobals::GetCurrentMessageLoop() { + return MessageLoopResource::GetCurrent(); +} + MessageLoopResource* PluginGlobals::loop_for_main_thread() { return loop_for_main_thread_.get(); } diff --git a/ppapi/proxy/plugin_globals.h b/ppapi/proxy/plugin_globals.h index 2bbc3f9..215ad63 100644 --- a/ppapi/proxy/plugin_globals.h +++ b/ppapi/proxy/plugin_globals.h @@ -56,6 +56,7 @@ class PPAPI_PROXY_EXPORT PluginGlobals : public PpapiGlobals { PP_LogLevel_Dev level, const std::string& source, const std::string& value) OVERRIDE; + virtual MessageLoopShared* GetCurrentMessageLoop() OVERRIDE; // Getters for the plugin-specific versions. PluginResourceTracker* plugin_resource_tracker() { diff --git a/ppapi/proxy/ppb_message_loop_proxy.cc b/ppapi/proxy/ppb_message_loop_proxy.cc index 916002d..b86ecf0 100644 --- a/ppapi/proxy/ppb_message_loop_proxy.cc +++ b/ppapi/proxy/ppb_message_loop_proxy.cc @@ -27,15 +27,15 @@ typedef thunk::EnterResource<PPB_MessageLoop_API> EnterMessageLoop; } MessageLoopResource::MessageLoopResource(PP_Instance instance) - : Resource(OBJECT_IS_PROXY, instance), + : MessageLoopShared(instance), nested_invocations_(0), destroyed_(false), should_destroy_(false), is_main_thread_loop_(false) { } -MessageLoopResource::MessageLoopResource(ForMainThread) - : Resource(Resource::Untracked()), +MessageLoopResource::MessageLoopResource(ForMainThread for_main_thread) + : MessageLoopShared(for_main_thread), nested_invocations_(0), destroyed_(false), should_destroy_(false), @@ -80,7 +80,7 @@ int32_t MessageLoopResource::AttachToCurrentThread() { if (slot->Get()) return PP_ERROR_INPROGRESS; } - // TODO(brettw) check that the current thread can support a message loop. + // TODO(dmichael) check that the current thread can support a message loop. // Take a ref to the MessageLoop on behalf of the TLS. Note that this is an // internal ref and not a plugin ref so the plugin can't accidentally @@ -140,20 +140,24 @@ int32_t MessageLoopResource::PostQuit(PP_Bool should_destroy) { if (PP_ToBool(should_destroy)) should_destroy_ = true; - if (IsCurrent()) + if (IsCurrent() && nested_invocations_ > 0) loop_->Quit(); else PostClosure(FROM_HERE, MessageLoop::QuitClosure(), 0); return PP_OK; } -void MessageLoopResource::DetachFromThread() { - // Never detach the main thread from its loop resource. Other plugin instances - // might need it. - if (is_main_thread_loop_) - return; +// static +MessageLoopResource* MessageLoopResource::GetCurrent() { + PluginGlobals* globals = PluginGlobals::Get(); + if (!globals->msg_loop_slot()) + return NULL; + return reinterpret_cast<MessageLoopResource*>( + globals->msg_loop_slot()->Get()); +} - // Note that the message loop must be destroyed on the thread is was created +void MessageLoopResource::DetachFromThread() { + // Note that the message loop must be destroyed on the thread it was created // on. loop_proxy_ = NULL; loop_.reset(); @@ -208,12 +212,10 @@ PP_Resource GetForMainThread() { } PP_Resource GetCurrent() { - PluginGlobals* globals = PluginGlobals::Get(); - if (!globals->msg_loop_slot()) - return 0; - MessageLoopResource* loop = reinterpret_cast<MessageLoopResource*>( - globals->msg_loop_slot()->Get()); - return loop->GetReference(); + Resource* resource = MessageLoopResource::GetCurrent(); + if (resource) + return resource->GetReference(); + return 0; } int32_t AttachToCurrentThread(PP_Resource message_loop) { diff --git a/ppapi/proxy/ppb_message_loop_proxy.h b/ppapi/proxy/ppb_message_loop_proxy.h index 325be78..689e439 100644 --- a/ppapi/proxy/ppb_message_loop_proxy.h +++ b/ppapi/proxy/ppb_message_loop_proxy.h @@ -11,7 +11,7 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "ppapi/proxy/interface_proxy.h" -#include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/ppb_message_loop_shared.h" #include "ppapi/thunk/ppb_message_loop_api.h" struct PPB_MessageLoop_Dev_0_1; @@ -19,13 +19,12 @@ struct PPB_MessageLoop_Dev_0_1; namespace ppapi { namespace proxy { -class MessageLoopResource : public Resource, public thunk::PPB_MessageLoop_API { +class MessageLoopResource : public MessageLoopShared { public: explicit MessageLoopResource(PP_Instance instance); // Construct the one MessageLoopResource for the main thread. This must be // invoked on the main thread. - struct ForMainThread {}; - MessageLoopResource(ForMainThread); + explicit MessageLoopResource(ForMainThread); virtual ~MessageLoopResource(); // Resource overrides. @@ -38,6 +37,7 @@ class MessageLoopResource : public Resource, public thunk::PPB_MessageLoop_API { int64_t delay_ms) OVERRIDE; virtual int32_t PostQuit(PP_Bool should_destroy) OVERRIDE; + static MessageLoopResource* GetCurrent(); void DetachFromThread(); bool is_main_thread_loop() const { return is_main_thread_loop_; @@ -58,9 +58,9 @@ class MessageLoopResource : public Resource, public thunk::PPB_MessageLoop_API { // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock. // This only makes sense for user code and completely thread-safe // proxy operations (e.g., MessageLoop::QuitClosure). - void PostClosure(const tracked_objects::Location& from_here, - const base::Closure& closure, - int64 delay_ms); + virtual void PostClosure(const tracked_objects::Location& from_here, + const base::Closure& closure, + int64 delay_ms) OVERRIDE; // TLS destructor function. static void ReleaseMessageLoop(void* value); diff --git a/ppapi/proxy/ppb_testing_proxy.cc b/ppapi/proxy/ppb_testing_proxy.cc index b8c9ce0..576302f 100644 --- a/ppapi/proxy/ppb_testing_proxy.cc +++ b/ppapi/proxy/ppb_testing_proxy.cc @@ -53,12 +53,14 @@ PP_Bool ReadImageData(PP_Resource graphics_2d, void RunMessageLoop(PP_Instance instance) { MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); - // TODO(dmichael): We should probably assert that this is the main thread. + CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> + BelongsToCurrentThread()); MessageLoop::current()->Run(); } void QuitMessageLoop(PP_Instance instance) { - // TODO(dmichael): We should probably assert that this is the main thread. + CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> + BelongsToCurrentThread()); MessageLoop::current()->QuitNow(); } |