tst-key1.c 2.2 KB

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