tst-key3.c 3.1 KB

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