NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pt.h
Go to the documentation of this file.
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  */
34 
48 #ifndef PT_H_
49 #define PT_H_
50 
75 #ifndef LC_ADDRLABELS_H_
76 #define LC_ADDRLABELS_H_
77 
79 typedef void * lc_t;
80 
81 #define LC_INIT(s) s = NULL
82 
83 
84 #define LC_RESUME(s) \
85  do { \
86  if(s != NULL) { \
87  goto *s; \
88  } \
89  } while(0)
90 
91 #define LC_SET(s) \
92  do { ({ __label__ resume; resume: (s) = &&resume; }); }while(0)
93 
94 #define LC_END(s)
95 
96 #endif /* LC_ADDRLABELS_H_ */
97 
99 struct pt {
101 };
102 
103 #define PT_WAITING 0
104 #define PT_YIELDED 1
105 #define PT_EXITED 2
106 #define PT_ENDED 3
107 
125 #define PT_INIT(pt) LC_INIT((pt)->lc)
126 
145 #define PT_THREAD(name_args) char name_args
146 
160 #define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; if (PT_YIELD_FLAG) {;} LC_RESUME((pt)->lc)
161 
172 #define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
173  PT_INIT(pt); return PT_ENDED; }
174 
193 #define PT_WAIT_UNTIL(pt, condition) \
194  do { \
195  LC_SET((pt)->lc); \
196  if(!(condition)) { \
197  return PT_WAITING; \
198  } \
199  } while(0)
200 
212 #define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
213 
237 #define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
238 
251 #define PT_SPAWN(pt, child, thread) \
252  do { \
253  PT_INIT((child)); \
254  PT_WAIT_THREAD((pt), (thread)); \
255  } while(0)
256 
274 #define PT_RESTART(pt) \
275  do { \
276  PT_INIT(pt); \
277  return PT_WAITING; \
278  } while(0)
279 
291 #define PT_EXIT(pt) \
292  do { \
293  PT_INIT(pt); \
294  return PT_EXITED; \
295  } while(0)
296 
316 #define PT_SCHEDULE(f) ((f) < PT_EXITED)
317 
335 #define PT_YIELD(pt) \
336  do { \
337  PT_YIELD_FLAG = 0; \
338  LC_SET((pt)->lc); \
339  if(PT_YIELD_FLAG == 0) { \
340  return PT_YIELDED; \
341  } \
342  } while(0)
343 
355 #define PT_YIELD_UNTIL(pt, cond) \
356  do { \
357  PT_YIELD_FLAG = 0; \
358  LC_SET((pt)->lc); \
359  if((PT_YIELD_FLAG == 0) || !(cond)) { \
360  return PT_YIELDED; \
361  } \
362  } while(0)
363 
366 #endif /* PT_H_ */
367 
void * lc_t
Definition: pt.h:79
lc_t lc
Definition: pt.h:100
Definition: pt.h:99