xref: /aosp_15_r20/system/chre/platform/include/chre/platform/thread_handle.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_PLATFORM_THREAD_HANDLE_H_
18 #define CHRE_PLATFORM_THREAD_HANDLE_H_
19 
20 #include "chre/target_platform/thread_handle_base.h"
21 
22 namespace chre {
23 
24 /**
25  * Wrapper around a platform-specific thread handle.
26  *
27  * The user can get a ThreadHandle representing the current thread or convert
28  * to/from a platform-specific representation. ThreadHandles can be compared for
29  * equality. ThreadHandles are copyable, though the exact behavior is platform
30  * specific.
31  *
32  * ThreadHandleBase is subclassed to allow platforms to inject the storage for
33  * their implementation.
34  */
35 class ThreadHandle : public ThreadHandleBase {
36  public:
37   using ThreadHandleBase::NativeHandle;
38 
39   /**
40    * Returns the ThreadHandle for the current thread/task.
41    */
GetCurrent()42   static ThreadHandle GetCurrent() {
43     return ThreadHandle();
44   }
45 
46   /**
47    * Creates a ThreadHandle from a platform-specific id.
48    */
49   explicit ThreadHandle(NativeHandle nativeHandle);
50 
51   /**
52    * ThreadHandle is copyable and movable.
53    */
54   ThreadHandle(const ThreadHandle &other);
55   ThreadHandle(ThreadHandle &&other);
56   ThreadHandle &operator=(const ThreadHandle &other);
57   ThreadHandle &operator=(ThreadHandle &&other);
58 
59   /**
60    * Allows the platform to perform any necessary de-initialization.
61    */
62   ~ThreadHandle();
63 
64   /**
65    * Returns the platform-specific id.
66    */
67   NativeHandle GetNative() const;
68 
69   /**
70    * Compares with another ThreadHandle for equality.
71    */
72   bool operator==(const ThreadHandle &other) const;
73 
74   /**
75    * Compares with another ThreadHandle for inequality.
76    */
77   bool operator!=(const ThreadHandle &other) const {
78     return !(*this == other);
79   }
80 
81  protected:
82   /**
83    * Allows the platform to perform any necessary initialization.
84    */
85   ThreadHandle();
86 };
87 
88 }  // namespace chre
89 
90 #include "chre/target_platform/thread_handle_impl.h"
91 
92 #endif  // CHRE_PLATFORM_THREAD_HANDLE_H_
93