awake.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2015 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. int verbose = 1;
  21. int
  22. failOnTimeout(void *v, char *s)
  23. {
  24. if(strncmp(s, "alarm", 4) == 0){
  25. if(verbose)
  26. print("%d: noted: %s\n", getpid(), s);
  27. print("FAIL: timeout\n");
  28. exits("FAIL");
  29. }
  30. return 0;
  31. }
  32. int
  33. writeTillBlock(int fd)
  34. {
  35. int i = 0;
  36. char buf[1025];
  37. memset(buf, 1, sizeof(buf));
  38. while(i < 300) /* pipes should block after at most 256kb */
  39. {
  40. if(jehanne_write(fd, buf, sizeof(buf)) < 0){
  41. break;
  42. }
  43. ++i;
  44. }
  45. return i;
  46. }
  47. void
  48. main(void)
  49. {
  50. int64_t start, elapsed, wkup, res;
  51. int32_t sem = 0;
  52. int fds[2];
  53. char buf[1];
  54. sys_semacquire(&sem, 0);
  55. sys_alarm(40000); /* global timeout, FAIL if reached */
  56. if (!atnotify(failOnTimeout, 1)){
  57. fprint(2, "%r\n");
  58. exits("atnotify fails");
  59. }
  60. sys_awake(100000); /* this will be handled by the kernel, see pexit */
  61. /* verify that rendezvous are interrupted */
  62. fprint(2, "verify that rendezvous are interrupted\n", elapsed);
  63. wkup = sys_awake(1000);
  64. start = nsec();
  65. res = (int64_t)sys_rendezvous(&elapsed, (void*)0x12345);
  66. elapsed = (nsec() - start) / (1000 * 1000);
  67. if(verbose)
  68. fprint(2, "rendezvous interrupted, returned %#p, elapsed = %d ms\n", res, elapsed);
  69. if(!awakened(wkup) || elapsed < 900 || elapsed > 1800){
  70. print("FAIL: rendezvous\n");
  71. exits("FAIL");
  72. }
  73. forgivewkp(wkup);
  74. /* verify that sleeps are NOT interrupted */
  75. fprint(2, "verify that sleeps are NOT interrupted\n", elapsed);
  76. wkup = sys_awake(700);
  77. start = nsec();
  78. sleep(1500);
  79. elapsed = (nsec() - start) / (1000 * 1000);
  80. if(verbose)
  81. fprint(2, "sleep(1500), elapsed = %d ms\n", elapsed);
  82. if(elapsed < 1300){
  83. print("FAIL: sleep\n");
  84. exits("FAIL");
  85. }
  86. forgivewkp(wkup);
  87. /* verify that semacquires are interrupted */
  88. fprint(2, "verify that semacquires are interrupted\n", elapsed);
  89. pipe(fds);
  90. wkup = sys_awake(1000);
  91. start = nsec();
  92. if(verbose)
  93. print("semacquire(&sem, 1)...\n");
  94. res = sys_semacquire(&sem, 1);
  95. elapsed = (nsec() - start) / (1000 * 1000);
  96. if(verbose)
  97. print("semacquire(&sem, 1): returned %lld, elapsed = %d ms\n", res, elapsed);
  98. if(!awakened(wkup) || res != -1 || elapsed < 900 || elapsed > 1800){
  99. print("FAIL: semacquire\n");
  100. exits("FAIL");
  101. }
  102. forgivewkp(wkup);
  103. /* verify that tsemacquire are NOT interrupted */
  104. fprint(2, "verify that tsemacquire are NOT interrupted\n", elapsed);
  105. start = nsec();
  106. wkup = sys_awake(500);
  107. res = tsemacquire(&sem, 1500);
  108. elapsed = (nsec() - start) / (1000 * 1000);
  109. if(verbose)
  110. fprint(2, "tsemacquire(&sem, 1500) returned %lld, elapsed = %d ms\n", res, elapsed);
  111. if(res != 0 || elapsed < 1300){
  112. print("FAIL: tsemacquire\n");
  113. exits("FAIL");
  114. }
  115. forgivewkp(wkup);
  116. /* verify that reads are interrupted */
  117. fprint(2, "verify that reads are interrupted\n", elapsed);
  118. if(pipe(fds) < 0){
  119. print("FAIL: pipe\n");
  120. exits("FAIL");
  121. }
  122. wkup = sys_awake(1000);
  123. start = nsec();
  124. res = jehanne_read(fds[0], buf, 1);
  125. elapsed = (nsec() - start) / (1000 * 1000);
  126. if(verbose)
  127. fprint(2, "read(fds[0], buf, 1) returned %lld, elapsed = %d ms\n", res, elapsed);
  128. if(!awakened(wkup) || res != -1 || elapsed < 900 || elapsed > 1800){
  129. print("FAIL: read\n");
  130. exits("FAIL");
  131. }
  132. forgivewkp(wkup);
  133. /* verify that writes are interrupted */
  134. fprint(2, "verify that writes are interrupted\n", elapsed);
  135. if(pipe(fds) < 0){
  136. print("FAIL: pipe\n");
  137. exits("FAIL");
  138. }
  139. wkup = sys_awake(1000);
  140. start = nsec();
  141. res = writeTillBlock(fds[0]);
  142. elapsed = (nsec() - start) / (1000 * 1000);
  143. if(verbose)
  144. fprint(2, "writeTillBlock(fds[0]) returned %lld, elapsed = %d ms\n", res, elapsed);
  145. if(!awakened(wkup) || res >= 256 || elapsed < 900 || elapsed > 1800){
  146. print("FAIL: write\n");
  147. exits("FAIL");
  148. }
  149. forgivewkp(wkup);
  150. /* do not forgivewkp the awake(100000): the kernel must handle it */
  151. print("PASS\n");
  152. exits("PASS");
  153. }