• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 "utils/logger.h"
17 
18 #ifdef PANDA_TARGET_UNIX
19 #include <unistd.h>
20 #include <sys/resource.h>
21 #include <csignal>
22 #endif
23 
24 namespace panda::os::thread {
25 
GetCurrentThreadId()26 ThreadId GetCurrentThreadId()
27 {
28 #if defined(HAVE_GETTID)
29     static_assert(sizeof(decltype(gettid())) == sizeof(ThreadId), "Incorrect alias for ThreadID");
30     return static_cast<ThreadId>(gettid());
31 #elif defined(PANDA_TARGET_MACOS) || defined(PANDA_TARGET_IOS)
32     uint64_t tid64;
33     pthread_threadid_np(NULL, &tid64);
34     return static_cast<ThreadId>(tid64);
35 #else
36     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
37     return static_cast<ThreadId>(syscall(SYS_gettid));
38 #endif
39 }
40 
GetPid()41 int GetPid()
42 {
43     return getpid();
44 }
45 
SetPriority(int thread_id,int prio)46 int SetPriority(int thread_id, int prio)
47 {
48     // The priority can be set within range [-20, 19]
49     ASSERT(prio <= 19);  // 19: the lowest priority
50     ASSERT(prio >= -20);  // -20: the highest priority
51     // The return value is 0 if the function succeeds, and -1 if it fails.
52     return setpriority(PRIO_PROCESS, thread_id, prio);
53 }
54 
GetPriority(int thread_id)55 int GetPriority(int thread_id)
56 {
57     return getpriority(PRIO_PROCESS, thread_id);
58 }
59 
SetThreadName(native_handle_type pthread_handle,const char * name)60 int SetThreadName(native_handle_type pthread_handle, const char *name)
61 {
62     ASSERT(pthread_handle != 0);
63 #if defined(PANDA_TARGET_MACOS) || defined(PANDA_TARGET_IOS)
64     return pthread_setname_np(name);
65 #else
66     return pthread_setname_np(pthread_handle, name);
67 #endif
68 }
69 
GetNativeHandle()70 native_handle_type GetNativeHandle()
71 {
72     return pthread_self();
73 }
74 
ThreadYield()75 void ThreadYield()
76 {
77     std::this_thread::yield();
78 }
79 
NativeSleep(unsigned int ms)80 void NativeSleep(unsigned int ms)
81 {
82     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
83 }
84 
ThreadDetach(native_handle_type pthread_handle)85 void ThreadDetach(native_handle_type pthread_handle)
86 {
87     pthread_detach(pthread_handle);
88 }
89 
ThreadExit(void * ret)90 void ThreadExit(void *ret)
91 {
92     pthread_exit(ret);
93 }
94 
ThreadJoin(native_handle_type pthread_handle,void ** ret)95 void ThreadJoin(native_handle_type pthread_handle, void **ret)
96 {
97     pthread_join(pthread_handle, ret);
98 }
99 
ThreadSendSignal(native_handle_type pthread_handle,int sig)100 void ThreadSendSignal(native_handle_type pthread_handle, int sig)
101 {
102     LOG_IF(pthread_kill(pthread_handle, sig) != 0, FATAL, COMMON) << "pthread_kill failed";
103 }
104 
ThreadGetStackInfo(native_handle_type thread,void ** stack_addr,size_t * stack_size,size_t * guard_size)105 int ThreadGetStackInfo(native_handle_type thread, void **stack_addr, size_t *stack_size, size_t *guard_size)
106 {
107     pthread_attr_t attr;
108     int s = pthread_attr_init(&attr);
109 #if !defined(PANDA_TARGET_MACOS) && !defined(PANDA_TARGET_IOS)
110     s += pthread_getattr_np(thread, &attr);
111     if (s == 0) {
112         s += pthread_attr_getguardsize(&attr, guard_size);
113         s += pthread_attr_getstack(&attr, stack_addr, stack_size);
114     }
115 #else
116     s += pthread_attr_getguardsize(&attr, guard_size);
117     s += pthread_attr_getstack(&attr, stack_addr, stack_size);
118 #endif
119     s += pthread_attr_destroy(&attr);
120     return s;
121 }
122 
123 }  // namespace panda::os::thread
124