tst-tsd2.c 2.2 KB

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