tst-pthread-attr-affinity.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Make sure that pthread_attr_getaffinity_np does not crash when the input
  2. cpuset size is smaller than that in the attribute structure.
  3. Copyright (C) 2013-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <sched.h>
  19. #include <errno.h>
  20. #include <sys/param.h>
  21. #define RETURN_IF_FAIL(f, ...) \
  22. ({ \
  23. int ret = f (__VA_ARGS__); \
  24. if (ret != 0) \
  25. { \
  26. printf ("%s:%d: %s returned %d (errno = %d)\n", __FILE__, __LINE__, \
  27. #f, ret, errno); \
  28. return ret; \
  29. } \
  30. })
  31. static int
  32. do_test (void)
  33. {
  34. for (int i = 0; i < 10; i++)
  35. {
  36. pthread_attr_t attr;
  37. cpu_set_t *cpuset = CPU_ALLOC (512);
  38. size_t cpusetsize = CPU_ALLOC_SIZE (512);
  39. CPU_ZERO_S (cpusetsize, cpuset);
  40. RETURN_IF_FAIL (pthread_attr_init, &attr);
  41. RETURN_IF_FAIL (pthread_attr_setaffinity_np, &attr, cpusetsize, cpuset);
  42. CPU_FREE (cpuset);
  43. cpuset = CPU_ALLOC (1);
  44. cpusetsize = CPU_ALLOC_SIZE (1);
  45. RETURN_IF_FAIL (pthread_attr_getaffinity_np, &attr, cpusetsize, cpuset);
  46. CPU_FREE (cpuset);
  47. }
  48. return 0;
  49. }
  50. #define TEST_FUNCTION do_test ()
  51. #include "../test-skeleton.c"