]> git.sesse.net Git - nageru/commitdiff
(Hopefully! :-) ) fix some deadlocks during CEF shutdown.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 13 Mar 2018 17:56:31 +0000 (18:56 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 13 Mar 2018 17:56:31 +0000 (18:56 +0100)
cef_capture.cpp
cef_capture.h
nageru_cef_app.cpp

index e46f0c065f7287e0dce70992a070cea557a0497d..ff978f288640d8036cb33e1479b5e5c39c6a07d8 100644 (file)
@@ -41,7 +41,7 @@ CEFCapture::~CEFCapture()
 
 void CEFCapture::post_to_cef_ui_thread(std::function<void()> &&func)
 {
-       lock_guard<mutex> lock(browser_mutex);
+       lock_guard<recursive_mutex> lock(browser_mutex);
        if (browser != nullptr) {
                CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func)));
        } else {
@@ -113,7 +113,10 @@ void CEFCapture::OnPaint(const void *buffer, int width, int height)
                // so we need to do this explicitly, or we could be stuck on an
                // old frame forever if the image doesn't change.)
                post_to_cef_ui_thread([this] {
-                       browser->GetHost()->Invalidate(PET_VIEW);
+                       lock_guard<recursive_mutex> lock(browser_mutex);
+                       if (browser != nullptr) {  // Could happen if we are shutting down.
+                               browser->GetHost()->Invalidate(PET_VIEW);
+                       }
                });
                ++timecode;
        } else {
@@ -156,7 +159,7 @@ void CEFCapture::start_bm_capture()
        cef_app->initialize_cef();
 
        CefPostTask(TID_UI, new CEFTaskAdapter([this]{
-               lock_guard<mutex> lock(browser_mutex);
+               lock_guard<recursive_mutex> lock(browser_mutex);
 
                CefBrowserSettings browser_settings;
                browser_settings.web_security = cef_state_t::STATE_DISABLED;
@@ -175,9 +178,11 @@ void CEFCapture::start_bm_capture()
 
 void CEFCapture::stop_dequeue_thread()
 {
-       lock_guard<mutex> lock(browser_mutex);
-       cef_app->close_browser(browser);
-       browser = nullptr;  // Or unref_cef() will be sad.
+       {
+               lock_guard<recursive_mutex> lock(browser_mutex);
+               cef_app->close_browser(browser);
+               browser = nullptr;  // Or unref_cef() will be sad.
+       }
        cef_app->unref_cef();
 }
 
index 311076eb9afd0fcaf2942e9d218086c1e9673539..9e231c9d6fc654e3c9e769669bafceebf4abd816 100644 (file)
@@ -189,7 +189,9 @@ private:
        std::string description, start_url;
        std::atomic<int> max_fps{60};
 
-       std::mutex browser_mutex;
+       // Needs to be recursive because the lambda in OnPaint could cause
+       // OnPaint itself to be called.
+       std::recursive_mutex browser_mutex;
        CefRefPtr<CefBrowser> browser;  // Under <browser_mutex>.
 
        // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
index 2ddd81659aaf0a7af0e992d7ce06ac59faf4e4d8..2e64ceee9aa35b11b1016fa4674b56fdaa1f3859 100644 (file)
@@ -38,6 +38,7 @@ void NageruCefApp::unref_cef()
        unique_lock<mutex> lock(cef_mutex);
        if (--cef_thread_refcount == 0) {
                CefPostTask(TID_UI, new CEFTaskAdapter(&CefQuitMessageLoop));
+               lock.unlock();
                cef_thread.join();
        }
 }