Comtrol eCos User Manual

Page 80

Advertising
background image

Event Flags

A consumer thread can wait for one or more events by calling

cyg_flag_wait

. This takes three arguments. The

first identifies a particular event flag. The second is some combination of bits, indicating which events are of
interest. The final argument should be one of the following:

CYG_FLAG_WAITMODE_AND

The call to

cyg_flag_wait

will block until all the specified event bits are set. The event flag is not cleared

when the wait succeeds, in other words all the bits remain set.

CYG_FLAG_WAITMODE_OR

The call will block until at least one of the specified event bits is set. The event flag is not cleared on return.

CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR

The call will block until all the specified event bits are set, and the entire event flag is cleared when the call
succeeds. Note that if this mode of operation is used then a single event flag cannot be used to store disjoint
sets of events, even though enough bits might be available. Instead each disjoint set of events requires its own
event flag.

CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR

The call will block until at least one of the specified event bits is set, and the entire flag is cleared when the
call succeeds.

A call to

cyg_flag_wait

normally blocks until the required condition is satisfied. It will return the value of

the event flag at the point that the operation succeeded, which may be a superset of the requested events. If

cyg_thread_release

is used to unblock a thread that is currently in a wait operation, the

cyg_flag_wait

call

will instead return 0.

cyg_flag_timed_wait

is a variant of

cyg_flag_wait

which adds a timeout: the wait operation must succeed

within the specified number of ticks, or it will fail with a return value of 0.

cyg_flag_poll

is a non-blocking vari-

ant: if the wait operation can succeed immediately it acts like

cyg_flag_wait

, otherwise it returns immediately

with a value of 0.

cyg_flag_setbits

is called by a producer thread or from inside a DSR when an event occurs. The specified bits

are or’d into the current event flag value. This may cause a waiting thread to be woken up, if its condition is now
satisfied.

cyg_flag_maskbits

can be used to clear one or more bits in the event flag. This can be called from a producer

when a particular condition is no longer satisfied, for example when the user is no longer pressing a particular
button. It can also be used by a consumer thread if

CYG_FLAG_WAITMODE_CLR

was not used as part of the wait

operation, to indicate that some but not all of the active events have been consumed. If there are multiple consumer
threads performing wait operations without using

CYG_FLAG_WAITMODE_CLR

then typically some additional syn-

chronization such as a mutex is needed to prevent multiple threads consuming the same event.

Two additional functions are provided to query the current state of an event flag.

cyg_flag_peek

returns the

current value of the event flag, and

cyg_flag_waiting

can be used to find out whether or not there are any

threads currently blocked on the event flag. Both of these functions must be used with care because other threads
may be operating on the event flag.

80

Advertising