blob: ed26ca03cc70d1e2d05814b05733c02d34241de8 [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
11#ifndef WEBRTC_BASE_PLATFORM_THREAD_H_
12#define WEBRTC_BASE_PLATFORM_THREAD_H_
13
pbos12411ef2015-11-23 14:47:56 -080014#include <string>
15
16#include "webrtc/base/constructormagic.h"
17#include "webrtc/base/event.h"
18#include "webrtc/base/platform_thread_types.h"
pbos12411ef2015-11-23 14:47:56 -080019#include "webrtc/base/thread_checker.h"
Tommibebc6902015-05-18 09:51:42 +020020
21namespace rtc {
22
Tommibebc6902015-05-18 09:51:42 +020023PlatformThreadId CurrentThreadId();
24PlatformThreadRef CurrentThreadRef();
25
26// Compares two thread identifiers for equality.
27bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
28
Tommiea14f0a2015-05-18 13:51:06 +020029// Sets the current thread name.
30void SetCurrentThreadName(const char* name);
31
pbos12411ef2015-11-23 14:47:56 -080032// 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.
tommi0f8b4032017-02-22 11:22:05 -080035typedef bool (*ThreadRunFunctionDeprecated)(void*);
36typedef void (*ThreadRunFunction)(void*);
pbos12411ef2015-11-23 14:47:56 -080037
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.
pbos12411ef2015-11-23 14:47:56 -080057class PlatformThread {
58 public:
tommi0f8b4032017-02-22 11:22:05 -080059 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);
pbos12411ef2015-11-23 14:47:56 -080066 virtual ~PlatformThread();
67
tommi845afa82016-04-22 09:08:44 -070068 const std::string& name() const { return name_; }
69
Peter Boström8c38e8b2015-11-26 17:45:47 +010070 // Spawns a thread and tries to set thread priority according to the priority
71 // from when CreateThread was called.
72 void Start();
pbos12411ef2015-11-23 14:47:56 -080073
Peter Boström8c38e8b2015-11-26 17:45:47 +010074 bool IsRunning() const;
pbos12411ef2015-11-23 14:47:56 -080075
tommi845afa82016-04-22 09:08:44 -070076 // Returns an identifier for the worker thread that can be used to do
77 // thread checks.
78 PlatformThreadRef GetThreadRef() const;
79
Peter Boström8c38e8b2015-11-26 17:45:47 +010080 // Stops (joins) the spawned thread.
81 void Stop();
pbos12411ef2015-11-23 14:47:56 -080082
Peter Boström8c38e8b2015-11-26 17:45:47 +010083 // Set the priority of the thread. Must be called when thread is running.
tommi0f8b4032017-02-22 11:22:05 -080084 // TODO(tommi): Make private and only allow public support via ctor.
pbos12411ef2015-11-23 14:47:56 -080085 bool SetPriority(ThreadPriority priority);
86
tommi845afa82016-04-22 09:08:44 -070087 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
pbos12411ef2015-11-23 14:47:56 -080093 private:
94 void Run();
95
tommi0f8b4032017-02-22 11:22:05 -080096 ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr;
97 ThreadRunFunction const run_function_ = nullptr;
98 const ThreadPriority priority_ = kNormalPriority;
pbos12411ef2015-11-23 14:47:56 -080099 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_;
tommi0f8b4032017-02-22 11:22:05 -0800104 rtc::ThreadChecker spawned_thread_checker_;
pbos12411ef2015-11-23 14:47:56 -0800105#if defined(WEBRTC_WIN)
106 static DWORD WINAPI StartThread(void* param);
107
tommi82ead602017-02-19 16:09:55 -0800108 bool stop_ = false;
109 HANDLE thread_ = nullptr;
110 DWORD thread_id_ = 0;
pbos12411ef2015-11-23 14:47:56 -0800111#else
112 static void* StartThread(void* param);
113
tommi82ead602017-02-19 16:09:55 -0800114 // 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;
pbos12411ef2015-11-23 14:47:56 -0800118#endif // defined(WEBRTC_WIN)
119 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
120};
121
Peter Boström8c38e8b2015-11-26 17:45:47 +0100122} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800123
Tommibebc6902015-05-18 09:51:42 +0200124#endif // WEBRTC_BASE_PLATFORM_THREAD_H_