tst-tsd2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 2002, 2003 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 <string.h>
  18. static int result;
  19. static void
  20. destr (void *arg)
  21. {
  22. if (arg != (void *) -2l)
  23. result = 2;
  24. else
  25. result = 0;
  26. }
  27. static void *
  28. tf (void *arg)
  29. {
  30. pthread_key_t key = (pthread_key_t) (long int) arg;
  31. int err;
  32. err = pthread_setspecific (key, (void *) -2l);
  33. if (err != 0)
  34. result = 3;
  35. return NULL;
  36. }
  37. static int
  38. do_test (void)
  39. {
  40. pthread_key_t key;
  41. pthread_t th;
  42. int err;
  43. err = pthread_key_create (&key, destr);
  44. if (err != 0)
  45. {
  46. printf ("key_create failed: %s\n", strerror (err));
  47. return 1;
  48. }
  49. result = 1;
  50. err = pthread_create (&th, NULL, tf, (void *) (long int) key);
  51. if (err != 0)
  52. {
  53. printf ("create failed: %s\n", strerror (err));
  54. return 1;
  55. }
  56. /* Wait for the thread to terminate. */
  57. err = pthread_join (th, NULL);
  58. if (err != 0)
  59. {
  60. printf ("join failed: %s\n", strerror (err));
  61. return 1;
  62. }
  63. if (result == 1)
  64. puts ("destructor not called");
  65. else if (result == 2)
  66. puts ("destructor got passed a wrong value");
  67. else if (result == 3)
  68. puts ("setspecific in child failed");
  69. else if (result != 0)
  70. puts ("result != 0");
  71. return result;
  72. }
  73. #define TEST_FUNCTION do_test ()
  74. #include "../test-skeleton.c"