blob: d74aec2811410e9533eaab8bcf86d854d98feab5 [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.
35typedef bool (*ThreadRunFunction)(void*);
36
37enum ThreadPriority {
38#ifdef WEBRTC_WIN
39 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
40 kNormalPriority = THREAD_PRIORITY_NORMAL,
41 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
42 kHighestPriority = THREAD_PRIORITY_HIGHEST,
43 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
44#else
45 kLowPriority = 1,
46 kNormalPriority = 2,
47 kHighPriority = 3,
48 kHighestPriority = 4,
49 kRealtimePriority = 5
50#endif
51};
52
53// Represents a simple worker thread. The implementation must be assumed
54// to be single threaded, meaning that all methods of the class, must be
55// called from the same thread, including instantiation.
pbos12411ef2015-11-23 14:47:56 -080056class PlatformThread {
57 public:
58 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name);
59 virtual ~PlatformThread();
60
tommi845afa82016-04-22 09:08:44 -070061 const std::string& name() const { return name_; }
62
Peter Boström8c38e8b2015-11-26 17:45:47 +010063 // Spawns a thread and tries to set thread priority according to the priority
64 // from when CreateThread was called.
65 void Start();
pbos12411ef2015-11-23 14:47:56 -080066
Peter Boström8c38e8b2015-11-26 17:45:47 +010067 bool IsRunning() const;
pbos12411ef2015-11-23 14:47:56 -080068
tommi845afa82016-04-22 09:08:44 -070069 // Returns an identifier for the worker thread that can be used to do
70 // thread checks.
71 PlatformThreadRef GetThreadRef() const;
72
Peter Boström8c38e8b2015-11-26 17:45:47 +010073 // Stops (joins) the spawned thread.
74 void Stop();
pbos12411ef2015-11-23 14:47:56 -080075
Peter Boström8c38e8b2015-11-26 17:45:47 +010076 // Set the priority of the thread. Must be called when thread is running.
pbos12411ef2015-11-23 14:47:56 -080077 bool SetPriority(ThreadPriority priority);
78
tommi845afa82016-04-22 09:08:44 -070079 protected:
80#if defined(WEBRTC_WIN)
81 // Exposed to derived classes to allow for special cases specific to Windows.
82 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
83#endif
84
pbos12411ef2015-11-23 14:47:56 -080085 private:
86 void Run();
87
88 ThreadRunFunction const run_function_;
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#if defined(WEBRTC_WIN)
95 static DWORD WINAPI StartThread(void* param);
96
97 bool stop_;
98 HANDLE thread_;
tommi845afa82016-04-22 09:08:44 -070099 DWORD thread_id_;
pbos12411ef2015-11-23 14:47:56 -0800100#else
101 static void* StartThread(void* param);
102
103 rtc::Event stop_event_;
104
105 pthread_t thread_;
106#endif // defined(WEBRTC_WIN)
107 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
108};
109
Peter Boström8c38e8b2015-11-26 17:45:47 +0100110} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800111
Tommibebc6902015-05-18 09:51:42 +0200112#endif // WEBRTC_BASE_PLATFORM_THREAD_H_