tst-key2.c 2.2 KB

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