mutex.c 637 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. FILE *f = fopen("stdio/stdio.in", "r");
  6. ERROR_IF(fopen, f, == NULL);
  7. flockfile(f);
  8. // Commenting this out should cause a deadlock:
  9. // flockfile(f);
  10. if (!ftrylockfile(f)) {
  11. puts("Mutex unlocked but it shouldn't be");
  12. exit(EXIT_FAILURE);
  13. }
  14. funlockfile(f);
  15. if (ftrylockfile(f)) {
  16. puts("Mutex locked but it shouldn't be");
  17. exit(EXIT_FAILURE);
  18. }
  19. if (!ftrylockfile(f)) {
  20. puts("Mutex unlocked but it shouldn't be");
  21. exit(EXIT_FAILURE);
  22. }
  23. funlockfile(f);
  24. }