tst-key4.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@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. #include <limits.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #ifdef PTHREAD_KEYS_MAX
  21. const int max = PTHREAD_KEYS_MAX;
  22. #else
  23. const int max = _POSIX_THREAD_KEYS_MAX;
  24. #endif
  25. static pthread_key_t *keys;
  26. static void *
  27. tf1 (void *arg)
  28. {
  29. int i;
  30. for (i = 0; i < max; ++i)
  31. if (pthread_setspecific (keys[i], (void *) (long int) (i + 1)) != 0)
  32. {
  33. puts ("setspecific failed");
  34. exit (1);
  35. }
  36. return NULL;
  37. }
  38. static void *
  39. tf2 (void *arg)
  40. {
  41. int i;
  42. for (i = 0; i < max; ++i)
  43. if (pthread_getspecific (keys[i]) != NULL)
  44. {
  45. printf ("getspecific for key %d not NULL\n", i);
  46. exit (1);
  47. }
  48. return NULL;
  49. }
  50. static int
  51. do_test (void)
  52. {
  53. keys = alloca (max * sizeof (pthread_key_t));
  54. int i;
  55. for (i = 0; i < max; ++i)
  56. if (pthread_key_create (&keys[i], NULL) != 0)
  57. {
  58. puts ("key_create failed");
  59. exit (1);
  60. }
  61. pthread_attr_t a;
  62. if (pthread_attr_init (&a) != 0)
  63. {
  64. puts ("attr_init failed");
  65. exit (1);
  66. }
  67. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  68. {
  69. puts ("attr_setstacksize failed");
  70. return 1;
  71. }
  72. for (i = 0; i < 10; ++i)
  73. {
  74. int j;
  75. #define N 2
  76. pthread_t th[N];
  77. for (j = 0; j < N; ++j)
  78. if (pthread_create (&th[j], NULL, tf1, NULL) != 0)
  79. {
  80. puts ("1st create failed");
  81. exit (1);
  82. }
  83. for (j = 0; j < N; ++j)
  84. if (pthread_join (th[j], NULL) != 0)
  85. {
  86. puts ("1st join failed");
  87. exit (1);
  88. }
  89. for (j = 0; j < N; ++j)
  90. if (pthread_create (&th[j], NULL, tf2, NULL) != 0)
  91. {
  92. puts ("2nd create failed");
  93. exit (1);
  94. }
  95. for (j = 0; j < N; ++j)
  96. if (pthread_join (th[j], NULL) != 0)
  97. {
  98. puts ("2nd join failed");
  99. exit (1);
  100. }
  101. }
  102. if (pthread_attr_destroy (&a) != 0)
  103. {
  104. puts ("attr_destroy failed");
  105. exit (1);
  106. }
  107. return 0;
  108. }
  109. #define TEST_FUNCTION do_test ()
  110. #include "../test-skeleton.c"