tst-tsd3.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2003 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. static pthread_key_t key1;
  21. static pthread_key_t key2;
  22. static int left;
  23. static void
  24. destr1 (void *arg)
  25. {
  26. if (--left > 0)
  27. {
  28. puts ("set key2");
  29. if (pthread_setspecific (key2, (void *) 1l) != 0)
  30. {
  31. puts ("destr1: setspecific failed");
  32. exit (1);
  33. }
  34. }
  35. }
  36. static void
  37. destr2 (void *arg)
  38. {
  39. if (--left > 0)
  40. {
  41. puts ("set key1");
  42. if (pthread_setspecific (key1, (void *) 1l) != 0)
  43. {
  44. puts ("destr2: setspecific failed");
  45. exit (1);
  46. }
  47. }
  48. }
  49. static void *
  50. tf (void *arg)
  51. {
  52. /* Let the destructors work. */
  53. left = 7;
  54. if (pthread_setspecific (key1, (void *) 1l) != 0
  55. || pthread_setspecific (key2, (void *) 1l) != 0)
  56. {
  57. puts ("tf: setspecific failed");
  58. exit (1);
  59. }
  60. return NULL;
  61. }
  62. static int
  63. do_test (void)
  64. {
  65. /* Allocate two keys, both with destructors. */
  66. if (pthread_key_create (&key1, destr1) != 0
  67. || pthread_key_create (&key2, destr2) != 0)
  68. {
  69. puts ("key_create failed");
  70. return 1;
  71. }
  72. pthread_t th;
  73. if (pthread_create (&th, NULL, tf, NULL) != 0)
  74. {
  75. puts ("create failed");
  76. return 1;
  77. }
  78. if (pthread_join (th, NULL) != 0)
  79. {
  80. puts ("join failed");
  81. return 1;
  82. }
  83. if (left != 0)
  84. {
  85. printf ("left == %d\n", left);
  86. return 1;
  87. }
  88. if (pthread_getspecific (key1) != NULL)
  89. {
  90. puts ("key1 data != NULL");
  91. return 1;
  92. }
  93. if (pthread_getspecific (key2) != NULL)
  94. {
  95. puts ("key2 data != NULL");
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. #define TEST_FUNCTION do_test ()
  101. #include "../test-skeleton.c"