OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [FreeTCPIP/] [sys/] [pt.h] - Blame information for rev 606

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.