pt.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 Contiki operating system.
  30. *
  31. * Author: Adam Dunkels <adam@sics.se>
  32. *
  33. * $Id: pt.h,v 1.7 2006/10/02 07:52:56 adam Exp $
  34. */
  35. /**
  36. * \addtogroup pt
  37. * @{
  38. */
  39. /**
  40. * \file
  41. * Protothreads implementation.
  42. * \author
  43. * Adam Dunkels <adam@sics.se>
  44. *
  45. */
  46. #ifndef __PT_H__
  47. #define __PT_H__
  48. #include "lc.h"
  49. struct pt {
  50. lc_t lc;
  51. };
  52. #define PT_WAITING 0
  53. #define PT_YIELDED 1
  54. #define PT_EXITED 2
  55. #define PT_ENDED 3
  56. /**
  57. * \name Initialization
  58. * @{
  59. */
  60. /**
  61. * Initialize a protothread.
  62. *
  63. * Initializes a protothread. Initialization must be done prior to
  64. * starting to execute the protothread.
  65. *
  66. * \param pt A pointer to the protothread control structure.
  67. *
  68. * \sa PT_SPAWN()
  69. *
  70. * \hideinitializer
  71. */
  72. #define PT_INIT(pt) LC_INIT((pt)->lc)
  73. /** @} */
  74. /**
  75. * \name Declaration and definition
  76. * @{
  77. */
  78. /**
  79. * Declaration of a protothread.
  80. *
  81. * This macro is used to declare a protothread. All protothreads must
  82. * be declared with this macro.
  83. *
  84. * \param name_args The name and arguments of the C function
  85. * implementing the protothread.
  86. *
  87. * \hideinitializer
  88. */
  89. #define PT_THREAD(name_args) char name_args
  90. /**
  91. * Declare the start of a protothread inside the C function
  92. * implementing the protothread.
  93. *
  94. * This macro is used to declare the starting point of a
  95. * protothread. It should be placed at the start of the function in
  96. * which the protothread runs. All C statements above the PT_BEGIN()
  97. * invokation will be executed each time the protothread is scheduled.
  98. *
  99. * \param pt A pointer to the protothread control structure.
  100. *
  101. * \hideinitializer
  102. */
  103. #define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
  104. /**
  105. * Declare the end of a protothread.
  106. *
  107. * This macro is used for declaring that a protothread ends. It must
  108. * always be used together with a matching PT_BEGIN() macro.
  109. *
  110. * \param pt A pointer to the protothread control structure.
  111. *
  112. * \hideinitializer
  113. */
  114. #define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
  115. PT_INIT(pt); return PT_ENDED; }
  116. /** @} */
  117. /**
  118. * \name Blocked wait
  119. * @{
  120. */
  121. /**
  122. * Block and wait until condition is true.
  123. *
  124. * This macro blocks the protothread until the specified condition is
  125. * true.
  126. *
  127. * \param pt A pointer to the protothread control structure.
  128. * \param condition The condition.
  129. *
  130. * \hideinitializer
  131. */
  132. #define PT_WAIT_UNTIL(pt, condition) \
  133. do { \
  134. LC_SET((pt)->lc); \
  135. if(!(condition)) { \
  136. return PT_WAITING; \
  137. } \
  138. } while(0)
  139. /**
  140. * Block and wait while condition is true.
  141. *
  142. * This function blocks and waits while condition is true. See
  143. * PT_WAIT_UNTIL().
  144. *
  145. * \param pt A pointer to the protothread control structure.
  146. * \param cond The condition.
  147. *
  148. * \hideinitializer
  149. */
  150. #define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
  151. /** @} */
  152. /**
  153. * \name Hierarchical protothreads
  154. * @{
  155. */
  156. /**
  157. * Block and wait until a child protothread completes.
  158. *
  159. * This macro schedules a child protothread. The current protothread
  160. * will block until the child protothread completes.
  161. *
  162. * \note The child protothread must be manually initialized with the
  163. * PT_INIT() function before this function is used.
  164. *
  165. * \param pt A pointer to the protothread control structure.
  166. * \param thread The child protothread with arguments
  167. *
  168. * \sa PT_SPAWN()
  169. *
  170. * \hideinitializer
  171. */
  172. #define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
  173. /**
  174. * Spawn a child protothread and wait until it exits.
  175. *
  176. * This macro spawns a child protothread and waits until it exits. The
  177. * macro can only be used within a protothread.
  178. *
  179. * \param pt A pointer to the protothread control structure.
  180. * \param child A pointer to the child protothread's control structure.
  181. * \param thread The child protothread with arguments
  182. *
  183. * \hideinitializer
  184. */
  185. #define PT_SPAWN(pt, child, thread) \
  186. do { \
  187. PT_INIT((child)); \
  188. PT_WAIT_THREAD((pt), (thread)); \
  189. } while(0)
  190. /** @} */
  191. /**
  192. * \name Exiting and restarting
  193. * @{
  194. */
  195. /**
  196. * Restart the protothread.
  197. *
  198. * This macro will block and cause the running protothread to restart
  199. * its execution at the place of the PT_BEGIN() call.
  200. *
  201. * \param pt A pointer to the protothread control structure.
  202. *
  203. * \hideinitializer
  204. */
  205. #define PT_RESTART(pt) \
  206. do { \
  207. PT_INIT(pt); \
  208. return PT_WAITING; \
  209. } while(0)
  210. /**
  211. * Exit the protothread.
  212. *
  213. * This macro causes the protothread to exit. If the protothread was
  214. * spawned by another protothread, the parent protothread will become
  215. * unblocked and can continue to run.
  216. *
  217. * \param pt A pointer to the protothread control structure.
  218. *
  219. * \hideinitializer
  220. */
  221. #define PT_EXIT(pt) \
  222. do { \
  223. PT_INIT(pt); \
  224. return PT_EXITED; \
  225. } while(0)
  226. /** @} */
  227. /**
  228. * \name Calling a protothread
  229. * @{
  230. */
  231. /**
  232. * Schedule a protothread.
  233. *
  234. * This function shedules a protothread. The return value of the
  235. * function is non-zero if the protothread is running or zero if the
  236. * protothread has exited.
  237. *
  238. * \param f The call to the C function implementing the protothread to
  239. * be scheduled
  240. *
  241. * \hideinitializer
  242. */
  243. #define PT_SCHEDULE(f) ((f) < PT_EXITED)
  244. /** @} */
  245. /**
  246. * \name Yielding from a protothread
  247. * @{
  248. */
  249. /**
  250. * Yield from the current protothread.
  251. *
  252. * This function will yield the protothread, thereby allowing other
  253. * processing to take place in the system.
  254. *
  255. * \param pt A pointer to the protothread control structure.
  256. *
  257. * \hideinitializer
  258. */
  259. #define PT_YIELD(pt) \
  260. do { \
  261. PT_YIELD_FLAG = 0; \
  262. LC_SET((pt)->lc); \
  263. if(PT_YIELD_FLAG == 0) { \
  264. return PT_YIELDED; \
  265. } \
  266. } while(0)
  267. /**
  268. * \brief Yield from the protothread until a condition occurs.
  269. * \param pt A pointer to the protothread control structure.
  270. * \param cond The condition.
  271. *
  272. * This function will yield the protothread, until the
  273. * specified condition evaluates to true.
  274. *
  275. *
  276. * \hideinitializer
  277. */
  278. #define PT_YIELD_UNTIL(pt, cond) \
  279. do { \
  280. PT_YIELD_FLAG = 0; \
  281. LC_SET((pt)->lc); \
  282. if((PT_YIELD_FLAG == 0) || !(cond)) { \
  283. return PT_YIELDED; \
  284. } \
  285. } while(0)
  286. /** @} */
  287. #endif /* __PT_H__ */
  288. /** @} */