Adjust mutex test
This commit is contained in:
+38
-21
@@ -1,30 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void) {
|
||||
FILE *f = fopen("stdio/stdio.in", "r");
|
||||
ERROR_IF(fopen, f, == NULL);
|
||||
FILE *f;
|
||||
|
||||
flockfile(f);
|
||||
void* thread_wontlock(void* arg) {
|
||||
(void)arg;
|
||||
|
||||
// Commenting this out should cause a deadlock:
|
||||
// flockfile(f);
|
||||
int result = ftrylockfile(f);
|
||||
UNEXP_IF(ftrylockfile, result, == 0);
|
||||
|
||||
if (!ftrylockfile(f)) {
|
||||
puts("Mutex unlocked but it shouldn't be");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
funlockfile(f);
|
||||
|
||||
if (ftrylockfile(f)) {
|
||||
puts("Mutex locked but it shouldn't be");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!ftrylockfile(f)) {
|
||||
puts("Mutex unlocked but it shouldn't be");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
funlockfile(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* thread_willlock(void* arg) {
|
||||
(void)arg;
|
||||
|
||||
int result = ftrylockfile(f);
|
||||
UNEXP_IF(ftrylockfile, result, != 0);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
f = fopen("stdio/stdio.in", "r");
|
||||
ERROR_IF(fopen, f, == NULL);
|
||||
flockfile(f);
|
||||
flockfile(f);
|
||||
thread_willlock(NULL);
|
||||
|
||||
pthread_t thread;
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
pthread_create(&thread, NULL, thread_wontlock, NULL);
|
||||
pthread_join(thread, NULL);
|
||||
funlockfile(f);
|
||||
}
|
||||
|
||||
pthread_create(&thread, NULL, thread_willlock, NULL);
|
||||
pthread_join(thread, NULL);
|
||||
|
||||
// TODO: glibc refuses to quit test without this
|
||||
// but in relibc, it will result in EPERM
|
||||
// funlockfile(f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user