tst-tsd3.c 2.5 KB

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