Line data Source code
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 WEBRTC_BASE_PLATFORM_THREAD_H_
12 : #define WEBRTC_BASE_PLATFORM_THREAD_H_
13 :
14 : #include <string>
15 :
16 : #include "webrtc/base/constructormagic.h"
17 : #include "webrtc/base/event.h"
18 : #include "webrtc/base/platform_thread_types.h"
19 : #include "webrtc/base/thread_checker.h"
20 :
21 : namespace rtc {
22 :
23 : PlatformThreadId CurrentThreadId();
24 : PlatformThreadRef CurrentThreadRef();
25 :
26 : // Compares two thread identifiers for equality.
27 : bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
28 :
29 : // Sets the current thread name.
30 : void SetCurrentThreadName(const char* name);
31 :
32 : // Callback function that the spawned thread will enter once spawned.
33 : // A return value of false is interpreted as that the function has no
34 : // more work to do and that the thread can be released.
35 : typedef bool (*ThreadRunFunction)(void*);
36 :
37 : enum ThreadPriority {
38 : #ifdef WEBRTC_WIN
39 : kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
40 : kNormalPriority = THREAD_PRIORITY_NORMAL,
41 : kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
42 : kHighestPriority = THREAD_PRIORITY_HIGHEST,
43 : kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
44 : #else
45 : kLowPriority = 1,
46 : kNormalPriority = 2,
47 : kHighPriority = 3,
48 : kHighestPriority = 4,
49 : kRealtimePriority = 5
50 : #endif
51 : };
52 :
53 : // Represents a simple worker thread. The implementation must be assumed
54 : // to be single threaded, meaning that all methods of the class, must be
55 : // called from the same thread, including instantiation.
56 : class PlatformThread {
57 : public:
58 : PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name);
59 : virtual ~PlatformThread();
60 :
61 0 : const std::string& name() const { return name_; }
62 :
63 : // Spawns a thread and tries to set thread priority according to the priority
64 : // from when CreateThread was called.
65 : void Start();
66 :
67 : bool IsRunning() const;
68 :
69 : // Returns an identifier for the worker thread that can be used to do
70 : // thread checks.
71 : PlatformThreadRef GetThreadRef() const;
72 :
73 : // Stops (joins) the spawned thread.
74 : virtual void Stop();
75 :
76 : // Set the priority of the thread. Must be called when thread is running.
77 : bool SetPriority(ThreadPriority priority);
78 :
79 : protected:
80 : #if defined(WEBRTC_WIN)
81 : // Exposed to derived classes to allow for special cases specific to Windows.
82 : bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
83 : #endif
84 :
85 : virtual void Run();
86 :
87 : ThreadRunFunction const run_function_;
88 : void* const obj_;
89 : // TODO(pbos): Make sure call sites use string literals and update to a const
90 : // char* instead of a std::string.
91 : const std::string name_;
92 : rtc::ThreadChecker thread_checker_;
93 : #if defined(WEBRTC_WIN)
94 : static DWORD WINAPI StartThread(void* param);
95 :
96 : bool stop_;
97 : HANDLE thread_;
98 : DWORD thread_id_;
99 : #else
100 : static void* StartThread(void* param);
101 :
102 : rtc::Event stop_event_;
103 :
104 : pthread_t thread_;
105 : #endif // defined(WEBRTC_WIN)
106 : RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
107 : };
108 :
109 : #if defined(WEBRTC_WIN)
110 : class PlatformUIThread : public PlatformThread {
111 : public:
112 : PlatformUIThread(ThreadRunFunction func, void* obj,
113 : const char* thread_name) :
114 : PlatformThread(func, obj, thread_name),
115 : hwnd_(nullptr),
116 : timerid_(0),
117 : timeout_(0) {
118 : }
119 : virtual ~PlatformUIThread() {}
120 :
121 : virtual void Stop() override;
122 :
123 : /**
124 : * Request an async callback soon.
125 : */
126 : void RequestCallback();
127 :
128 : /**
129 : * Request a recurring callback.
130 : */
131 : bool RequestCallbackTimer(unsigned int milliseconds);
132 :
133 : protected:
134 : virtual void Run() override;
135 :
136 : private:
137 : static LRESULT CALLBACK EventWindowProc(HWND, UINT, WPARAM, LPARAM);
138 : void NativeEventCallback();
139 : bool InternalInit();
140 :
141 : HWND hwnd_;
142 : UINT_PTR timerid_;
143 : unsigned int timeout_;
144 : };
145 : #endif
146 :
147 : } // namespace rtc
148 :
149 : #endif // WEBRTC_BASE_PLATFORM_THREAD_H_
|