1 /*
2 * Copyright (c) 2025 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 #include "common_components/base/sys_call.h"
16
17 #include <cstdlib>
18 #include <unistd.h>
19 #if defined(_WIN64)
20 #include <processthreadsapi.h>
21 #elif defined(__APPLE__)
22 #include "sys/syscall.h"
23 #else
24 #include "linux/futex.h"
25 #include "sys/syscall.h"
26 #endif
27
28 namespace common {
29 #ifndef SYS_futex
30 #define SYS_futex __NR_futex
31 #endif
32
33 #if defined(__linux__) || defined(PANDA_TARGET_OHOS)
34 // only support FUTEX_WAIT/FUTEX_WAKE
Futex(const volatile int * uaddr,int op,int val)35 int Futex(const volatile int* uaddr, int op, int val)
36 {
37 return syscall(SYS_futex, uaddr, op, val, nullptr, nullptr, 0);
38 }
39 #endif
40
GetTid()41 pid_t GetTid()
42 {
43 #if defined(_WIN64)
44 return GetCurrentThreadId();
45 #elif defined(__APPLE__)
46 uint64_t tid;
47 pthread_threadid_np(nullptr, &tid);
48 return static_cast<pid_t>(tid);
49 #else
50 return syscall(SYS_gettid);
51 #endif
52 }
53
GetPid()54 int GetPid()
55 {
56 #if defined(__APPLE__)
57 return getpid();
58 #elif defined(_WIN64)
59 return GetCurrentProcessId();
60 #else
61 return syscall(SYS_getpid);
62 #endif
63 }
64 } // namespace common
65