thread.c 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <lib9.h>
  3. #include <thread.h>
  4. enum
  5. {
  6. STACK = 2048,
  7. };
  8. void
  9. clockproc(void *arg)
  10. {
  11. int t;
  12. Channel *c;
  13. c = arg;
  14. for(t=0;; t++){
  15. sleep(100);
  16. sendul(c, t);
  17. }
  18. }
  19. void
  20. threadmain(int argc, char *argv[])
  21. {
  22. int tick, tock;
  23. Alt a[] = {
  24. {nil, &tick, CHANRCV},
  25. {nil, &tock, CHANRCV},
  26. {nil, nil, CHANEND},
  27. };
  28. fprint(2, "threadmain hi!\n");
  29. a[0].c = chancreate(sizeof tick, 0);
  30. a[1].c = chancreate(sizeof tock, 0);
  31. proccreate(clockproc, a[0].c, STACK);
  32. proccreate(clockproc, a[1].c, STACK);
  33. tick = 0;
  34. tock = 0;
  35. while(tick < 10 || tock < 10){
  36. switch(alt(a)){
  37. case 0:
  38. fprint(2, "tick %d\n", tick);
  39. break;
  40. case 1:
  41. fprint(2, "tock %d\n", tock);
  42. break;
  43. default:
  44. sysfatal("can't happen");
  45. }
  46. }
  47. print("PASS\n");
  48. threadexitsall("PASS");
  49. }