tst-thread-affinity-pthread2.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Separate thread test for pthread_getaffinity_np, pthread_setaffinity_np.
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. /* Defined for the benefit of tst-skeleton-thread-affinity.c, included
  21. below. This variant runs the functions on a separate thread. */
  22. struct affinity_access_task
  23. {
  24. pthread_t thread;
  25. cpu_set_t *set;
  26. size_t size;
  27. bool get;
  28. int result;
  29. };
  30. static void *
  31. affinity_access_thread (void *closure)
  32. {
  33. struct affinity_access_task *task = closure;
  34. if (task->get)
  35. task->result = pthread_getaffinity_np
  36. (task->thread, task->size, task->set);
  37. else
  38. task->result = pthread_setaffinity_np
  39. (task->thread, task->size, task->set);
  40. return NULL;
  41. }
  42. static int
  43. run_affinity_access_thread (cpu_set_t *set, size_t size, bool get)
  44. {
  45. struct affinity_access_task task =
  46. {
  47. .thread = pthread_self (),
  48. .set = set,
  49. .size = size,
  50. .get = get
  51. };
  52. pthread_t thr;
  53. int ret = pthread_create (&thr, NULL, affinity_access_thread, &task);
  54. if (ret != 0)
  55. {
  56. errno = ret;
  57. printf ("error: could not create affinity access thread: %m\n");
  58. abort ();
  59. }
  60. ret = pthread_join (thr, NULL);
  61. if (ret != 0)
  62. {
  63. errno = ret;
  64. printf ("error: could not join affinity access thread: %m\n");
  65. abort ();
  66. }
  67. if (task.result != 0)
  68. {
  69. errno = task.result;
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static int
  75. setaffinity (size_t size, const cpu_set_t *set)
  76. {
  77. return run_affinity_access_thread ((cpu_set_t *) set, size, false);
  78. }
  79. static int
  80. getaffinity (size_t size, cpu_set_t *set)
  81. {
  82. return run_affinity_access_thread (set, size, true);
  83. }
  84. #include "tst-skeleton-thread-affinity.c"