pthread_setaffinity.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. size_t cnt;
  58. for (cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
  59. if (((char *) cpuset)[cnt] != '\0')
  60. /* Found a nonzero byte. This means the user request cannot be
  61. fulfilled. */
  62. return EINVAL;
  63. res = INTERNAL_SYSCALL (sched_setaffinity, err, 3, pd->tid, cpusetsize,
  64. cpuset);
  65. #ifdef RESET_VGETCPU_CACHE
  66. if (!INTERNAL_SYSCALL_ERROR_P (res, err))
  67. RESET_VGETCPU_CACHE ();
  68. #endif
  69. return (INTERNAL_SYSCALL_ERROR_P (res, err)
  70. ? INTERNAL_SYSCALL_ERRNO (res, err)
  71. : 0);
  72. }