tst-tsd6.c 1.5 KB

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