blob: befd61849d7a857af436356fb28e257c83467fd4 [file] [log] [blame]
Tommibebc6902015-05-18 07:51:421/*
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 04:47:3111#ifndef RTC_BASE_PLATFORM_THREAD_H_
12#define RTC_BASE_PLATFORM_THREAD_H_
Tommibebc6902015-05-18 07:51:4213
Markus Handellad5037b2021-05-07 13:02:3614#include <functional>
Henrik Kjellanderec78f1c2017-06-29 05:52:5015#include <string>
Hans Wennborg1ee02d42023-03-13 12:04:0916#if !defined(WEBRTC_WIN)
17#include <pthread.h>
18#endif
pbos12411ef2015-11-23 22:47:5619
Danil Chapovalov5a1a6db2019-01-17 18:55:4620#include "absl/strings/string_view.h"
Markus Handellad5037b2021-05-07 13:02:3621#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "rtc_base/platform_thread_types.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5023
24namespace rtc {
25
Markus Handellad5037b2021-05-07 13:02:3626enum class ThreadPriority {
27 kLow = 1,
28 kNormal,
29 kHigh,
30 kRealtime,
Henrik Kjellanderec78f1c2017-06-29 05:52:5031};
32
Markus Handell97c44582021-04-20 15:41:5433struct ThreadAttributes {
Markus Handellad5037b2021-05-07 13:02:3634 ThreadPriority priority = ThreadPriority::kNormal;
Markus Handell97c44582021-04-20 15:41:5435 ThreadAttributes& SetPriority(ThreadPriority priority_param) {
36 priority = priority_param;
37 return *this;
38 }
Markus Handell97c44582021-04-20 15:41:5439};
40
Markus Handellad5037b2021-05-07 13:02:3641// Represents a simple worker thread.
42class PlatformThread final {
Henrik Kjellanderec78f1c2017-06-29 05:52:5043 public:
Markus Handellad5037b2021-05-07 13:02:3644 // Handle is the base platform thread handle.
45#if defined(WEBRTC_WIN)
46 using Handle = HANDLE;
47#else
48 using Handle = pthread_t;
49#endif // defined(WEBRTC_WIN)
50 // This ctor creates the PlatformThread with an unset handle (returning true
51 // in empty()) and is provided for convenience.
52 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
53 // removed.
54 PlatformThread() = default;
55
Artem Titov96e3b992021-07-26 14:03:1456 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3657 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
58 // removed.
59 PlatformThread(PlatformThread&& rhs);
60
Tommi145fdbf2022-04-08 10:41:2561 // Copies won't work since we'd have problems with joinable threads.
62 PlatformThread(const PlatformThread&) = delete;
63 PlatformThread& operator=(const PlatformThread&) = delete;
64
Artem Titov96e3b992021-07-26 14:03:1465 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3666 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
67 // removed.
68 PlatformThread& operator=(PlatformThread&& rhs);
69
70 // For a PlatformThread that's been spawned joinable, the destructor suspends
71 // the calling thread until the created thread exits unless the thread has
72 // already exited.
Henrik Kjellanderec78f1c2017-06-29 05:52:5073 virtual ~PlatformThread();
74
Markus Handellad5037b2021-05-07 13:02:3675 // Finalizes any allocated resources.
76 // For a PlatformThread that's been spawned joinable, Finalize() suspends
77 // the calling thread until the created thread exits unless the thread has
78 // already exited.
79 // empty() returns true after completion.
80 void Finalize();
Henrik Kjellanderec78f1c2017-06-29 05:52:5081
Markus Handellad5037b2021-05-07 13:02:3682 // Returns true if default constructed, moved from, or Finalize()ed.
83 bool empty() const { return !handle_.has_value(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5084
Markus Handellad5037b2021-05-07 13:02:3685 // Creates a started joinable thread which will be joined when the returned
86 // PlatformThread destructs or Finalize() is called.
87 static PlatformThread SpawnJoinable(
88 std::function<void()> thread_function,
89 absl::string_view name,
90 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 05:52:5091
Markus Handellad5037b2021-05-07 13:02:3692 // Creates a started detached thread. The caller has to use external
93 // synchronization as nothing is provided by the PlatformThread construct.
94 static PlatformThread SpawnDetached(
95 std::function<void()> thread_function,
96 absl::string_view name,
97 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 05:52:5098
Markus Handellad5037b2021-05-07 13:02:3699 // Returns the base platform thread handle of this thread.
100 absl::optional<Handle> GetHandle() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:50101
Henrik Kjellanderec78f1c2017-06-29 05:52:50102#if defined(WEBRTC_WIN)
Markus Handellad5037b2021-05-07 13:02:36103 // Queue a Windows APC function that runs when the thread is alertable.
Henrik Kjellanderec78f1c2017-06-29 05:52:50104 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
105#endif
106
107 private:
Markus Handellad5037b2021-05-07 13:02:36108 PlatformThread(Handle handle, bool joinable);
109 static PlatformThread SpawnThread(std::function<void()> thread_function,
110 absl::string_view name,
111 ThreadAttributes attributes,
112 bool joinable);
113
114 absl::optional<Handle> handle_;
115 bool joinable_ = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:50116};
117
118} // namespace rtc
pbos12411ef2015-11-23 22:47:56119
Mirko Bonadei92ea95e2017-09-15 04:47:31120#endif // RTC_BASE_PLATFORM_THREAD_H_