blob: 79ea7c15a6158ce4e8fa12eb1711178c73cb3846 [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
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/platform_thread_types.h"
21#include "rtc_base/thread_checker.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025// Callback function that the spawned thread will enter once spawned.
26// A return value of false is interpreted as that the function has no
27// more work to do and that the thread can be released.
28typedef bool (*ThreadRunFunctionDeprecated)(void*);
29typedef void (*ThreadRunFunction)(void*);
30
31enum ThreadPriority {
32#ifdef WEBRTC_WIN
33 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
34 kNormalPriority = THREAD_PRIORITY_NORMAL,
35 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
36 kHighestPriority = THREAD_PRIORITY_HIGHEST,
37 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
38#else
39 kLowPriority = 1,
40 kNormalPriority = 2,
41 kHighPriority = 3,
42 kHighestPriority = 4,
43 kRealtimePriority = 5
44#endif
45};
46
47// Represents a simple worker thread. The implementation must be assumed
48// to be single threaded, meaning that all methods of the class, must be
49// called from the same thread, including instantiation.
50class PlatformThread {
51 public:
52 PlatformThread(ThreadRunFunctionDeprecated func,
53 void* obj,
54 const char* thread_name);
55 PlatformThread(ThreadRunFunction func,
56 void* obj,
57 const char* thread_name,
58 ThreadPriority priority = kNormalPriority);
59 virtual ~PlatformThread();
60
61 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 void Stop();
75
76 // Set the priority of the thread. Must be called when thread is running.
77 // TODO(tommi): Make private and only allow public support via ctor.
78 bool SetPriority(ThreadPriority priority);
79
80 protected:
81#if defined(WEBRTC_WIN)
82 // Exposed to derived classes to allow for special cases specific to Windows.
83 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
84#endif
85
86 private:
87 void Run();
88
89 ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr;
90 ThreadRunFunction const run_function_ = nullptr;
91 const ThreadPriority priority_ = kNormalPriority;
92 void* const obj_;
93 // TODO(pbos): Make sure call sites use string literals and update to a const
94 // char* instead of a std::string.
95 const std::string name_;
96 rtc::ThreadChecker thread_checker_;
97 rtc::ThreadChecker spawned_thread_checker_;
98#if defined(WEBRTC_WIN)
99 static DWORD WINAPI StartThread(void* param);
100
101 bool stop_ = false;
102 HANDLE thread_ = nullptr;
103 DWORD thread_id_ = 0;
104#else
105 static void* StartThread(void* param);
106
107 // An atomic flag that we use to stop the thread. Only modified on the
108 // controlling thread and checked on the worker thread.
109 volatile int stop_flag_ = 0;
110 pthread_t thread_ = 0;
111#endif // defined(WEBRTC_WIN)
112 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
113};
114
115} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800116
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // RTC_BASE_PLATFORM_THREAD_H_