pthread_setaffinity.c 2.8 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <alloca.h>
  17. #include <errno.h>
  18. #include <pthreadP.h>
  19. #include <sysdep.h>
  20. #include <sys/types.h>
  21. size_t __kernel_cpumask_size attribute_hidden;
  22. /* Determine the current affinity. As a side affect we learn
  23. about the size of the cpumask_t in the kernel. */
  24. extern int __determine_cpumask_size (pid_t tid);
  25. libpthread_hidden_proto(__determine_cpumask_size)
  26. int __determine_cpumask_size (pid_t tid)
  27. {
  28. INTERNAL_SYSCALL_DECL (err);
  29. int res;
  30. size_t psize = 128;
  31. void *p = alloca (psize);
  32. while (res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, tid, psize, p),
  33. INTERNAL_SYSCALL_ERROR_P (res, err)
  34. && INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL)
  35. p = extend_alloca (p, psize, 2 * psize);
  36. if (res == 0 || INTERNAL_SYSCALL_ERROR_P (res, err))
  37. return INTERNAL_SYSCALL_ERRNO (res, err);
  38. __kernel_cpumask_size = res;
  39. return 0;
  40. }
  41. libpthread_hidden_def(__determine_cpumask_size)
  42. int
  43. pthread_setaffinity_np (pthread_t th, size_t cpusetsize,
  44. const cpu_set_t *cpuset)
  45. {
  46. const struct pthread *pd = (const struct pthread *) th;
  47. INTERNAL_SYSCALL_DECL (err);
  48. int res;
  49. if (__builtin_expect (__kernel_cpumask_size == 0, 0))
  50. {
  51. res = __determine_cpumask_size (pd->tid);
  52. if (res != 0)
  53. return res;
  54. }
  55. /* We now know the size of the kernel cpumask_t. Make sure the user
  56. does not request to set a bit beyond that. */
  57. for (size_t 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. }