sched_getcpu.c 833 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <sched.h>
  4. #include "syscall.h"
  5. #include "atomic.h"
  6. #ifdef VDSO_GETCPU_SYM
  7. static void *volatile vdso_func;
  8. typedef long (*getcpu_f)(unsigned *, unsigned *, void *);
  9. static long getcpu_init(unsigned *cpu, unsigned *node, void *unused)
  10. {
  11. void *p = __vdsosym(VDSO_GETCPU_VER, VDSO_GETCPU_SYM);
  12. getcpu_f f = (getcpu_f)p;
  13. a_cas_p(&vdso_func, (void *)getcpu_init, p);
  14. return f ? f(cpu, node, unused) : -ENOSYS;
  15. }
  16. static void *volatile vdso_func = (void *)getcpu_init;
  17. #endif
  18. int sched_getcpu(void)
  19. {
  20. int r;
  21. unsigned cpu;
  22. #ifdef VDSO_GETCPU_SYM
  23. getcpu_f f = (getcpu_f)vdso_func;
  24. if (f) {
  25. r = f(&cpu, 0, 0);
  26. if (!r) return cpu;
  27. if (r != -ENOSYS) return __syscall_ret(r);
  28. }
  29. #endif
  30. r = __syscall(SYS_getcpu, &cpu, 0, 0);
  31. if (!r) return cpu;
  32. return __syscall_ret(r);
  33. }