tst-stack1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <limits.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/param.h>
  20. #include <unistd.h>
  21. static void *stack;
  22. static size_t size;
  23. static void *
  24. tf (void *a)
  25. {
  26. int result = 0;
  27. puts ("child start");
  28. pthread_attr_t attr;
  29. if (pthread_getattr_np (pthread_self (), &attr) != 0)
  30. {
  31. puts ("getattr_np failed");
  32. exit (1);
  33. }
  34. size_t test_size;
  35. void *test_stack;
  36. if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
  37. {
  38. puts ("attr_getstack failed");
  39. exit (1);
  40. }
  41. if (test_size != size)
  42. {
  43. printf ("child: reported size differs: is %zu, expected %zu\n",
  44. test_size, size);
  45. result = 1;
  46. }
  47. if (test_stack != stack)
  48. {
  49. printf ("child: reported stack address differs: is %p, expected %p\n",
  50. test_stack, stack);
  51. result = 1;
  52. }
  53. puts ("child OK");
  54. return result ? (void *) 1l : NULL;
  55. }
  56. int
  57. do_test (void)
  58. {
  59. int result = 0;
  60. size = MAX (4 * getpagesize (), PTHREAD_STACK_MIN);
  61. if (posix_memalign (&stack, getpagesize (), size) != 0)
  62. {
  63. puts ("out of memory while allocating the stack memory");
  64. exit (1);
  65. }
  66. pthread_attr_t attr;
  67. if (pthread_attr_init (&attr) != 0)
  68. {
  69. puts ("attr_init failed");
  70. exit (1);
  71. }
  72. puts ("attr_setstack");
  73. if (pthread_attr_setstack (&attr, stack, size) != 0)
  74. {
  75. puts ("attr_setstack failed");
  76. exit (1);
  77. }
  78. size_t test_size;
  79. void *test_stack;
  80. puts ("attr_getstack");
  81. if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
  82. {
  83. puts ("attr_getstack failed");
  84. exit (1);
  85. }
  86. if (test_size != size)
  87. {
  88. printf ("reported size differs: is %zu, expected %zu\n",
  89. test_size, size);
  90. result = 1;
  91. }
  92. if (test_stack != stack)
  93. {
  94. printf ("reported stack address differs: is %p, expected %p\n",
  95. test_stack, stack);
  96. result = 1;
  97. }
  98. puts ("create");
  99. pthread_t th;
  100. if (pthread_create (&th, &attr, tf, NULL) != 0)
  101. {
  102. puts ("failed to create thread");
  103. exit (1);
  104. }
  105. void *status;
  106. if (pthread_join (th, &status) != 0)
  107. {
  108. puts ("join failed");
  109. exit (1);
  110. }
  111. result |= status != NULL;
  112. return result;
  113. }
  114. #define TEST_FUNCTION do_test ()
  115. #include "../test-skeleton.c"