1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "os/thread.h"
17
18 #include <cstdint>
19 #ifdef PANDA_TARGET_UNIX
20 #include <sys/syscall.h>
21 #include <sys/resource.h>
22 #endif
23 #include <unistd.h>
24
25 namespace panda::os::thread {
26
GetCurrentThreadId()27 ThreadId GetCurrentThreadId()
28 {
29 #if defined(HAVE_GETTID)
30 static_assert(sizeof(decltype(gettid())) == sizeof(ThreadId), "Incorrect alias for ThreadID");
31 return static_cast<ThreadId>(gettid());
32 #elif defined(PANDA_TARGET_MACOS)
33 uint64_t tid64;
34 pthread_threadid_np(NULL, &tid64);
35 return static_cast<ThreadId>(tid64);
36 #elif defined(PANDA_TARGET_UNIX)
37 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
38 return static_cast<ThreadId>(syscall(SYS_gettid));
39 #else
40 #error "Unsupported platform"
41 #endif
42 }
43
SetPriority(int thread_id,int prio)44 int SetPriority(int thread_id, int prio)
45 {
46 #if defined(PANDA_TARGET_UNIX)
47 return setpriority(PRIO_PROCESS, thread_id, prio);
48 #else
49 #error "Unsupported platform"
50 #endif
51 }
52
GetPriority(int thread_id)53 int GetPriority(int thread_id)
54 {
55 #if defined(PANDA_TARGET_UNIX)
56 return getpriority(PRIO_PROCESS, thread_id);
57 #else
58 #error "Unsupported platform"
59 #endif
60 }
61
SetThreadName(native_handle_type pthread_id,const char * name)62 int SetThreadName(native_handle_type pthread_id, const char *name)
63 {
64 ASSERT(pthread_id != 0);
65 #if defined(PANDA_TARGET_MACOS)
66 return pthread_setname_np(name);
67 #elif defined(PANDA_TARGET_UNIX)
68 return pthread_setname_np(pthread_id, name);
69 #else
70 #error "Unsupported platform"
71 #endif
72 }
73
GetNativeHandle()74 native_handle_type GetNativeHandle()
75 {
76 #if defined(PANDA_TARGET_UNIX)
77 return pthread_self();
78 #else
79 #error "Unsupported platform"
80 #endif
81 }
82
Yield()83 void Yield()
84 {
85 #if defined(PANDA_TARGET_UNIX)
86 std::this_thread::yield();
87 #else
88 #error "Unsupported platform"
89 #endif
90 }
91
NativeSleep(unsigned int ms)92 void NativeSleep(unsigned int ms)
93 {
94 #if defined(PANDA_TARGET_UNIX)
95 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
96 #else
97 #error "Unsupported platform"
98 #endif
99 }
100
ThreadDetach(native_handle_type pthread_id)101 void ThreadDetach(native_handle_type pthread_id)
102 {
103 #if defined(PANDA_TARGET_UNIX)
104 pthread_detach(pthread_id);
105 #else
106 #error "Unsupported platform"
107 #endif
108 }
109
ThreadExit(void * retval)110 void ThreadExit(void *retval)
111 {
112 #if defined(PANDA_TARGET_UNIX)
113 pthread_exit(retval);
114 #else
115 #error "Unsupported platform"
116 #endif
117 }
118
ThreadJoin(native_handle_type pthread_id,void ** retval)119 void ThreadJoin(native_handle_type pthread_id, void **retval)
120 {
121 #if defined(PANDA_TARGET_UNIX)
122 pthread_join(pthread_id, retval);
123 #else
124 #error "Unsupported platform"
125 #endif
126 }
127
128 } // namespace panda::os::thread
129