pthread_attr_setaffinity.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2003, 2004, 2006 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 <assert.h>
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <pthreadP.h>
  22. /* Defined in pthread_setaffinity.c. */
  23. extern size_t __kernel_cpumask_size attribute_hidden;
  24. extern int __determine_cpumask_size (pid_t tid);
  25. libpthread_hidden_proto(__determine_cpumask_size)
  26. int
  27. pthread_attr_setaffinity_np (pthread_attr_t *attr, size_t cpusetsize,
  28. const cpu_set_t *cpuset)
  29. {
  30. struct pthread_attr *iattr;
  31. assert (sizeof (*attr) >= sizeof (struct pthread_attr));
  32. iattr = (struct pthread_attr *) attr;
  33. if (cpuset == NULL || cpusetsize == 0)
  34. {
  35. free (iattr->cpuset);
  36. iattr->cpuset = NULL;
  37. iattr->cpusetsize = 0;
  38. }
  39. else
  40. {
  41. if (__kernel_cpumask_size == 0)
  42. {
  43. int res = __determine_cpumask_size (THREAD_SELF->tid);
  44. if (res != 0)
  45. /* Some serious problem. */
  46. return res;
  47. }
  48. /* Check whether the new bitmask has any bit set beyond the
  49. last one the kernel accepts. */
  50. size_t cnt;
  51. for (cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
  52. if (((char *) cpuset)[cnt] != '\0')
  53. /* Found a nonzero byte. This means the user request cannot be
  54. fulfilled. */
  55. return EINVAL;
  56. if (iattr->cpusetsize != cpusetsize)
  57. {
  58. void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize);
  59. if (newp == NULL)
  60. return ENOMEM;
  61. iattr->cpuset = newp;
  62. iattr->cpusetsize = cpusetsize;
  63. }
  64. memcpy (iattr->cpuset, cpuset, cpusetsize);
  65. }
  66. return 0;
  67. }