pthread_setaffinity.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (C) 2003, 2004 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;
  22. /* Determine the current affinity. As a side affect we learn
  23. about the size of the cpumask_t in the kernel. */
  24. int
  25. __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. int
  41. __pthread_setaffinity_new (pthread_t th, size_t cpusetsize,
  42. const cpu_set_t *cpuset)
  43. {
  44. const struct pthread *pd = (const struct pthread *) th;
  45. INTERNAL_SYSCALL_DECL (err);
  46. int res;
  47. if (__builtin_expect (__kernel_cpumask_size == 0, 0))
  48. {
  49. res = __determine_cpumask_size (pd->tid);
  50. if (res != 0)
  51. return res;
  52. }
  53. /* We now know the size of the kernel cpumask_t. Make sure the user
  54. does not request to set a bit beyond that. */
  55. size_t cnt;
  56. for (cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
  57. if (((char *) cpuset)[cnt] != '\0')
  58. /* Found a nonzero byte. This means the user request cannot be
  59. fulfilled. */
  60. return EINVAL;
  61. res = INTERNAL_SYSCALL (sched_setaffinity, err, 3, pd->tid, cpusetsize,
  62. cpuset);
  63. return (INTERNAL_SYSCALL_ERROR_P (res, err)
  64. ? INTERNAL_SYSCALL_ERRNO (res, err)
  65. : 0);
  66. }
  67. weak_alias(__pthread_setaffinity_new, pthread_setaffinity_np)