tst-key2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 2002-2019 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 do_test (void);
  20. #define TEST_FUNCTION do_test ()
  21. #include "../test-skeleton.c"
  22. static int cnt0;
  23. static void
  24. f0 (void *p)
  25. {
  26. ++cnt0;
  27. }
  28. static int cnt1;
  29. static void
  30. f1 (void *p)
  31. {
  32. ++cnt1;
  33. }
  34. static void (*fcts[N]) (void *) =
  35. {
  36. f0,
  37. f1
  38. };
  39. static void *
  40. tf (void *arg)
  41. {
  42. pthread_key_t *key = (pthread_key_t *) arg;
  43. if (pthread_setspecific (*key, (void *) -1l) != 0)
  44. {
  45. write_message ("setspecific failed\n");
  46. _exit (1);
  47. }
  48. return NULL;
  49. }
  50. int
  51. do_test (void)
  52. {
  53. pthread_key_t keys[N];
  54. int i;
  55. for (i = 0; i < N; ++i)
  56. if (pthread_key_create (&keys[i], fcts[i]) != 0)
  57. {
  58. write_message ("key_create failed\n");
  59. _exit (1);
  60. }
  61. pthread_t th;
  62. if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
  63. {
  64. write_message ("create failed\n");
  65. _exit (1);
  66. }
  67. if (pthread_join (th, NULL) != 0)
  68. {
  69. write_message ("join failed\n");
  70. _exit (1);
  71. }
  72. if (cnt0 != 0)
  73. {
  74. write_message ("cnt0 != 0\n");
  75. _exit (1);
  76. }
  77. if (cnt1 != 1)
  78. {
  79. write_message ("cnt1 != 1\n");
  80. _exit (1);
  81. }
  82. for (i = 0; i < N; ++i)
  83. if (pthread_key_delete (keys[i]) != 0)
  84. {
  85. write_message ("key_delete failed\n");
  86. _exit (1);
  87. }
  88. return 0;
  89. }