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 "os/thread.h"
17 #include "utils/logger.h"
18
19 #include <errhandlingapi.h>
20 #include <handleapi.h>
21 #include <process.h>
22 #include <processthreadsapi.h>
23 #include <thread>
24
25 namespace panda::os::thread {
GetCurrentThreadId()26 ThreadId GetCurrentThreadId()
27 {
28 // The function is provided by mingw
29 return ::GetCurrentThreadId();
30 }
31
GetPid()32 int GetPid()
33 {
34 return _getpid();
35 }
36
SetPriority(DWORD thread_id,int prio)37 int SetPriority(DWORD thread_id, int prio)
38 {
39 // The priority can be set within range [-2, 2]
40 ASSERT(prio >= -2); // -2: the lowest priority
41 ASSERT(prio <= 2); // 2: the highest priority
42 HANDLE thread = OpenThread(THREAD_SET_INFORMATION, false, thread_id);
43 if (thread == NULL) {
44 LOG(FATAL, COMMON) << "OpenThread failed, error code " << GetLastError();
45 }
46 auto ret = SetThreadPriority(thread, prio);
47 CloseHandle(thread);
48 // Thre return value is nonzero if the function succeeds, and zero if it fails.
49 return ret;
50 }
51
GetPriority(DWORD thread_id)52 int GetPriority(DWORD thread_id)
53 {
54 HANDLE thread = OpenThread(THREAD_QUERY_INFORMATION, false, thread_id);
55 if (thread == NULL) {
56 LOG(FATAL, COMMON) << "OpenThread failed, error code " << GetLastError();
57 }
58 auto ret = GetThreadPriority(thread);
59 CloseHandle(thread);
60 return ret;
61 }
62
SetThreadName(native_handle_type pthread_handle,const char * name)63 int SetThreadName(native_handle_type pthread_handle, const char *name)
64 {
65 ASSERT(pthread_handle != 0);
66 pthread_t thread = reinterpret_cast<pthread_t>(pthread_handle);
67 return pthread_setname_np(thread, name);
68 }
69
GetNativeHandle()70 native_handle_type GetNativeHandle()
71 {
72 return reinterpret_cast<native_handle_type>(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(reinterpret_cast<pthread_t>(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(reinterpret_cast<pthread_t>(pthread_handle), ret);
98 }
99 } // namespace panda::os::thread
100