Steven He / Mbed 2 deprecated StringMatchingProtothread

Dependencies:   mbed

Committer:
StevenHe
Date:
Wed Dec 01 04:39:00 2010 +0000
Revision:
0:544b5f6ad832

        

Who changed what in which revision?

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