1
0

sched_getcpu.c 879 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. void *__vdsosym(const char *, const char *);
  8. static void *volatile vdso_func;
  9. typedef long (*getcpu_f)(unsigned *, unsigned *, void *);
  10. static long getcpu_init(unsigned *cpu, unsigned *node, void *unused)
  11. {
  12. void *p = __vdsosym(VDSO_GETCPU_VER, VDSO_GETCPU_SYM);
  13. getcpu_f f = (getcpu_f)p;
  14. a_cas_p(&vdso_func, (void *)getcpu_init, p);
  15. return f ? f(cpu, node, unused) : -ENOSYS;
  16. }
  17. static void *volatile vdso_func = (void *)getcpu_init;
  18. #endif
  19. int sched_getcpu(void)
  20. {
  21. int r;
  22. unsigned cpu;
  23. #ifdef VDSO_GETCPU_SYM
  24. getcpu_f f = (getcpu_f)vdso_func;
  25. if (f) {
  26. r = f(&cpu, 0, 0);
  27. if (!r) return cpu;
  28. if (r != -ENOSYS) return __syscall_ret(r);
  29. }
  30. #endif
  31. r = __syscall(SYS_getcpu, &cpu, 0, 0);
  32. if (!r) return cpu;
  33. return __syscall_ret(r);
  34. }