blob: 35c0e274322cdffc3d15302dcd284e33225e6000 [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"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/platform_thread_types.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.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027typedef 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
Markus Handell97c44582021-04-20 17:41:54 +020045struct ThreadAttributes {
46 ThreadPriority priority = kNormalPriority;
47 bool joinable = true;
48
49 ThreadAttributes& SetPriority(ThreadPriority priority_param) {
50 priority = priority_param;
51 return *this;
52 }
53 ThreadAttributes& SetDetached() {
54 joinable = false;
55 return *this;
56 }
57};
58
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059// Represents a simple worker thread. The implementation must be assumed
60// to be single threaded, meaning that all methods of the class, must be
61// called from the same thread, including instantiation.
62class PlatformThread {
63 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064 PlatformThread(ThreadRunFunction func,
65 void* obj,
Danil Chapovalov5a1a6db2019-01-17 19:55:46 +010066 absl::string_view thread_name,
Markus Handell97c44582021-04-20 17:41:54 +020067 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 virtual ~PlatformThread();
69
70 const std::string& name() const { return name_; }
71
72 // Spawns a thread and tries to set thread priority according to the priority
73 // from when CreateThread was called.
Markus Handell97c44582021-04-20 17:41:54 +020074 // Start can only be called after the constructor or after a call to Stop().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075 void Start();
76
77 bool IsRunning() const;
78
79 // Returns an identifier for the worker thread that can be used to do
80 // thread checks.
81 PlatformThreadRef GetThreadRef() const;
82
Markus Handell97c44582021-04-20 17:41:54 +020083 // Stop() prepares the PlatformThread for destruction or another call to
84 // Start(). For a PlatformThread that's been created with
85 // ThreadAttributes::joinable true (the default), Stop() suspends the calling
86 // thread until the created thread exits unless the thread has already exited.
87 // Stop() can only be called after calling Start().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 void Stop();
89
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 protected:
91#if defined(WEBRTC_WIN)
92 // Exposed to derived classes to allow for special cases specific to Windows.
93 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
94#endif
95
96 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 ThreadRunFunction const run_function_ = nullptr;
Markus Handell97c44582021-04-20 17:41:54 +020098 const ThreadAttributes attributes_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099 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_;
Artem Titovc8421c42021-02-02 10:57:19 +0100103 webrtc::SequenceChecker thread_checker_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104#if defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105 HANDLE thread_ = nullptr;
106 DWORD thread_id_ = 0;
107#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108 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_