tst-key2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 void *
  37. tf (void *arg)
  38. {
  39. pthread_key_t *key = (pthread_key_t *) arg;
  40. if (pthread_setspecific (*key, (void *) -1l) != 0)
  41. {
  42. write (2, "setspecific failed\n", 19);
  43. _exit (1);
  44. }
  45. return NULL;
  46. }
  47. int
  48. do_test (void)
  49. {
  50. pthread_key_t keys[N];
  51. int i;
  52. for (i = 0; i < N; ++i)
  53. if (pthread_key_create (&keys[i], fcts[i]) != 0)
  54. {
  55. write (2, "key_create failed\n", 18);
  56. _exit (1);
  57. }
  58. pthread_t th;
  59. if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
  60. {
  61. write (2, "create failed\n", 14);
  62. _exit (1);
  63. }
  64. if (pthread_join (th, NULL) != 0)
  65. {
  66. write (2, "join failed\n", 12);
  67. _exit (1);
  68. }
  69. if (cnt0 != 0)
  70. {
  71. write (2, "cnt0 != 0\n", 10);
  72. _exit (1);
  73. }
  74. if (cnt1 != 1)
  75. {
  76. write (2, "cnt1 != 1\n", 10);
  77. _exit (1);
  78. }
  79. for (i = 0; i < N; ++i)
  80. if (pthread_key_delete (keys[i]) != 0)
  81. {
  82. write (2, "key_delete failed\n", 18);
  83. _exit (1);
  84. }
  85. return 0;
  86. }
  87. #define TEST_FUNCTION do_test ()
  88. #include "../test-skeleton.c"