platform_uithread.h (3452B)
1 /* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef RTC_BASE_PLATFORM_UITHREAD_H_ 12 #define RTC_BASE_PLATFORM_UITHREAD_H_ 13 14 #if defined(WEBRTC_WIN) 15 # include "Assertions.h" 16 # include "ThreadSafety.h" 17 # include "api/sequence_checker.h" 18 # include "rtc_base/deprecated/recursive_critical_section.h" 19 # include "rtc_base/platform_thread.h" 20 21 namespace webrtc { 22 /* 23 * Windows UI thread for screen capture 24 * Launches a thread which enters a message wait loop after calling the 25 * provided ThreadRunFunction once. A repeating timer event might be registered 26 * with a callback through the Win32 API. If so, that timer will cause WM_TIMER 27 * messages to appear in the threads message queue. This will wake the thread 28 * which will then first look to see if it received the WM_QUIT message, then 29 * it will pass any non WM_QUIT messages on to the registered message handlers 30 * (synchronously on the current thread). In the case oF WM_TIMER the 31 * registered handler calls the NativeEventCallback which is simply the 32 * ThreadRunFunction which was passed to the constructor. 33 * 34 * Shutdown of the message wait loop is triggered by sending a WM_CLOSE which 35 * will start tearing down the "window" which hosts the UI thread. This will 36 * cause a WM_DESTROY message to be received. Upon reception a WM_QUIT message 37 * is enqueued. When the message wait loop receives a WM_QUIT message it stops, 38 * thus allowing the thread to be joined. 39 * 40 * Note: that the only source of a WM_CLOSE should be PlatformUIThread::Stop. 41 * Note: because PlatformUIThread::Stop is called from a different thread than 42 * PlatformUIThread::Run, it is possible that Stop can race Run. 43 * 44 * After being stopped PlatformUIThread can not be started again. 45 * 46 */ 47 48 class PlatformUIThread { 49 public: 50 PlatformUIThread(std::function<void()> func, const char* name, 51 ThreadAttributes attributes) 52 : name_(name), 53 native_event_callback_(std::move(func)), 54 monitor_thread_(PlatformThread::SpawnJoinable([this]() { Run(); }, name, 55 attributes)) {} 56 57 virtual ~PlatformUIThread(); 58 59 void Stop(); 60 61 /** 62 * Request a recurring callback. 63 */ 64 bool RequestCallbackTimer(unsigned int milliseconds); 65 66 protected: 67 void Run(); 68 69 private: 70 static LRESULT CALLBACK EventWindowProc(HWND, UINT, WPARAM, LPARAM); 71 void NativeEventCallback(); 72 // Initialize the UI thread that is servicing the timer events 73 bool InternalInit(); 74 75 // Needs to be initialized before monitor_thread_ as it takes a string view to 76 // name_ 77 std::string name_; 78 RecursiveCriticalSection cs_; 79 std::function<void()> native_event_callback_; 80 webrtc::SequenceChecker thread_checker_; 81 PlatformThread monitor_thread_; 82 HWND hwnd_ MOZ_GUARDED_BY(cs_) = nullptr; 83 UINT_PTR timerid_ MOZ_GUARDED_BY(cs_) = 0; 84 unsigned int timeout_ MOZ_GUARDED_BY(cs_) = 0; 85 enum class State { 86 UNSTARTED, 87 STARTED, 88 STOPPED, 89 }; 90 State state_ MOZ_GUARDED_BY(cs_) = State::UNSTARTED; 91 }; 92 93 } // namespace webrtc 94 95 #endif 96 #endif // RTC_BASE_PLATFORM_UITHREAD_H_