Jingyuan Dong / Mbed 2 deprecated protoThread

Dependencies:   mbed

Committer:
guruimage
Date:
Wed Dec 01 02:46:33 2010 +0000
Revision:
0:9b9c31c57895
protoThread problem for hw2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
guruimage 0:9b9c31c57895 1 /*
guruimage 0:9b9c31c57895 2 * Copyright (c) 2004-2005, Swedish Institute of Computer Science.
guruimage 0:9b9c31c57895 3 * All rights reserved.
guruimage 0:9b9c31c57895 4 *
guruimage 0:9b9c31c57895 5 * Redistribution and use in source and binary forms, with or without
guruimage 0:9b9c31c57895 6 * modification, are permitted provided that the following conditions
guruimage 0:9b9c31c57895 7 * are met:
guruimage 0:9b9c31c57895 8 * 1. Redistributions of source code must retain the above copyright
guruimage 0:9b9c31c57895 9 * notice, this list of conditions and the following disclaimer.
guruimage 0:9b9c31c57895 10 * 2. Redistributions in binary form must reproduce the above copyright
guruimage 0:9b9c31c57895 11 * notice, this list of conditions and the following disclaimer in the
guruimage 0:9b9c31c57895 12 * documentation and/or other materials provided with the distribution.
guruimage 0:9b9c31c57895 13 * 3. Neither the name of the Institute nor the names of its contributors
guruimage 0:9b9c31c57895 14 * may be used to endorse or promote products derived from this software
guruimage 0:9b9c31c57895 15 * without specific prior written permission.
guruimage 0:9b9c31c57895 16 *
guruimage 0:9b9c31c57895 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
guruimage 0:9b9c31c57895 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
guruimage 0:9b9c31c57895 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
guruimage 0:9b9c31c57895 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
guruimage 0:9b9c31c57895 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
guruimage 0:9b9c31c57895 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
guruimage 0:9b9c31c57895 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
guruimage 0:9b9c31c57895 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
guruimage 0:9b9c31c57895 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
guruimage 0:9b9c31c57895 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
guruimage 0:9b9c31c57895 27 * SUCH DAMAGE.
guruimage 0:9b9c31c57895 28 *
guruimage 0:9b9c31c57895 29 * This file is part of the Contiki operating system.
guruimage 0:9b9c31c57895 30 *
guruimage 0:9b9c31c57895 31 * Author: Adam Dunkels <adam@sics.se>
guruimage 0:9b9c31c57895 32 *
guruimage 0:9b9c31c57895 33 * $Id: pt.h,v 1.7 2006/10/02 07:52:56 adam Exp $
guruimage 0:9b9c31c57895 34 */
guruimage 0:9b9c31c57895 35
guruimage 0:9b9c31c57895 36 /**
guruimage 0:9b9c31c57895 37 * \addtogroup pt
guruimage 0:9b9c31c57895 38 * @{
guruimage 0:9b9c31c57895 39 */
guruimage 0:9b9c31c57895 40
guruimage 0:9b9c31c57895 41 /**
guruimage 0:9b9c31c57895 42 * \file
guruimage 0:9b9c31c57895 43 * Protothreads implementation.
guruimage 0:9b9c31c57895 44 * \author
guruimage 0:9b9c31c57895 45 * Adam Dunkels <adam@sics.se>
guruimage 0:9b9c31c57895 46 *
guruimage 0:9b9c31c57895 47 */
guruimage 0:9b9c31c57895 48
guruimage 0:9b9c31c57895 49 #ifndef __PT_H__
guruimage 0:9b9c31c57895 50 #define __PT_H__
guruimage 0:9b9c31c57895 51
guruimage 0:9b9c31c57895 52 #include "lc.h"
guruimage 0:9b9c31c57895 53
guruimage 0:9b9c31c57895 54 struct pt {
guruimage 0:9b9c31c57895 55 lc_t lc;
guruimage 0:9b9c31c57895 56 };
guruimage 0:9b9c31c57895 57
guruimage 0:9b9c31c57895 58 #define PT_WAITING 0
guruimage 0:9b9c31c57895 59 #define PT_YIELDED 1
guruimage 0:9b9c31c57895 60 #define PT_EXITED 2
guruimage 0:9b9c31c57895 61 #define PT_ENDED 3
guruimage 0:9b9c31c57895 62
guruimage 0:9b9c31c57895 63 /**
guruimage 0:9b9c31c57895 64 * \name Initialization
guruimage 0:9b9c31c57895 65 * @{
guruimage 0:9b9c31c57895 66 */
guruimage 0:9b9c31c57895 67
guruimage 0:9b9c31c57895 68 /**
guruimage 0:9b9c31c57895 69 * Initialize a protothread.
guruimage 0:9b9c31c57895 70 *
guruimage 0:9b9c31c57895 71 * Initializes a protothread. Initialization must be done prior to
guruimage 0:9b9c31c57895 72 * starting to execute the protothread.
guruimage 0:9b9c31c57895 73 *
guruimage 0:9b9c31c57895 74 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 75 *
guruimage 0:9b9c31c57895 76 * \sa PT_SPAWN()
guruimage 0:9b9c31c57895 77 *
guruimage 0:9b9c31c57895 78 * \hideinitializer
guruimage 0:9b9c31c57895 79 */
guruimage 0:9b9c31c57895 80 #define PT_INIT(pt) LC_INIT((pt)->lc)
guruimage 0:9b9c31c57895 81
guruimage 0:9b9c31c57895 82 /** @} */
guruimage 0:9b9c31c57895 83
guruimage 0:9b9c31c57895 84 /**
guruimage 0:9b9c31c57895 85 * \name Declaration and definition
guruimage 0:9b9c31c57895 86 * @{
guruimage 0:9b9c31c57895 87 */
guruimage 0:9b9c31c57895 88
guruimage 0:9b9c31c57895 89 /**
guruimage 0:9b9c31c57895 90 * Declaration of a protothread.
guruimage 0:9b9c31c57895 91 *
guruimage 0:9b9c31c57895 92 * This macro is used to declare a protothread. All protothreads must
guruimage 0:9b9c31c57895 93 * be declared with this macro.
guruimage 0:9b9c31c57895 94 *
guruimage 0:9b9c31c57895 95 * \param name_args The name and arguments of the C function
guruimage 0:9b9c31c57895 96 * implementing the protothread.
guruimage 0:9b9c31c57895 97 *
guruimage 0:9b9c31c57895 98 * \hideinitializer
guruimage 0:9b9c31c57895 99 */
guruimage 0:9b9c31c57895 100 #define PT_THREAD(name_args) char name_args
guruimage 0:9b9c31c57895 101
guruimage 0:9b9c31c57895 102 /**
guruimage 0:9b9c31c57895 103 * Declare the start of a protothread inside the C function
guruimage 0:9b9c31c57895 104 * implementing the protothread.
guruimage 0:9b9c31c57895 105 *
guruimage 0:9b9c31c57895 106 * This macro is used to declare the starting point of a
guruimage 0:9b9c31c57895 107 * protothread. It should be placed at the start of the function in
guruimage 0:9b9c31c57895 108 * which the protothread runs. All C statements above the PT_BEGIN()
guruimage 0:9b9c31c57895 109 * invokation will be executed each time the protothread is scheduled.
guruimage 0:9b9c31c57895 110 *
guruimage 0:9b9c31c57895 111 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 112 *
guruimage 0:9b9c31c57895 113 * \hideinitializer
guruimage 0:9b9c31c57895 114 */
guruimage 0:9b9c31c57895 115 #define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
guruimage 0:9b9c31c57895 116
guruimage 0:9b9c31c57895 117 /**
guruimage 0:9b9c31c57895 118 * Declare the end of a protothread.
guruimage 0:9b9c31c57895 119 *
guruimage 0:9b9c31c57895 120 * This macro is used for declaring that a protothread ends. It must
guruimage 0:9b9c31c57895 121 * always be used together with a matching PT_BEGIN() macro.
guruimage 0:9b9c31c57895 122 *
guruimage 0:9b9c31c57895 123 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 124 *
guruimage 0:9b9c31c57895 125 * \hideinitializer
guruimage 0:9b9c31c57895 126 */
guruimage 0:9b9c31c57895 127 #define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
guruimage 0:9b9c31c57895 128 PT_INIT(pt); return PT_ENDED; }
guruimage 0:9b9c31c57895 129
guruimage 0:9b9c31c57895 130 /** @} */
guruimage 0:9b9c31c57895 131
guruimage 0:9b9c31c57895 132 /**
guruimage 0:9b9c31c57895 133 * \name Blocked wait
guruimage 0:9b9c31c57895 134 * @{
guruimage 0:9b9c31c57895 135 */
guruimage 0:9b9c31c57895 136
guruimage 0:9b9c31c57895 137 /**
guruimage 0:9b9c31c57895 138 * Block and wait until condition is true.
guruimage 0:9b9c31c57895 139 *
guruimage 0:9b9c31c57895 140 * This macro blocks the protothread until the specified condition is
guruimage 0:9b9c31c57895 141 * true.
guruimage 0:9b9c31c57895 142 *
guruimage 0:9b9c31c57895 143 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 144 * \param condition The condition.
guruimage 0:9b9c31c57895 145 *
guruimage 0:9b9c31c57895 146 * \hideinitializer
guruimage 0:9b9c31c57895 147 */
guruimage 0:9b9c31c57895 148 #define PT_WAIT_UNTIL(pt, condition) \
guruimage 0:9b9c31c57895 149 do { \
guruimage 0:9b9c31c57895 150 LC_SET((pt)->lc); \
guruimage 0:9b9c31c57895 151 if(!(condition)) { \
guruimage 0:9b9c31c57895 152 return PT_WAITING; \
guruimage 0:9b9c31c57895 153 } \
guruimage 0:9b9c31c57895 154 } while(0)
guruimage 0:9b9c31c57895 155
guruimage 0:9b9c31c57895 156 /**
guruimage 0:9b9c31c57895 157 * Block and wait while condition is true.
guruimage 0:9b9c31c57895 158 *
guruimage 0:9b9c31c57895 159 * This function blocks and waits while condition is true. See
guruimage 0:9b9c31c57895 160 * PT_WAIT_UNTIL().
guruimage 0:9b9c31c57895 161 *
guruimage 0:9b9c31c57895 162 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 163 * \param cond The condition.
guruimage 0:9b9c31c57895 164 *
guruimage 0:9b9c31c57895 165 * \hideinitializer
guruimage 0:9b9c31c57895 166 */
guruimage 0:9b9c31c57895 167 #define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
guruimage 0:9b9c31c57895 168
guruimage 0:9b9c31c57895 169 /** @} */
guruimage 0:9b9c31c57895 170
guruimage 0:9b9c31c57895 171 /**
guruimage 0:9b9c31c57895 172 * \name Hierarchical protothreads
guruimage 0:9b9c31c57895 173 * @{
guruimage 0:9b9c31c57895 174 */
guruimage 0:9b9c31c57895 175
guruimage 0:9b9c31c57895 176 /**
guruimage 0:9b9c31c57895 177 * Block and wait until a child protothread completes.
guruimage 0:9b9c31c57895 178 *
guruimage 0:9b9c31c57895 179 * This macro schedules a child protothread. The current protothread
guruimage 0:9b9c31c57895 180 * will block until the child protothread completes.
guruimage 0:9b9c31c57895 181 *
guruimage 0:9b9c31c57895 182 * \note The child protothread must be manually initialized with the
guruimage 0:9b9c31c57895 183 * PT_INIT() function before this function is used.
guruimage 0:9b9c31c57895 184 *
guruimage 0:9b9c31c57895 185 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 186 * \param thread The child protothread with arguments
guruimage 0:9b9c31c57895 187 *
guruimage 0:9b9c31c57895 188 * \sa PT_SPAWN()
guruimage 0:9b9c31c57895 189 *
guruimage 0:9b9c31c57895 190 * \hideinitializer
guruimage 0:9b9c31c57895 191 */
guruimage 0:9b9c31c57895 192 #define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
guruimage 0:9b9c31c57895 193
guruimage 0:9b9c31c57895 194 /**
guruimage 0:9b9c31c57895 195 * Spawn a child protothread and wait until it exits.
guruimage 0:9b9c31c57895 196 *
guruimage 0:9b9c31c57895 197 * This macro spawns a child protothread and waits until it exits. The
guruimage 0:9b9c31c57895 198 * macro can only be used within a protothread.
guruimage 0:9b9c31c57895 199 *
guruimage 0:9b9c31c57895 200 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 201 * \param child A pointer to the child protothread's control structure.
guruimage 0:9b9c31c57895 202 * \param thread The child protothread with arguments
guruimage 0:9b9c31c57895 203 *
guruimage 0:9b9c31c57895 204 * \hideinitializer
guruimage 0:9b9c31c57895 205 */
guruimage 0:9b9c31c57895 206 #define PT_SPAWN(pt, child, thread) \
guruimage 0:9b9c31c57895 207 do { \
guruimage 0:9b9c31c57895 208 PT_INIT((child)); \
guruimage 0:9b9c31c57895 209 PT_WAIT_THREAD((pt), (thread)); \
guruimage 0:9b9c31c57895 210 } while(0)
guruimage 0:9b9c31c57895 211
guruimage 0:9b9c31c57895 212 /** @} */
guruimage 0:9b9c31c57895 213
guruimage 0:9b9c31c57895 214 /**
guruimage 0:9b9c31c57895 215 * \name Exiting and restarting
guruimage 0:9b9c31c57895 216 * @{
guruimage 0:9b9c31c57895 217 */
guruimage 0:9b9c31c57895 218
guruimage 0:9b9c31c57895 219 /**
guruimage 0:9b9c31c57895 220 * Restart the protothread.
guruimage 0:9b9c31c57895 221 *
guruimage 0:9b9c31c57895 222 * This macro will block and cause the running protothread to restart
guruimage 0:9b9c31c57895 223 * its execution at the place of the PT_BEGIN() call.
guruimage 0:9b9c31c57895 224 *
guruimage 0:9b9c31c57895 225 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 226 *
guruimage 0:9b9c31c57895 227 * \hideinitializer
guruimage 0:9b9c31c57895 228 */
guruimage 0:9b9c31c57895 229 #define PT_RESTART(pt) \
guruimage 0:9b9c31c57895 230 do { \
guruimage 0:9b9c31c57895 231 PT_INIT(pt); \
guruimage 0:9b9c31c57895 232 return PT_WAITING; \
guruimage 0:9b9c31c57895 233 } while(0)
guruimage 0:9b9c31c57895 234
guruimage 0:9b9c31c57895 235 /**
guruimage 0:9b9c31c57895 236 * Exit the protothread.
guruimage 0:9b9c31c57895 237 *
guruimage 0:9b9c31c57895 238 * This macro causes the protothread to exit. If the protothread was
guruimage 0:9b9c31c57895 239 * spawned by another protothread, the parent protothread will become
guruimage 0:9b9c31c57895 240 * unblocked and can continue to run.
guruimage 0:9b9c31c57895 241 *
guruimage 0:9b9c31c57895 242 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 243 *
guruimage 0:9b9c31c57895 244 * \hideinitializer
guruimage 0:9b9c31c57895 245 */
guruimage 0:9b9c31c57895 246 #define PT_EXIT(pt) \
guruimage 0:9b9c31c57895 247 do { \
guruimage 0:9b9c31c57895 248 PT_INIT(pt); \
guruimage 0:9b9c31c57895 249 return PT_EXITED; \
guruimage 0:9b9c31c57895 250 } while(0)
guruimage 0:9b9c31c57895 251
guruimage 0:9b9c31c57895 252 /** @} */
guruimage 0:9b9c31c57895 253
guruimage 0:9b9c31c57895 254 /**
guruimage 0:9b9c31c57895 255 * \name Calling a protothread
guruimage 0:9b9c31c57895 256 * @{
guruimage 0:9b9c31c57895 257 */
guruimage 0:9b9c31c57895 258
guruimage 0:9b9c31c57895 259 /**
guruimage 0:9b9c31c57895 260 * Schedule a protothread.
guruimage 0:9b9c31c57895 261 *
guruimage 0:9b9c31c57895 262 * This function shedules a protothread. The return value of the
guruimage 0:9b9c31c57895 263 * function is non-zero if the protothread is running or zero if the
guruimage 0:9b9c31c57895 264 * protothread has exited.
guruimage 0:9b9c31c57895 265 *
guruimage 0:9b9c31c57895 266 * \param f The call to the C function implementing the protothread to
guruimage 0:9b9c31c57895 267 * be scheduled
guruimage 0:9b9c31c57895 268 *
guruimage 0:9b9c31c57895 269 * \hideinitializer
guruimage 0:9b9c31c57895 270 */
guruimage 0:9b9c31c57895 271 #define PT_SCHEDULE(f) ((f) < PT_EXITED)
guruimage 0:9b9c31c57895 272
guruimage 0:9b9c31c57895 273 /** @} */
guruimage 0:9b9c31c57895 274
guruimage 0:9b9c31c57895 275 /**
guruimage 0:9b9c31c57895 276 * \name Yielding from a protothread
guruimage 0:9b9c31c57895 277 * @{
guruimage 0:9b9c31c57895 278 */
guruimage 0:9b9c31c57895 279
guruimage 0:9b9c31c57895 280 /**
guruimage 0:9b9c31c57895 281 * Yield from the current protothread.
guruimage 0:9b9c31c57895 282 *
guruimage 0:9b9c31c57895 283 * This function will yield the protothread, thereby allowing other
guruimage 0:9b9c31c57895 284 * processing to take place in the system.
guruimage 0:9b9c31c57895 285 *
guruimage 0:9b9c31c57895 286 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 287 *
guruimage 0:9b9c31c57895 288 * \hideinitializer
guruimage 0:9b9c31c57895 289 */
guruimage 0:9b9c31c57895 290 #define PT_YIELD(pt) \
guruimage 0:9b9c31c57895 291 do { \
guruimage 0:9b9c31c57895 292 PT_YIELD_FLAG = 0; \
guruimage 0:9b9c31c57895 293 LC_SET((pt)->lc); \
guruimage 0:9b9c31c57895 294 if(PT_YIELD_FLAG == 0) { \
guruimage 0:9b9c31c57895 295 return PT_YIELDED; \
guruimage 0:9b9c31c57895 296 } \
guruimage 0:9b9c31c57895 297 } while(0)
guruimage 0:9b9c31c57895 298
guruimage 0:9b9c31c57895 299 /**
guruimage 0:9b9c31c57895 300 * \brief Yield from the protothread until a condition occurs.
guruimage 0:9b9c31c57895 301 * \param pt A pointer to the protothread control structure.
guruimage 0:9b9c31c57895 302 * \param cond The condition.
guruimage 0:9b9c31c57895 303 *
guruimage 0:9b9c31c57895 304 * This function will yield the protothread, until the
guruimage 0:9b9c31c57895 305 * specified condition evaluates to true.
guruimage 0:9b9c31c57895 306 *
guruimage 0:9b9c31c57895 307 *
guruimage 0:9b9c31c57895 308 * \hideinitializer
guruimage 0:9b9c31c57895 309 */
guruimage 0:9b9c31c57895 310 #define PT_YIELD_UNTIL(pt, cond) \
guruimage 0:9b9c31c57895 311 do { \
guruimage 0:9b9c31c57895 312 PT_YIELD_FLAG = 0; \
guruimage 0:9b9c31c57895 313 LC_SET((pt)->lc); \
guruimage 0:9b9c31c57895 314 if((PT_YIELD_FLAG == 0) || !(cond)) { \
guruimage 0:9b9c31c57895 315 return PT_YIELDED; \
guruimage 0:9b9c31c57895 316 } \
guruimage 0:9b9c31c57895 317 } while(0)
guruimage 0:9b9c31c57895 318
guruimage 0:9b9c31c57895 319 /** @} */
guruimage 0:9b9c31c57895 320
guruimage 0:9b9c31c57895 321 #endif /* __PT_H__ */
guruimage 0:9b9c31c57895 322
guruimage 0:9b9c31c57895 323 /** @} */