pt-sem.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2004, 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: pt-sem.h,v 1.2 2005/02/24 10:36:59 adam Exp $
  34. */
  35. /**
  36. * \addtogroup pt
  37. * @{
  38. */
  39. /**
  40. * \defgroup ptsem Protothread semaphores
  41. * @{
  42. *
  43. * This module implements counting semaphores on top of
  44. * protothreads. Semaphores are a synchronization primitive that
  45. * provide two operations: "wait" and "signal". The "wait" operation
  46. * checks the semaphore counter and blocks the thread if the counter
  47. * is zero. The "signal" operation increases the semaphore counter but
  48. * does not block. If another thread has blocked waiting for the
  49. * semaphore that is signalled, the blocked thread will become
  50. * runnable again.
  51. *
  52. * Semaphores can be used to implement other, more structured,
  53. * synchronization primitives such as monitors and message
  54. * queues/bounded buffers (see below).
  55. *
  56. * The following example shows how the producer-consumer problem, also
  57. * known as the bounded buffer problem, can be solved using
  58. * protothreads and semaphores. Notes on the program follow after the
  59. * example.
  60. *
  61. \code
  62. #include "pt-sem.h"
  63. #define NUM_ITEMS 32
  64. #define BUFSIZE 8
  65. static struct pt_sem mutex, full, empty;
  66. PT_THREAD(producer(struct pt *pt))
  67. {
  68. static int produced;
  69. PT_BEGIN(pt);
  70. for(produced = 0; produced < NUM_ITEMS; ++produced) {
  71. PT_SEM_WAIT(pt, &full);
  72. PT_SEM_WAIT(pt, &mutex);
  73. add_to_buffer(produce_item());
  74. PT_SEM_SIGNAL(pt, &mutex);
  75. PT_SEM_SIGNAL(pt, &empty);
  76. }
  77. PT_END(pt);
  78. }
  79. PT_THREAD(consumer(struct pt *pt))
  80. {
  81. static int consumed;
  82. PT_BEGIN(pt);
  83. for(consumed = 0; consumed < NUM_ITEMS; ++consumed) {
  84. PT_SEM_WAIT(pt, &empty);
  85. PT_SEM_WAIT(pt, &mutex);
  86. consume_item(get_from_buffer());
  87. PT_SEM_SIGNAL(pt, &mutex);
  88. PT_SEM_SIGNAL(pt, &full);
  89. }
  90. PT_END(pt);
  91. }
  92. PT_THREAD(driver_thread(struct pt *pt))
  93. {
  94. static struct pt pt_producer, pt_consumer;
  95. PT_BEGIN(pt);
  96. PT_SEM_INIT(&empty, 0);
  97. PT_SEM_INIT(&full, BUFSIZE);
  98. PT_SEM_INIT(&mutex, 1);
  99. PT_INIT(&pt_producer);
  100. PT_INIT(&pt_consumer);
  101. PT_WAIT_THREAD(pt, producer(&pt_producer) &
  102. consumer(&pt_consumer));
  103. PT_END(pt);
  104. }
  105. \endcode
  106. *
  107. * The program uses three protothreads: one protothread that
  108. * implements the consumer, one thread that implements the producer,
  109. * and one protothread that drives the two other protothreads. The
  110. * program uses three semaphores: "full", "empty" and "mutex". The
  111. * "mutex" semaphore is used to provide mutual exclusion for the
  112. * buffer, the "empty" semaphore is used to block the consumer is the
  113. * buffer is empty, and the "full" semaphore is used to block the
  114. * producer is the buffer is full.
  115. *
  116. * The "driver_thread" holds two protothread state variables,
  117. * "pt_producer" and "pt_consumer". It is important to note that both
  118. * these variables are declared as <i>static</i>. If the static
  119. * keyword is not used, both variables are stored on the stack. Since
  120. * protothreads do not store the stack, these variables may be
  121. * overwritten during a protothread wait operation. Similarly, both
  122. * the "consumer" and "producer" protothreads declare their local
  123. * variables as static, to avoid them being stored on the stack.
  124. *
  125. *
  126. */
  127. /**
  128. * \file
  129. * Couting semaphores implemented on protothreads
  130. * \author
  131. * Adam Dunkels <adam@sics.se>
  132. *
  133. */
  134. #ifndef __PT_SEM_H__
  135. #define __PT_SEM_H__
  136. #include "pt.h"
  137. struct pt_sem {
  138. unsigned int count;
  139. };
  140. /**
  141. * Initialize a semaphore
  142. *
  143. * This macro initializes a semaphore with a value for the
  144. * counter. Internally, the semaphores use an "unsigned int" to
  145. * represent the counter, and therefore the "count" argument should be
  146. * within range of an unsigned int.
  147. *
  148. * \param s (struct pt_sem *) A pointer to the pt_sem struct
  149. * representing the semaphore
  150. *
  151. * \param c (unsigned int) The initial count of the semaphore.
  152. * \hideinitializer
  153. */
  154. #define PT_SEM_INIT(s, c) (s)->count = c
  155. /**
  156. * Wait for a semaphore
  157. *
  158. * This macro carries out the "wait" operation on the semaphore. The
  159. * wait operation causes the protothread to block while the counter is
  160. * zero. When the counter reaches a value larger than zero, the
  161. * protothread will continue.
  162. *
  163. * \param pt (struct pt *) A pointer to the protothread (struct pt) in
  164. * which the operation is executed.
  165. *
  166. * \param s (struct pt_sem *) A pointer to the pt_sem struct
  167. * representing the semaphore
  168. *
  169. * \hideinitializer
  170. */
  171. #define PT_SEM_WAIT(pt, s) \
  172. do { \
  173. PT_WAIT_UNTIL(pt, (s)->count > 0); \
  174. --(s)->count; \
  175. } while(0)
  176. /**
  177. * Signal a semaphore
  178. *
  179. * This macro carries out the "signal" operation on the semaphore. The
  180. * signal operation increments the counter inside the semaphore, which
  181. * eventually will cause waiting protothreads to continue executing.
  182. *
  183. * \param pt (struct pt *) A pointer to the protothread (struct pt) in
  184. * which the operation is executed.
  185. *
  186. * \param s (struct pt_sem *) A pointer to the pt_sem struct
  187. * representing the semaphore
  188. *
  189. * \hideinitializer
  190. */
  191. #define PT_SEM_SIGNAL(pt, s) ++(s)->count
  192. #endif /* __PT_SEM_H__ */
  193. /** @} */
  194. /** @} */