blob: 8c2161baa891c4018c8ca3b18995349a25e9752f [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"
19#include "webrtc/base/scoped_ptr.h"
20#include "webrtc/base/thread_checker.h"
Tommibebc6902015-05-18 09:51:42 +020021
22namespace rtc {
23
Tommibebc6902015-05-18 09:51:42 +020024PlatformThreadId CurrentThreadId();
25PlatformThreadRef CurrentThreadRef();
26
27// Compares two thread identifiers for equality.
28bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
29
Tommiea14f0a2015-05-18 13:51:06 +020030// Sets the current thread name.
31void SetCurrentThreadName(const char* name);
32
pbos12411ef2015-11-23 14:47:56 -080033// Callback function that the spawned thread will enter once spawned.
34// A return value of false is interpreted as that the function has no
35// more work to do and that the thread can be released.
36typedef bool (*ThreadRunFunction)(void*);
37
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:
59 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name);
60 virtual ~PlatformThread();
61
tommi845afa82016-04-22 09:08:44 -070062 const std::string& name() const { return name_; }
63
Peter Boström8c38e8b2015-11-26 17:45:47 +010064 // Spawns a thread and tries to set thread priority according to the priority
65 // from when CreateThread was called.
66 void Start();
pbos12411ef2015-11-23 14:47:56 -080067
Peter Boström8c38e8b2015-11-26 17:45:47 +010068 bool IsRunning() const;
pbos12411ef2015-11-23 14:47:56 -080069
tommi845afa82016-04-22 09:08:44 -070070 // Returns an identifier for the worker thread that can be used to do
71 // thread checks.
72 PlatformThreadRef GetThreadRef() const;
73
Peter Boström8c38e8b2015-11-26 17:45:47 +010074 // Stops (joins) the spawned thread.
75 void Stop();
pbos12411ef2015-11-23 14:47:56 -080076
Peter Boström8c38e8b2015-11-26 17:45:47 +010077 // Set the priority of the thread. Must be called when thread is running.
pbos12411ef2015-11-23 14:47:56 -080078 bool SetPriority(ThreadPriority priority);
79
tommi845afa82016-04-22 09:08:44 -070080 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
pbos12411ef2015-11-23 14:47:56 -080086 private:
87 void Run();
88
89 ThreadRunFunction const run_function_;
90 void* const obj_;
91 // TODO(pbos): Make sure call sites use string literals and update to a const
92 // char* instead of a std::string.
93 const std::string name_;
94 rtc::ThreadChecker thread_checker_;
95#if defined(WEBRTC_WIN)
96 static DWORD WINAPI StartThread(void* param);
97
98 bool stop_;
99 HANDLE thread_;
tommi845afa82016-04-22 09:08:44 -0700100 DWORD thread_id_;
pbos12411ef2015-11-23 14:47:56 -0800101#else
102 static void* StartThread(void* param);
103
104 rtc::Event stop_event_;
105
106 pthread_t thread_;
107#endif // defined(WEBRTC_WIN)
108 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
109};
110
Peter Boström8c38e8b2015-11-26 17:45:47 +0100111} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -0800112
Tommibebc6902015-05-18 09:51:42 +0200113#endif // WEBRTC_BASE_PLATFORM_THREAD_H_