tst-key1.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <limits.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. int
  21. do_test (void)
  22. {
  23. int max;
  24. #ifdef PTHREAD_KEYS_MAX
  25. max = PTHREAD_KEYS_MAX;
  26. #else
  27. max = _POSIX_THREAD_KEYS_MAX;
  28. #endif
  29. pthread_key_t *keys = alloca (max * sizeof (pthread_key_t));
  30. int i;
  31. for (i = 0; i < max; ++i)
  32. if (pthread_key_create (&keys[i], NULL) != 0)
  33. {
  34. write (2, "key_create failed\n", 18);
  35. _exit (1);
  36. }
  37. else
  38. {
  39. printf ("created key %d\n", i);
  40. if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
  41. {
  42. write (2, "setspecific failed\n", 19);
  43. _exit (1);
  44. }
  45. }
  46. for (i = 0; i < max; ++i)
  47. {
  48. if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
  49. {
  50. write (2, "getspecific failed\n", 19);
  51. _exit (1);
  52. }
  53. if (pthread_key_delete (keys[i]) != 0)
  54. {
  55. write (2, "key_delete failed\n", 18);
  56. _exit (1);
  57. }
  58. }
  59. /* Now it must be once again possible to allocate keys. */
  60. if (pthread_key_create (&keys[0], NULL) != 0)
  61. {
  62. write (2, "2nd key_create failed\n", 22);
  63. _exit (1);
  64. }
  65. if (pthread_key_delete (keys[0]) != 0)
  66. {
  67. write (2, "2nd key_delete failed\n", 22);
  68. _exit (1);
  69. }
  70. return 0;
  71. }
  72. #define TEST_FUNCTION do_test ()
  73. #include "../test-skeleton.c"