tst-tsd6.c 1.7 KB

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