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