blob: cfc9d17af4fff5c3bbf31d4a5ec14d234ce4e8e6 [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
Florent Castelli8037fc62024-08-29 13:00:4020#include <optional>
21
Danil Chapovalov5a1a6db2019-01-17 18:55:4622#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/platform_thread_types.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5024
25namespace rtc {
26
Markus Handellad5037b2021-05-07 13:02:3627enum class ThreadPriority {
28 kLow = 1,
29 kNormal,
30 kHigh,
31 kRealtime,
Henrik Kjellanderec78f1c2017-06-29 05:52:5032};
33
Markus Handell97c44582021-04-20 15:41:5434struct ThreadAttributes {
Markus Handellad5037b2021-05-07 13:02:3635 ThreadPriority priority = ThreadPriority::kNormal;
Markus Handell97c44582021-04-20 15:41:5436 ThreadAttributes& SetPriority(ThreadPriority priority_param) {
37 priority = priority_param;
38 return *this;
39 }
Markus Handell97c44582021-04-20 15:41:5440};
41
Markus Handellad5037b2021-05-07 13:02:3642// Represents a simple worker thread.
43class PlatformThread final {
Henrik Kjellanderec78f1c2017-06-29 05:52:5044 public:
Markus Handellad5037b2021-05-07 13:02:3645 // Handle is the base platform thread handle.
46#if defined(WEBRTC_WIN)
47 using Handle = HANDLE;
48#else
49 using Handle = pthread_t;
50#endif // defined(WEBRTC_WIN)
51 // This ctor creates the PlatformThread with an unset handle (returning true
52 // in empty()) and is provided for convenience.
53 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
54 // removed.
55 PlatformThread() = default;
56
Artem Titov96e3b992021-07-26 14:03:1457 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3658 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
59 // removed.
60 PlatformThread(PlatformThread&& rhs);
61
Tommi145fdbf2022-04-08 10:41:2562 // Copies won't work since we'd have problems with joinable threads.
63 PlatformThread(const PlatformThread&) = delete;
64 PlatformThread& operator=(const PlatformThread&) = delete;
65
Artem Titov96e3b992021-07-26 14:03:1466 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3667 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
68 // removed.
69 PlatformThread& operator=(PlatformThread&& rhs);
70
71 // For a PlatformThread that's been spawned joinable, the destructor suspends
72 // the calling thread until the created thread exits unless the thread has
73 // already exited.
Henrik Kjellanderec78f1c2017-06-29 05:52:5074 virtual ~PlatformThread();
75
Markus Handellad5037b2021-05-07 13:02:3676 // Finalizes any allocated resources.
77 // For a PlatformThread that's been spawned joinable, Finalize() suspends
78 // the calling thread until the created thread exits unless the thread has
79 // already exited.
80 // empty() returns true after completion.
81 void Finalize();
Henrik Kjellanderec78f1c2017-06-29 05:52:5082
Markus Handellad5037b2021-05-07 13:02:3683 // Returns true if default constructed, moved from, or Finalize()ed.
84 bool empty() const { return !handle_.has_value(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5085
Markus Handellad5037b2021-05-07 13:02:3686 // Creates a started joinable thread which will be joined when the returned
87 // PlatformThread destructs or Finalize() is called.
88 static PlatformThread SpawnJoinable(
89 std::function<void()> thread_function,
90 absl::string_view name,
91 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 05:52:5092
Markus Handellad5037b2021-05-07 13:02:3693 // Creates a started detached thread. The caller has to use external
94 // synchronization as nothing is provided by the PlatformThread construct.
95 static PlatformThread SpawnDetached(
96 std::function<void()> thread_function,
97 absl::string_view name,
98 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 05:52:5099
Markus Handellad5037b2021-05-07 13:02:36100 // Returns the base platform thread handle of this thread.
Florent Castelli8037fc62024-08-29 13:00:40101 std::optional<Handle> GetHandle() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:50102
Henrik Kjellanderec78f1c2017-06-29 05:52:50103#if defined(WEBRTC_WIN)
Markus Handellad5037b2021-05-07 13:02:36104 // Queue a Windows APC function that runs when the thread is alertable.
Henrik Kjellanderec78f1c2017-06-29 05:52:50105 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
106#endif
107
108 private:
Markus Handellad5037b2021-05-07 13:02:36109 PlatformThread(Handle handle, bool joinable);
110 static PlatformThread SpawnThread(std::function<void()> thread_function,
111 absl::string_view name,
112 ThreadAttributes attributes,
113 bool joinable);
114
Florent Castelli8037fc62024-08-29 13:00:40115 std::optional<Handle> handle_;
Markus Handellad5037b2021-05-07 13:02:36116 bool joinable_ = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:50117};
118
119} // namespace rtc
pbos12411ef2015-11-23 22:47:56120
Mirko Bonadei92ea95e2017-09-15 04:47:31121#endif // RTC_BASE_PLATFORM_THREAD_H_