example-buffer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2004-2005, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the protothreads library.
  30. *
  31. * Author: Adam Dunkels <adam@sics.se>
  32. *
  33. * $Id: example-buffer.c,v 1.5 2005/10/07 05:21:33 adam Exp $
  34. */
  35. #ifdef _WIN32
  36. #include <windows.h>
  37. #else
  38. #include <unistd.h>
  39. #endif
  40. #include <stdio.h>
  41. #include "pt-sem.h"
  42. #define NUM_ITEMS 32
  43. #define BUFSIZE 8
  44. static int buffer[BUFSIZE];
  45. static int bufptr;
  46. static void
  47. add_to_buffer(int item)
  48. {
  49. printf("Item %d added to buffer at place %d\n", item, bufptr);
  50. buffer[bufptr] = item;
  51. bufptr = (bufptr + 1) % BUFSIZE;
  52. }
  53. static int
  54. get_from_buffer(void)
  55. {
  56. int item;
  57. item = buffer[bufptr];
  58. printf("Item %d retrieved from buffer at place %d\n",
  59. item, bufptr);
  60. bufptr = (bufptr + 1) % BUFSIZE;
  61. return item;
  62. }
  63. static int
  64. produce_item(void)
  65. {
  66. static int item = 0;
  67. printf("Item %d produced\n", item);
  68. return item++;
  69. }
  70. static void
  71. consume_item(int item)
  72. {
  73. printf("Item %d consumed\n", item);
  74. }
  75. static struct pt_sem full, empty;
  76. static
  77. PT_THREAD(producer(struct pt *pt))
  78. {
  79. static int produced;
  80. PT_BEGIN(pt);
  81. for(produced = 0; produced < NUM_ITEMS; ++produced) {
  82. PT_SEM_WAIT(pt, &full);
  83. add_to_buffer(produce_item());
  84. PT_SEM_SIGNAL(pt, &empty);
  85. }
  86. PT_END(pt);
  87. }
  88. static
  89. PT_THREAD(consumer(struct pt *pt))
  90. {
  91. static int consumed;
  92. PT_BEGIN(pt);
  93. for(consumed = 0; consumed < NUM_ITEMS; ++consumed) {
  94. PT_SEM_WAIT(pt, &empty);
  95. consume_item(get_from_buffer());
  96. PT_SEM_SIGNAL(pt, &full);
  97. }
  98. PT_END(pt);
  99. }
  100. static
  101. PT_THREAD(driver_thread(struct pt *pt))
  102. {
  103. static struct pt pt_producer, pt_consumer;
  104. PT_BEGIN(pt);
  105. PT_SEM_INIT(&empty, 0);
  106. PT_SEM_INIT(&full, BUFSIZE);
  107. PT_INIT(&pt_producer);
  108. PT_INIT(&pt_consumer);
  109. PT_WAIT_THREAD(pt, producer(&pt_producer) &
  110. consumer(&pt_consumer));
  111. PT_END(pt);
  112. }
  113. int
  114. main(void)
  115. {
  116. struct pt driver_pt;
  117. PT_INIT(&driver_pt);
  118. while(PT_SCHEDULE(driver_thread(&driver_pt))) {
  119. /*
  120. * When running this example on a multitasking system, we must
  121. * give other processes a chance to run too and therefore we call
  122. * usleep() resp. Sleep() here. On a dedicated embedded system,
  123. * we usually do not need to do this.
  124. */
  125. #ifdef _WIN32
  126. Sleep(0);
  127. #else
  128. usleep(10);
  129. #endif
  130. }
  131. return 0;
  132. }