[ECOS] cyg_flag_timed_wait()/cyg_flag_setbits() behaviour question

Andrew Lunn andrew@lunn.ch
Thu Sep 14 18:13:00 GMT 2006

On Thu, Sep 14, 2006 at 10:50:39AM -0700, David Roethig wrote:
> We're using flags to wakeup threads. We use different bit
> contained within one flag for signaling. (i.e. CAN_RX is bit 1,
> SHUTDOWN is bit2, etc.)
> I have some questions/comments regarding the use
> of this kernel feature:
> 1) the flag_result returned with cyg_flag_timed_wait()
>   contains all of the current bits set not the mask
>   supplied with the cyg_flag_timed_wait() call.
>   It seems to me that the result should only return the
>   bits this call is interested in seeing. Is this
>   expected behaviour?

The documentation
http://ecos.sourceware.org/docs-latest/ref/kernel-flags.html says:

    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.

so yes, this is expected behaviour. You know what you are interested
in, so you can do the masking yourself.

> 2) the cyg_flag_setbits() routine in flags.cxx clears
>   the current value of the flag upon finding the first
>   thread waiting on the bit(s). It should wait until
>   all tasks are woken before clearing the flag bits.
>   It too suffers from the problem that the entire flag
>   is cleared, not just the bit(s) desired.
>   This means that multiple threads cannot use the
>   same cyg_flag_t to wait for different (or common) bits.
> An I misunderstanding the use of the flag feature?

The clearing depends on how the waiters are waiting, what flags they
have passed.

     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 one or more waiting
     threads to be woken up, if their conditions are now
     satisfied. How many threads are awoken depends on the use of
     CYG_FLAG_WAITMODE_CLR. The queue of threads waiting on the flag
     is walked to find threads which now have their wake condition
     fulfilled. If the awoken thread has passed CYG_FLAG_WAITMODE_CLR
     the walking of the queue is terminated, otherwise the walk
     continues. Thus if no threads have passed CYG_FLAG_WAITMORE_CLR
     all threads with fulfilled conditions will be awoken. If
     CYG_FLAG_WAITMODE_CLR is passed by threads with fulfilled
     conditions, the number of awoken threads will depend on the order
     the threads are in the queue.


Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list