1
0

rwakeup0.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* verify that rwakeup returns 0 on Rendez that has already been awaken */
  21. Rendez r;
  22. QLock l;
  23. int ready;
  24. int verbose = 0;
  25. int killerProc; /* pid, will kill the other processes if starved */
  26. int
  27. handletimeout(void *v, char *s)
  28. {
  29. /* just not exit, please */
  30. if(strcmp(s, "timedout") == 0){
  31. if(verbose)
  32. print("%d: noted: %s\n", getpid(), s);
  33. print("FAIL: %s timedout\n", argv0);
  34. exits("FAIL");
  35. }
  36. return 0;
  37. }
  38. void
  39. killKiller(void)
  40. {
  41. postnote(PNPROC, killerProc, "interrupt");
  42. }
  43. void
  44. stopAllAfter(int seconds)
  45. {
  46. int pid;
  47. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  48. {
  49. case 0:
  50. if(verbose)
  51. print("killer proc started: pid %d\n", getpid());
  52. sleep(seconds * 1000);
  53. postnote(PNGROUP, killerProc, "timedout");
  54. if(verbose)
  55. print("killer proc timedout: pid %d\n", getpid());
  56. exits("FAIL");
  57. case -1:
  58. fprint(2, "%r\n");
  59. exits("sys_rfork fails");
  60. default:
  61. killerProc = pid;
  62. atexit(killKiller);
  63. }
  64. }
  65. void
  66. main(int argc, char* argv[])
  67. {
  68. int s, w;
  69. ARGBEGIN{
  70. }ARGEND;
  71. sys_rfork(RFNOTEG|RFREND);
  72. if (!atnotify(handletimeout, 1)){
  73. fprint(2, "%r\n");
  74. exits("atnotify fails");
  75. }
  76. r.l = &l;
  77. stopAllAfter(30);
  78. /* one process to sleep */
  79. switch((s = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  80. {
  81. case 0:
  82. qlock(&l);
  83. ready = 1;
  84. rsleep(&r);
  85. qunlock(&l);
  86. exits(nil);
  87. break;
  88. case -1:
  89. print("sys_rfork: %r\n");
  90. exits("sys_rfork fails");
  91. break;
  92. default:
  93. while(ready == 0)
  94. ;
  95. break;
  96. }
  97. /* one process to wakeup */
  98. switch((w = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  99. {
  100. case 0:
  101. qlock(&l);
  102. rwakeup(&r);
  103. qunlock(&l);
  104. ready = 2;
  105. exits(nil);
  106. break;
  107. case -1:
  108. print("sys_rfork: %r\n");
  109. exits("sys_rfork fails");
  110. break;
  111. default:
  112. while(ready == 1)
  113. ;
  114. break;
  115. }
  116. /* now, we try to wakeup a free Rendez */
  117. qlock(&l);
  118. if(rwakeup(&r) == 0){
  119. qunlock(&l);
  120. print("PASS\n");
  121. exits("PASS");
  122. }
  123. if((s = sys_open(smprint("/proc/%d/ctl", s), OWRITE)) >= 0){
  124. jehanne_write(s, "kill", 5);
  125. sys_close(s);
  126. }
  127. if((w = sys_open(smprint("/proc/%d/ctl", w), OWRITE)) >= 0){
  128. jehanne_write(s, "kill", 5);
  129. sys_close(s);
  130. }
  131. print("FAIL\n");
  132. exits("FAIL");
  133. }