lock-3.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* { dg-do run { target *-*-linux* *-*-gnu* *-*-freebsd* } } */
  2. #ifndef _GNU_SOURCE
  3. #define _GNU_SOURCE 1
  4. #endif
  5. #include <pthread.h>
  6. #include <omp.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. pthread_barrier_t bar;
  10. omp_nest_lock_t lock;
  11. void *tf (void *p)
  12. {
  13. int l;
  14. if (p)
  15. {
  16. if (omp_test_nest_lock (&lock) != 1)
  17. abort ();
  18. if (omp_test_nest_lock (&lock) != 2)
  19. abort ();
  20. }
  21. pthread_barrier_wait (&bar);
  22. if (!p && omp_test_nest_lock (&lock) != 0)
  23. abort ();
  24. pthread_barrier_wait (&bar);
  25. if (p)
  26. {
  27. if (omp_test_nest_lock (&lock) != 3)
  28. abort ();
  29. omp_unset_nest_lock (&lock);
  30. omp_unset_nest_lock (&lock);
  31. omp_unset_nest_lock (&lock);
  32. }
  33. pthread_barrier_wait (&bar);
  34. if (!p)
  35. {
  36. if (omp_test_nest_lock (&lock) != 1)
  37. abort ();
  38. if (omp_test_nest_lock (&lock) != 2)
  39. abort ();
  40. omp_unset_nest_lock (&lock);
  41. omp_unset_nest_lock (&lock);
  42. }
  43. return NULL;
  44. }
  45. int
  46. main (void)
  47. {
  48. pthread_t th;
  49. omp_init_nest_lock (&lock);
  50. pthread_barrier_init (&bar, NULL, 2);
  51. pthread_create (&th, NULL, tf, NULL);
  52. tf ("");
  53. pthread_join (th, NULL);
  54. omp_destroy_nest_lock (&lock);
  55. return 0;
  56. }