pthread_setaffinity.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <alloca.h>
  16. #include <errno.h>
  17. #include <pthreadP.h>
  18. #include <sysdep.h>
  19. #include <sys/types.h>
  20. size_t __kernel_cpumask_size attribute_hidden;
  21. /* Determine the current affinity. As a side affect we learn
  22. about the size of the cpumask_t in the kernel. */
  23. extern int __determine_cpumask_size (pid_t tid);
  24. libpthread_hidden_proto(__determine_cpumask_size)
  25. int __determine_cpumask_size (pid_t tid)
  26. {
  27. INTERNAL_SYSCALL_DECL (err);
  28. int res;
  29. size_t psize = 128;
  30. void *p = alloca (psize);
  31. while (res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, tid, psize, p),
  32. INTERNAL_SYSCALL_ERROR_P (res, err)
  33. && INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL)
  34. p = extend_alloca (p, psize, 2 * psize);
  35. if (res == 0 || INTERNAL_SYSCALL_ERROR_P (res, err))
  36. return INTERNAL_SYSCALL_ERRNO (res, err);
  37. __kernel_cpumask_size = res;
  38. return 0;
  39. }
  40. libpthread_hidden_def(__determine_cpumask_size)
  41. int
  42. pthread_setaffinity_np (pthread_t th, size_t cpusetsize,
  43. const cpu_set_t *cpuset)
  44. {
  45. const struct pthread *pd = (const struct pthread *) th;
  46. INTERNAL_SYSCALL_DECL (err);
  47. int res;
  48. if (__builtin_expect (__kernel_cpumask_size == 0, 0))
  49. {
  50. res = __determine_cpumask_size (pd->tid);
  51. if (res != 0)
  52. return res;
  53. }
  54. /* We now know the size of the kernel cpumask_t. Make sure the user
  55. does not request to set a bit beyond that. */
  56. size_t cnt;
  57. for (cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
  58. if (((char *) cpuset)[cnt] != '\0')
  59. /* Found a nonzero byte. This means the user request cannot be
  60. fulfilled. */
  61. return EINVAL;
  62. res = INTERNAL_SYSCALL (sched_setaffinity, err, 3, pd->tid, cpusetsize,
  63. cpuset);
  64. #ifdef RESET_VGETCPU_CACHE
  65. if (!INTERNAL_SYSCALL_ERROR_P (res, err))
  66. RESET_VGETCPU_CACHE ();
  67. #endif
  68. return (INTERNAL_SYSCALL_ERROR_P (res, err)
  69. ? INTERNAL_SYSCALL_ERRNO (res, err)
  70. : 0);
  71. }