tst-tss-basic.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* C11 threads specific storage tests.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <threads.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <support/check.h>
  19. /* Thread specific storage. */
  20. static tss_t key;
  21. #define TSS_VALUE (void*) 0xFF
  22. static int
  23. tss_thrd (void *arg)
  24. {
  25. if (tss_create (&key, NULL) != thrd_success)
  26. FAIL_EXIT1 ("tss_create failed");
  27. if (tss_set (key, TSS_VALUE))
  28. FAIL_EXIT1 ("tss_set failed");
  29. void *value = tss_get (key);
  30. if (value == 0)
  31. FAIL_EXIT1 ("tss_get failed");
  32. if (value != TSS_VALUE)
  33. FAIL_EXIT1 ("tss_get returned %p, expected %p", value, TSS_VALUE);
  34. thrd_exit (thrd_success);
  35. }
  36. static int
  37. do_test (void)
  38. {
  39. /* Setting an invalid key should return an error. */
  40. if (tss_set (key, TSS_VALUE) == thrd_success)
  41. FAIL_EXIT1 ("tss_set succeed where it should have failed");
  42. if (tss_create (&key, NULL) != thrd_success)
  43. FAIL_EXIT1 ("tss_create failed");
  44. thrd_t id;
  45. if (thrd_create (&id, tss_thrd, NULL) != thrd_success)
  46. FAIL_EXIT1 ("thrd_create failed");
  47. if (thrd_join (id, NULL) != thrd_success)
  48. FAIL_EXIT1 ("thrd failed");
  49. /* The value set in tss_thrd should not be visible here. */
  50. void *value = tss_get (key);
  51. if (value != 0)
  52. FAIL_EXIT1 ("tss_get succeed where it should have failed");
  53. tss_delete (key);
  54. return 0;
  55. }
  56. #include <support/test-driver.c>