tst-key3.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <pthread.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #define N 2
  19. static int cnt0;
  20. static void
  21. f0 (void *p)
  22. {
  23. ++cnt0;
  24. }
  25. static int cnt1;
  26. static void
  27. f1 (void *p)
  28. {
  29. ++cnt1;
  30. }
  31. static void (*fcts[N]) (void *) =
  32. {
  33. f0,
  34. f1
  35. };
  36. static pthread_barrier_t b;
  37. static void *
  38. tf (void *arg)
  39. {
  40. pthread_key_t *key = (pthread_key_t *) arg;
  41. if (pthread_setspecific (*key, (void *) -1l) != 0)
  42. {
  43. write (2, "setspecific failed\n", 19);
  44. _exit (1);
  45. }
  46. pthread_barrier_wait (&b);
  47. const struct timespec t = { .tv_sec = 1000, .tv_nsec = 0 };
  48. while (1)
  49. nanosleep (&t, NULL);
  50. /* NOTREACHED */
  51. return NULL;
  52. }
  53. int
  54. do_test (void)
  55. {
  56. pthread_key_t keys[N];
  57. int i;
  58. for (i = 0; i < N; ++i)
  59. if (pthread_key_create (&keys[i], fcts[i]) != 0)
  60. {
  61. write (2, "key_create failed\n", 18);
  62. _exit (1);
  63. }
  64. if (pthread_barrier_init (&b, NULL, 2) != 0)
  65. {
  66. write (2, "barrier_init failed\n", 20);
  67. _exit (1);
  68. }
  69. pthread_t th;
  70. if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
  71. {
  72. write (2, "create failed\n", 14);
  73. _exit (1);
  74. }
  75. pthread_barrier_wait (&b);
  76. if (pthread_cancel (th) != 0)
  77. {
  78. write (2, "cancel failed\n", 14);
  79. _exit (1);
  80. }
  81. void *status;
  82. if (pthread_join (th, &status) != 0)
  83. {
  84. write (2, "join failed\n", 12);
  85. _exit (1);
  86. }
  87. if (status != PTHREAD_CANCELED)
  88. {
  89. write (2, "thread not canceled\n", 20);
  90. _exit (1);
  91. }
  92. /* Note that the TSD destructors not necessarily have to have
  93. finished by the time pthread_join returns. At least according to
  94. POSIX. We implement the stronger requirement that they indeed
  95. have run and therefore these tests succeed. */
  96. if (cnt0 != 0)
  97. {
  98. write (2, "cnt0 != 0\n", 10);
  99. _exit (1);
  100. }
  101. if (cnt1 != 1)
  102. {
  103. write (2, "cnt1 != 1\n", 10);
  104. _exit (1);
  105. }
  106. for (i = 0; i < N; ++i)
  107. if (pthread_key_delete (keys[i]) != 0)
  108. {
  109. write (2, "key_delete failed\n", 18);
  110. _exit (1);
  111. }
  112. if (pthread_barrier_destroy (&b) != 0)
  113. {
  114. write (2, "barrier_destroy failed\n", 23);
  115. _exit (1);
  116. }
  117. return 0;
  118. }
  119. #define TEST_FUNCTION do_test ()
  120. #include "../test-skeleton.c"