blob: 2c82c024550ff18a3d1c6f4739b080aca2b8c7c7 [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>
pbos12411ef2015-11-23 22:47:5616
Danil Chapovalov5a1a6db2019-01-17 18:55:4617#include "absl/strings/string_view.h"
Markus Handellad5037b2021-05-07 13:02:3618#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/platform_thread_types.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5020
21namespace rtc {
22
Markus Handellad5037b2021-05-07 13:02:3623enum class ThreadPriority {
24 kLow = 1,
25 kNormal,
26 kHigh,
27 kRealtime,
Henrik Kjellanderec78f1c2017-06-29 05:52:5028};
29
Markus Handell97c44582021-04-20 15:41:5430struct ThreadAttributes {
Markus Handellad5037b2021-05-07 13:02:3631 ThreadPriority priority = ThreadPriority::kNormal;
Markus Handell97c44582021-04-20 15:41:5432 ThreadAttributes& SetPriority(ThreadPriority priority_param) {
33 priority = priority_param;
34 return *this;
35 }
Markus Handell97c44582021-04-20 15:41:5436};
37
Markus Handellad5037b2021-05-07 13:02:3638// Represents a simple worker thread.
39class PlatformThread final {
Henrik Kjellanderec78f1c2017-06-29 05:52:5040 public:
Markus Handellad5037b2021-05-07 13:02:3641 // Handle is the base platform thread handle.
42#if defined(WEBRTC_WIN)
43 using Handle = HANDLE;
44#else
45 using Handle = pthread_t;
46#endif // defined(WEBRTC_WIN)
47 // This ctor creates the PlatformThread with an unset handle (returning true
48 // in empty()) and is provided for convenience.
49 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
50 // removed.
51 PlatformThread() = default;
52
Artem Titov96e3b992021-07-26 14:03:1453 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3654 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
55 // removed.
56 PlatformThread(PlatformThread&& rhs);
57
Artem Titov96e3b992021-07-26 14:03:1458 // Moves `rhs` into this, storing an empty state in `rhs`.
Markus Handellad5037b2021-05-07 13:02:3659 // TODO(bugs.webrtc.org/12727) Look into if default and move support can be
60 // removed.
61 PlatformThread& operator=(PlatformThread&& rhs);
62
63 // For a PlatformThread that's been spawned joinable, the destructor suspends
64 // the calling thread until the created thread exits unless the thread has
65 // already exited.
Henrik Kjellanderec78f1c2017-06-29 05:52:5066 virtual ~PlatformThread();
67
Markus Handellad5037b2021-05-07 13:02:3668 // Finalizes any allocated resources.
69 // For a PlatformThread that's been spawned joinable, Finalize() suspends
70 // the calling thread until the created thread exits unless the thread has
71 // already exited.
72 // empty() returns true after completion.
73 void Finalize();
Henrik Kjellanderec78f1c2017-06-29 05:52:5074
Markus Handellad5037b2021-05-07 13:02:3675 // Returns true if default constructed, moved from, or Finalize()ed.
76 bool empty() const { return !handle_.has_value(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5077
Markus Handellad5037b2021-05-07 13:02:3678 // Creates a started joinable thread which will be joined when the returned
79 // PlatformThread destructs or Finalize() is called.
80 static PlatformThread SpawnJoinable(
81 std::function<void()> thread_function,
82 absl::string_view name,
83 ThreadAttributes attributes = ThreadAttributes());
Henrik Kjellanderec78f1c2017-06-29 05:52:5084
Markus Handellad5037b2021-05-07 13:02:3685 // Creates a started detached thread. The caller has to use external
86 // synchronization as nothing is provided by the PlatformThread construct.
87 static PlatformThread SpawnDetached(
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 // Returns the base platform thread handle of this thread.
93 absl::optional<Handle> GetHandle() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:5094
Henrik Kjellanderec78f1c2017-06-29 05:52:5095#if defined(WEBRTC_WIN)
Markus Handellad5037b2021-05-07 13:02:3696 // Queue a Windows APC function that runs when the thread is alertable.
Henrik Kjellanderec78f1c2017-06-29 05:52:5097 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
98#endif
99
100 private:
Markus Handellad5037b2021-05-07 13:02:36101 PlatformThread(Handle handle, bool joinable);
102 static PlatformThread SpawnThread(std::function<void()> thread_function,
103 absl::string_view name,
104 ThreadAttributes attributes,
105 bool joinable);
106
107 absl::optional<Handle> handle_;
108 bool joinable_ = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:50109};
110
111} // namespace rtc
pbos12411ef2015-11-23 22:47:56112
Mirko Bonadei92ea95e2017-09-15 04:47:31113#endif // RTC_BASE_PLATFORM_THREAD_H_