tst-key4.c 2.8 KB

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