tst-robust9.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <sys/time.h>
  7. static pthread_mutex_t m;
  8. static void *
  9. tf (void *data)
  10. {
  11. int err = pthread_mutex_lock (&m);
  12. if (err == EOWNERDEAD)
  13. {
  14. err = pthread_mutex_consistent_np (&m);
  15. if (err)
  16. {
  17. puts ("pthread_mutex_consistent_np");
  18. exit (1);
  19. }
  20. }
  21. else if (err)
  22. {
  23. puts ("pthread_mutex_lock");
  24. exit (1);
  25. }
  26. printf ("thread%ld got the lock.\n", (long int) data);
  27. sleep (1);
  28. /* exit without unlock */
  29. return NULL;
  30. }
  31. static int
  32. do_test (void)
  33. {
  34. int err, i;
  35. pthread_t t[3];
  36. pthread_mutexattr_t ma;
  37. pthread_mutexattr_init (&ma);
  38. err = pthread_mutexattr_setrobust_np (&ma, PTHREAD_MUTEX_ROBUST_NP);
  39. if (err)
  40. {
  41. puts ("pthread_mutexattr_setrobust_np");
  42. return 1;
  43. }
  44. #ifdef ENABLE_PI
  45. if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT) != 0)
  46. {
  47. puts ("pthread_mutexattr_setprotocol failed");
  48. return 1;
  49. }
  50. #endif
  51. err = pthread_mutex_init (&m, &ma);
  52. #ifdef ENABLE_PI
  53. if (err == ENOTSUP)
  54. {
  55. puts ("PI robust mutexes not supported");
  56. return 0;
  57. }
  58. #endif
  59. if (err)
  60. {
  61. puts ("pthread_mutex_init");
  62. return 1;
  63. }
  64. for (i = 0; i < sizeof (t) / sizeof (t[0]); i++)
  65. {
  66. err = pthread_create (&t[i], NULL, tf, (void *) (long int) i);
  67. if (err)
  68. {
  69. puts ("pthread_create");
  70. return 1;
  71. }
  72. }
  73. for (i = 0; i < sizeof (t) / sizeof (t[0]); i++)
  74. {
  75. err = pthread_join (t[i], NULL);
  76. if (err)
  77. {
  78. puts ("pthread_join");
  79. return 1;
  80. }
  81. }
  82. return 0;
  83. }
  84. #define TEST_FUNCTION do_test ()
  85. #include "../test-skeleton.c"