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