blob: 25fa036908573f0e35e2d9a6bf9e3e091fa1bed7 [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
23PlatformThreadId CurrentThreadId();
24PlatformThreadRef CurrentThreadRef();
25
26// Compares two thread identifiers for equality.
27bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
28
29// Sets the current thread name.
30void 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.
35typedef bool (*ThreadRunFunctionDeprecated)(void*);
36typedef void (*ThreadRunFunction)(void*);
37
38enum ThreadPriority {
39#ifdef WEBRTC_WIN
40 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
41 kNormalPriority = THREAD_PRIORITY_NORMAL,
42 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
43 kHighestPriority = THREAD_PRIORITY_HIGHEST,
44 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
45#else
46 kLowPriority = 1,
47 kNormalPriority = 2,
48 kHighPriority = 3,
49 kHighestPriority = 4,
50 kRealtimePriority = 5
51#endif
52};
53
54// Represents a simple worker thread. The implementation must be assumed
55// to be single threaded, meaning that all methods of the class, must be
56// called from the same thread, including instantiation.
57class PlatformThread {
58 public:
59 PlatformThread(ThreadRunFunctionDeprecated func,
60 void* obj,
61 const char* thread_name);
62 PlatformThread(ThreadRunFunction func,
63 void* obj,
64 const char* thread_name,
65 ThreadPriority priority = kNormalPriority);
66 virtual ~PlatformThread();
67
68 const std::string& name() const { return name_; }
69
70 // Spawns a thread and tries to set thread priority according to the priority
71 // from when CreateThread was called.
72 void Start();
73
74 bool IsRunning() const;
75
76 // Returns an identifier for the worker thread that can be used to do
77 // thread checks.
78 PlatformThreadRef GetThreadRef() const;
79
80 // Stops (joins) the spawned thread.
81 void Stop();
82
83 // Set the priority of the thread. Must be called when thread is running.
84 // TODO(tommi): Make private and only allow public support via ctor.
85 bool SetPriority(ThreadPriority priority);
86
87 protected:
88#if defined(WEBRTC_WIN)
89 // Exposed to derived classes to allow for special cases specific to Windows.
90 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
91#endif
92
93 private:
94 void Run();
95
96 ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr;
97 ThreadRunFunction const run_function_ = nullptr;
98 const ThreadPriority priority_ = kNormalPriority;
99 void* const obj_;
100 // TODO(pbos): Make sure call sites use string literals and update to a const
101 // char* instead of a std::string.
102 const std::string name_;
103 rtc::ThreadChecker thread_checker_;
104 rtc::ThreadChecker spawned_thread_checker_;
105#if defined(WEBRTC_WIN)
106 static DWORD WINAPI StartThread(void* param);
107
108 bool stop_ = false;
109 HANDLE thread_ = nullptr;
110 DWORD thread_id_ = 0;
111#else
112 static void* StartThread(void* param);
113
114 // An atomic flag that we use to stop the thread. Only modified on the
115 // controlling thread and checked on the worker thread.
116 volatile int stop_flag_ = 0;
117 pthread_t thread_ = 0;
118#endif // defined(WEBRTC_WIN)
119 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
120};
121
122} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // RTC_BASE_PLATFORM_THREAD_H_