blob: 53465e4b17a2f06334cefc17d91add8bd23c9019 [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
Peter Boström8c38e8b2015-11-26 17:45:47 +010062 // Spawns a thread and tries to set thread priority according to the priority
63 // from when CreateThread was called.
64 void Start();
pbos12411ef2015-11-23 14:47:56 -080065
Peter Boström8c38e8b2015-11-26 17:45:47 +010066 bool IsRunning() const;
pbos12411ef2015-11-23 14:47:56 -080067
Peter Boström8c38e8b2015-11-26 17:45:47 +010068 // Stops (joins) the spawned thread.
69 void Stop();
pbos12411ef2015-11-23 14:47:56 -080070
Peter Boström8c38e8b2015-11-26 17:45:47 +010071 // Set the priority of the thread. Must be called when thread is running.
pbos12411ef2015-11-23 14:47:56 -080072 bool SetPriority(ThreadPriority priority);
73
74 private:
75 void Run();
76
77 ThreadRunFunction const run_function_;
78 void* const obj_;
79 // TODO(pbos): Make sure call sites use string literals and update to a const
80 // char* instead of a std::string.
81 const std::string name_;
82 rtc::ThreadChecker thread_checker_;
83#if defined(WEBRTC_WIN)
84 static DWORD WINAPI StartThread(void* param);
85
86 bool stop_;
87 HANDLE thread_;
88#else
89 static void* StartThread(void* param);
90
91 rtc::Event stop_event_;
92
93 pthread_t thread_;
94#endif // defined(WEBRTC_WIN)
95 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
96};
97
Peter Boström8c38e8b2015-11-26 17:45:47 +010098} // namespace rtc
pbos12411ef2015-11-23 14:47:56 -080099
Tommibebc6902015-05-18 09:51:42 +0200100#endif // WEBRTC_BASE_PLATFORM_THREAD_H_