example-small.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * This is a very small example that shows how to use
  3. * protothreads. The program consists of two protothreads that wait
  4. * for each other to toggle a variable.
  5. */
  6. /* We must always include pt.h in our protothreads code. */
  7. #include "pt.h"
  8. #include <stdio.h> /* For printf(). */
  9. /* Two flags that the two protothread functions use. */
  10. static int protothread1_flag, protothread2_flag;
  11. /**
  12. * The first protothread function. A protothread function must always
  13. * return an integer, but must never explicitly return - returning is
  14. * performed inside the protothread statements.
  15. *
  16. * The protothread function is driven by the main loop further down in
  17. * the code.
  18. */
  19. static int
  20. protothread1(struct pt *pt)
  21. {
  22. /* A protothread function must begin with PT_BEGIN() which takes a
  23. pointer to a struct pt. */
  24. PT_BEGIN(pt);
  25. /* We loop forever here. */
  26. while(1) {
  27. /* Wait until the other protothread has set its flag. */
  28. PT_WAIT_UNTIL(pt, protothread2_flag != 0);
  29. printf("Protothread 1 running\n");
  30. /* We then reset the other protothread's flag, and set our own
  31. flag so that the other protothread can run. */
  32. protothread2_flag = 0;
  33. protothread1_flag = 1;
  34. /* And we loop. */
  35. }
  36. /* All protothread functions must end with PT_END() which takes a
  37. pointer to a struct pt. */
  38. PT_END(pt);
  39. }
  40. /**
  41. * The second protothread function. This is almost the same as the
  42. * first one.
  43. */
  44. static int
  45. protothread2(struct pt *pt)
  46. {
  47. PT_BEGIN(pt);
  48. while(1) {
  49. /* Let the other protothread run. */
  50. protothread2_flag = 1;
  51. /* Wait until the other protothread has set its flag. */
  52. PT_WAIT_UNTIL(pt, protothread1_flag != 0);
  53. printf("Protothread 2 running\n");
  54. /* We then reset the other protothread's flag. */
  55. protothread1_flag = 0;
  56. /* And we loop. */
  57. }
  58. PT_END(pt);
  59. }
  60. /**
  61. * Finally, we have the main loop. Here is where the protothreads are
  62. * initialized and scheduled. First, however, we define the
  63. * protothread state variables pt1 and pt2, which hold the state of
  64. * the two protothreads.
  65. */
  66. static struct pt pt1, pt2;
  67. int
  68. main(void)
  69. {
  70. /* Initialize the protothread state variables with PT_INIT(). */
  71. PT_INIT(&pt1);
  72. PT_INIT(&pt2);
  73. /*
  74. * Then we schedule the two protothreads by repeatedly calling their
  75. * protothread functions and passing a pointer to the protothread
  76. * state variables as arguments.
  77. */
  78. while(1) {
  79. protothread1(&pt1);
  80. protothread2(&pt2);
  81. }
  82. }