| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #ifndef __RT_GET_CPU_H |
| 3 | #define __RT_GET_CPU_H |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/syscall.h> /* For SYS_xxx definitions */ |
| 9 | #include <sched.h> |
| 10 | #include <dlfcn.h> |
| 11 | #ifdef __NR_getcpu |
| 12 | static inline int get_cpu_setup(void) { return 0; } |
| 13 | static inline int get_cpu(void) |
| 14 | { |
| 15 | int c,s; |
| 16 | /* Show the source of get_cpu */ |
| 17 | #ifdef DEBUG |
| 18 | fprintf(stderr, "__NR_getcpu\n"); |
| 19 | #endif |
| 20 | s = syscall(__NR_getcpu, &c, NULL, NULL); |
| 21 | return (s == -1) ? s : c; |
| 22 | } |
| 23 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
| 24 | && __GLIBC__>=2 && __GLIBC_MINOR__>=6 |
| 25 | #include <utmpx.h> |
| 26 | static inline int get_cpu_setup(void) { return 0; } |
| 27 | static inline int get_cpu(void) { return sched_getcpu(); } |
| 28 | #else |
| 29 | extern int get_cpu_setup(void); |
| 30 | extern int (*get_cpu)(void); |
| 31 | extern int (*get_cpu_vdsop)(unsigned int *, unsigned int *, void *); |
| 32 | |
| 33 | static inline int getcpu_vdso(void) |
| 34 | { |
| 35 | unsigned int c,s; |
| 36 | /* Show the source of get_cpu */ |
| 37 | #ifdef DEBUG |
| 38 | fprintf(stderr, "getcpu_vdso\n"); |
| 39 | #endif |
| 40 | s = get_cpu_vdsop(&c, NULL, NULL); |
| 41 | return (s == -1) ? s : c; |
| 42 | } |
| 43 | |
| 44 | #endif |
| 45 | |
| 46 | #endif /* __RT_GET_CPU_H */ |
| 47 |