tst-stack1.c 3.2 KB

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