pthread_attr_setaffinity.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. for (size_t cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
  51. if (((char *) cpuset)[cnt] != '\0')
  52. /* Found a nonzero byte. This means the user request cannot be
  53. fulfilled. */
  54. return EINVAL;
  55. if (iattr->cpusetsize != cpusetsize)
  56. {
  57. void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize);
  58. if (newp == NULL)
  59. return ENOMEM;
  60. iattr->cpuset = newp;
  61. iattr->cpusetsize = cpusetsize;
  62. }
  63. memcpy (iattr->cpuset, cpuset, cpusetsize);
  64. }
  65. return 0;
  66. }