From ef6aa99b743a33b20f6b04b199c9d98b1230e578 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Wed, 16 Aug 2017 00:37:39 +0200 Subject: [PATCH] qa: libc: slow note handlers cant't break sleep --- qa/lib/c/build.json | 1 + qa/lib/c/sleep2.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 qa/lib/c/sleep2.c diff --git a/qa/lib/c/build.json b/qa/lib/c/build.json index 9a058dd..1271f7d 100644 --- a/qa/lib/c/build.json +++ b/qa/lib/c/build.json @@ -32,6 +32,7 @@ "rwakeup0.c", "rwakeup1.c", "sleep.c", + "sleep2.c", "sqrt.c", "sysfatal.c", "va_copy.c", diff --git a/qa/lib/c/sleep2.c b/qa/lib/c/sleep2.c new file mode 100644 index 0000000..5dc8c5b --- /dev/null +++ b/qa/lib/c/sleep2.c @@ -0,0 +1,83 @@ +/* + * This file is part of Jehanne. + * + * Copyright (C) 2017 Giacomo Tesio + * + * Jehanne is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * Jehanne is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jehanne. If not, see . + */ +#include +#include + +/* Test sleep(2): notes during a sleep don't break it even if note + * handler takes a while + */ +int verbose = 0; + +int +printFirst(void *v, char *s) +{ + /* just not exit, please */ + if(strcmp(s, "alarm") == 0){ + if(verbose) + fprint(2, "%d: noted: %s at %lld\n", getpid(), s, nsec()); + atnotify(printFirst, 0); + sleep(2000); + return 1; + } + return 0; +} + +int +failOnSecond(void *v, char *s) +{ + /* just not exit, please */ + if(strcmp(s, "alarm") == 0){ + print("FAIL: timedout\n"); + exits("FAIL"); + } + return 0; +} + +void +main(void) +{ + int64_t a2000, a500, tStart, tEnd; + if (!atnotify(printFirst, 1) || !atnotify(failOnSecond, 1)){ + fprint(2, "%r\n"); + exits("atnotify fails"); + } + + alarm(5000); + a2000 = nsec(); + alarm(500); + a500 = nsec(); + tStart = nsec(); + sleep(1000); + tEnd = nsec(); + + if(verbose) + fprint(2, "%d: set alarm(2000)@%lld then alarm(500)@%lld; elapsed in sleep() %lld nanosecond\n", getpid(), a2000, a500, tEnd-tStart); + + if((tEnd-tStart)/(1000 * 1000) > 500+2000+400 /* 400ms = acceptable error */){ + print("FAIL: should sleep less\n"); + exits("FAIL"); + } + + if((tEnd-tStart)/(1000 * 1000) < 500+2000-400 /* 400ms = acceptable error */){ + print("FAIL: should sleep more\n"); + exits("FAIL"); + } + + print("PASS\n"); + exits("PASS"); +}