sleep2.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. /* Test sleep(2): notes during a sleep don't break it even if note
  21. * handler takes a while
  22. */
  23. int verbose = 0;
  24. int
  25. printFirst(void *v, char *s)
  26. {
  27. /* just not exit, please */
  28. if(strcmp(s, "alarm") == 0){
  29. if(verbose)
  30. fprint(2, "%d: noted: %s at %lld\n", getpid(), s, nsec());
  31. atnotify(printFirst, 0);
  32. sleep(2000);
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. int
  38. failOnSecond(void *v, char *s)
  39. {
  40. /* just not exit, please */
  41. if(strcmp(s, "alarm") == 0){
  42. print("FAIL: timedout\n");
  43. exits("FAIL");
  44. }
  45. return 0;
  46. }
  47. void
  48. main(void)
  49. {
  50. int64_t a2000, a500, tStart, tEnd;
  51. if (!atnotify(printFirst, 1) || !atnotify(failOnSecond, 1)){
  52. fprint(2, "%r\n");
  53. exits("atnotify fails");
  54. }
  55. sys_alarm(5000);
  56. a2000 = nsec();
  57. sys_alarm(500);
  58. a500 = nsec();
  59. tStart = nsec();
  60. sleep(1000);
  61. tEnd = nsec();
  62. if(verbose)
  63. fprint(2, "%d: set sys_alarm(2000)@%lld then sys_alarm(500)@%lld; elapsed in sleep() %lld nanosecond\n", getpid(), a2000, a500, tEnd-tStart);
  64. if((tEnd-tStart)/(1000 * 1000) > 500+2000+400 /* 400ms = acceptable error */){
  65. print("FAIL: should sleep less\n");
  66. exits("FAIL");
  67. }
  68. if((tEnd-tStart)/(1000 * 1000) < 500+2000-400 /* 400ms = acceptable error */){
  69. print("FAIL: should sleep more\n");
  70. exits("FAIL");
  71. }
  72. print("PASS\n");
  73. exits("PASS");
  74. }