tst-stack3.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Test whether pthread_create/pthread_join with user defined stacks
  16. doesn't leak memory.
  17. NOTE: this tests functionality beyond POSIX. In POSIX user defined
  18. stacks cannot be ever freed once used by pthread_create nor they can
  19. be reused for other thread. */
  20. #include <limits.h>
  21. #include <mcheck.h>
  22. #include <pthread.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. static int seen;
  28. static void *
  29. tf (void *p)
  30. {
  31. ++seen;
  32. return NULL;
  33. }
  34. static int
  35. do_test (void)
  36. {
  37. mtrace ();
  38. void *stack;
  39. int res = posix_memalign (&stack, getpagesize (), 4 * PTHREAD_STACK_MIN);
  40. if (res)
  41. {
  42. printf ("malloc failed %s\n", strerror (res));
  43. return 1;
  44. }
  45. pthread_attr_t attr;
  46. pthread_attr_init (&attr);
  47. int result = 0;
  48. res = pthread_attr_setstack (&attr, stack, 4 * PTHREAD_STACK_MIN);
  49. if (res)
  50. {
  51. printf ("pthread_attr_setstack failed %d\n", res);
  52. result = 1;
  53. }
  54. for (int i = 0; i < 16; ++i)
  55. {
  56. /* Create the thread. */
  57. pthread_t th;
  58. res = pthread_create (&th, &attr, tf, NULL);
  59. if (res)
  60. {
  61. printf ("pthread_create failed %d\n", res);
  62. result = 1;
  63. }
  64. else
  65. {
  66. res = pthread_join (th, NULL);
  67. if (res)
  68. {
  69. printf ("pthread_join failed %d\n", res);
  70. result = 1;
  71. }
  72. }
  73. }
  74. pthread_attr_destroy (&attr);
  75. if (seen != 16)
  76. {
  77. printf ("seen %d != 16\n", seen);
  78. result = 1;
  79. }
  80. free (stack);
  81. return result;
  82. }
  83. #define TEST_FUNCTION do_test ()
  84. #include "../test-skeleton.c"