tst-tsd6.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <errno.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include "../test-skeleton.h"
  8. #define NKEYS 100
  9. static pthread_key_t keys[NKEYS];
  10. static pthread_barrier_t b;
  11. static void *
  12. tf (void *arg)
  13. {
  14. void *res = NULL;
  15. for (int i = 0; i < NKEYS; ++i)
  16. {
  17. void *p = pthread_getspecific (keys[i]);
  18. pthread_setspecific (keys[i], (void *) 7);
  19. if (p != NULL)
  20. res = p;
  21. }
  22. if (arg != NULL)
  23. {
  24. pthread_barrier_wait (arg);
  25. pthread_barrier_wait (arg);
  26. }
  27. return res;
  28. }
  29. static int
  30. do_test (void)
  31. {
  32. pthread_barrier_init (&b, NULL, 2);
  33. for (int i = 0; i < NKEYS; ++i)
  34. if (pthread_key_create (&keys[i], NULL) != 0)
  35. {
  36. puts ("cannot create keys");
  37. return 1;
  38. }
  39. pthread_t th;
  40. if (pthread_create (&th, NULL, tf, &b) != 0)
  41. {
  42. puts ("cannot create thread in parent");
  43. return 1;
  44. }
  45. pthread_barrier_wait (&b);
  46. pid_t pid = fork ();
  47. if (pid == 0)
  48. {
  49. if (pthread_create (&th, NULL, tf, NULL) != 0)
  50. {
  51. puts ("cannot create thread in child");
  52. exit (1);
  53. }
  54. void *res;
  55. pthread_join (th, &res);
  56. exit (res != NULL);
  57. }
  58. else if (pid == -1)
  59. {
  60. puts ("cannot create child process");
  61. return 1;
  62. }
  63. int s;
  64. if (TEMP_FAILURE_RETRY (waitpid (pid, &s, 0)) != pid)
  65. {
  66. puts ("failing to wait for child process");
  67. return 1;
  68. }
  69. pthread_barrier_wait (&b);
  70. pthread_join (th, NULL);
  71. return !WIFEXITED (s) ? 2 : WEXITSTATUS (s);
  72. }
  73. #define TEST_FUNCTION do_test ()
  74. #include "../test-skeleton.c"