1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="The eCos Kernel"
23 HREF="kernel.html"><LINK
26 HREF="kernel-mail-boxes.html"><LINK
29 HREF="kernel-spinlocks.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="kernel-mail-boxes.html"
71 HREF="kernel-spinlocks.html"
82 NAME="KERNEL-FLAGS">Event Flags</H1
90 >cyg_flag_init, cyg_flag_destroy, cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_peek, cyg_flag_waiting -- Synchronization primitive</DIV
92 CLASS="REFSYNOPSISDIV"
108 CLASS="FUNCSYNOPSISINFO"
109 >#include <cyg/kernel/kapi.h>
118 >void cyg_flag_init</CODE
119 >(cyg_flag_t* flag);</CODE
125 >void cyg_flag_destroy</CODE
126 >(cyg_flag_t* flag);</CODE
132 >void cyg_flag_setbits</CODE
133 >(cyg_flag_t* flag, cyg_flag_value_t value);</CODE
139 >void cyg_flag_maskbits</CODE
140 >(cyg_flag_t* flag, cyg_flag_value_t value);</CODE
146 >cyg_flag_value_t cyg_flag_wait</CODE
147 >(cyg_flag_t* flag, cyg_flag_value_t pattern, cyg_flag_mode_t mode);</CODE
153 >cyg_flag_value_t cyg_flag_timed_wait</CODE
154 >(cyg_flag_t* flag, cyg_flag_value_t pattern, cyg_flag_mode_t mode, cyg_tick_count_t abstime);</CODE
160 >cyg_flag_value_t cyg_flag_poll</CODE
161 >(cyg_flag_t* flag, cyg_flag_value_t pattern, cyg_flag_mode_t mode);</CODE
167 >cyg_flag_value_t cyg_flag_peek</CODE
168 >(cyg_flag_t* flag);</CODE
174 >cyg_bool_t cyg_flag_waiting</CODE
175 >(cyg_flag_t* flag);</CODE
184 NAME="KERNEL-FLAGS-DESCRIPTION"
189 >Event flags allow a consumer thread to wait for one of several
190 different types of event to occur. Alternatively it is possible to
191 wait for some combination of events. The implementation is relatively
192 straightforward. Each event flag contains a 32-bit integer.
193 Application code associates these bits with specific events, so for
194 example bit 0 could indicate that an I/O operation has completed and
195 data is available, while bit 1 could indicate that the user has
196 pressed a start button. A producer thread or a DSR can cause one or
197 more of the bits to be set, and a consumer thread currently waiting
198 for these bits will be woken up.
201 >Unlike semaphores no attempt is made to keep track of event counts. It
202 does not matter whether a given event occurs once or multiple times
203 before being consumed, the corresponding bit in the event flag will
204 change only once. However semaphores cannot easily be used to handle
205 multiple event sources. Event flags can often be used as an
206 alternative to condition variables, although they cannot be used for
207 completely arbitrary conditions and they only support the equivalent
208 of condition variable broadcasts, not signals.
211 >Before an event flag can be used it must be initialized by a call to
215 >. This takes a pointer to a
219 > data structure, which can be part of a
220 larger structure. All 32 bits in the event flag will be set to 0,
221 indicating that no events have yet occurred. If an event flag is no
222 longer required it can be cleaned up with a call to
225 >cyg_flag_destroy</TT
226 >, allowing the memory for the
232 > structure to be re-used.
235 >A consumer thread can wait for one or more events by calling
239 >. This takes three arguments. The
240 first identifies a particular event flag. The second is some
241 combination of bits, indicating which events are of interest. The
242 final argument should be one of the following:
252 >CYG_FLAG_WAITMODE_AND</TT
259 > will block until all
260 the specified event bits are set. The event flag is not cleared when
261 the wait succeeds, in other words all the bits remain set.
267 >CYG_FLAG_WAITMODE_OR</TT
271 >The call will block until at least one of the specified event bits is
272 set. The event flag is not cleared on return.
278 >CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR</TT
282 >The call will block until all the specified event bits are set, and
283 the entire event flag is cleared when the call succeeds. Note that
284 if this mode of operation is used then a single event flag cannot be
285 used to store disjoint sets of events, even though enough bits might
286 be available. Instead each disjoint set of events requires its own
293 >CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR</TT
297 >The call will block until at least one of the specified event bits is
298 set, and the entire flag is cleared when the call succeeds.
307 > normally blocks until the
308 required condition is satisfied. It will return the value of the event
309 flag at the point that the operation succeeded, which may be a
310 superset of the requested events. If
313 >cyg_thread_release</TT
314 > is used to unblock a thread
315 that is currently in a wait operation, the
319 > call will instead return 0.
324 >cyg_flag_timed_wait</TT
329 > which adds a timeout: the wait
330 operation must succeed within the specified number of ticks, or it
331 will fail with a return value of 0. <TT
335 is a non-blocking variant: if the wait operation can succeed
336 immediately it acts like <TT
340 it returns immediately with a value of 0.
345 >cyg_flag_setbits</TT
346 > is called by a producer thread
347 or from inside a DSR when an event occurs. The specified bits are or'd
348 into the current event flag value. This may cause a waiting thread to
349 be woken up, if its condition is now satisfied.
354 >cyg_flag_maskbits</TT
355 > can be used to clear one or
356 more bits in the event flag. This can be called from a producer when a
357 particular condition is no longer satisfied, for example when the user
358 is no longer pressing a particular button. It can also be used by a
359 consumer thread if <TT
361 >CYG_FLAG_WAITMODE_CLR</TT
363 used as part of the wait operation, to indicate that some but not all
364 of the active events have been consumed. If there are multiple
365 consumer threads performing wait operations without using
368 >CYG_FLAG_WAITMODE_CLR</TT
369 > then typically some
370 additional synchronization such as a mutex is needed to prevent
371 multiple threads consuming the same event.
374 >Two additional functions are provided to query the current state of an
378 > returns the current
379 value of the event flag, and <TT
381 >cyg_flag_waiting</TT
383 be used to find out whether or not there are any threads currently
384 blocked on the event flag. Both of these functions must be used with
385 care because other threads may be operating on the event flag.
391 NAME="KERNEL-FLAGS-CONTEXT"
399 > is typically called during system
400 initialization but may also be called in thread context. The same
403 >cyg_flag_destroy</TT
411 >cyg_flag_timed_wait</TT
412 > may only be called from
413 thread context. The remaining functions may be called from thread or
422 SUMMARY="Footer navigation table"
433 HREF="kernel-mail-boxes.html"
451 HREF="kernel-spinlocks.html"