blob: 33921c209da0289b0747daa47b1545bca5446d54 [file] [log] [blame]
Tommibebc6902015-05-18 09:51:42 +02001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_PLATFORM_THREAD_H_
12#define RTC_BASE_PLATFORM_THREAD_H_
Tommibebc6902015-05-18 09:51:42 +020013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
pbos12411ef2015-11-23 14:47:56 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/constructormagic.h"
17#include "rtc_base/event.h"
18#include "rtc_base/platform_thread_types.h"
19#include "rtc_base/thread_checker.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023// Callback function that the spawned thread will enter once spawned.
24// A return value of false is interpreted as that the function has no
25// more work to do and that the thread can be released.
26typedef bool (*ThreadRunFunctionDeprecated)(void*);
27typedef void (*ThreadRunFunction)(void*);
28
29enum ThreadPriority {
30#ifdef WEBRTC_WIN
31 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
32 kNormalPriority = THREAD_PRIORITY_NORMAL,
33 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
34 kHighestPriority = THREAD_PRIORITY_HIGHEST,
35 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
36#else
37 kLowPriority = 1,
38 kNormalPriority = 2,
39 kHighPriority = 3,
40 kHighestPriority = 4,
41 kRealtimePriority = 5
42#endif
43};
44
45// Represents a simple worker thread. The implementation must be assumed
46// to be single threaded, meaning that all methods of the class, must be
47// called from the same thread, including instantiation.
48class PlatformThread {
49 public:
50 PlatformThread(ThreadRunFunctionDeprecated func,
51 void* obj,
52 const char* thread_name);
53 PlatformThread(ThreadRunFunction func,
54 void* obj,
55 const char* thread_name,
56 ThreadPriority priority = kNormalPriority);
57 virtual ~PlatformThread();
58
59 const std::string& name() const { return name_; }
60
61 // Spawns a thread and tries to set thread priority according to the priority
62 // from when CreateThread was called.
63 void Start();
64
65 bool IsRunning() const;
66
67 // Returns an identifier for the worker thread that can be used to do
68 // thread checks.
69 PlatformThreadRef GetThreadRef() const;
70
71 // Stops (joins) the spawned thread.
72 void Stop();
73
74 // Set the priority of the thread. Must be called when thread is running.
75 // TODO(tommi): Make private and only allow public support via ctor.
76 bool SetPriority(ThreadPriority priority);
77
78 protected:
79#if defined(WEBRTC_WIN)
80 // Exposed to derived classes to allow for special cases specific to Windows.
81 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
82#endif
83
84 private:
85 void Run();
86
87 ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr;
88 ThreadRunFunction const run_function_ = nullptr;
89 const ThreadPriority priority_ = kNormalPriority;
90 void* const obj_;
91 // TODO(pbos): Make sure call sites use string literals and update to a const
92 // char* instead of a std::string.
93 const std::string name_;
94 rtc::ThreadChecker thread_checker_;
95 rtc::ThreadChecker spawned_thread_checker_;
96#if defined(WEBRTC_WIN)
97 static DWORD WINAPI StartThread(void* param);
98
99 bool stop_ = false;
100 HANDLE thread_ = nullptr;
101 DWORD thread_id_ = 0;
102#else
103 static void* StartThread(void* param);
104
105 // An atomic flag that we use to stop the thread. Only modified on the
106 // controlling thread and checked on the worker thread.
107 volatile int stop_flag_ = 0;
108 pthread_t thread_ = 0;
109#endif // defined(WEBRTC_WIN)
110 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
111};
112
113} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800114
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200115#endif // RTC_BASE_PLATFORM_THREAD_H_