when to use volatile for reading from objects written by signalhandlers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • viza

    when to use volatile for reading from objects written by signalhandlers

    Hi all

    Suppose I have:

    static sig_atomic_t timed_out= 0;

    void time_out( int unrequired ){
    timed_out= 1;
    }

    And I successfully call:

    signal( SIGFOO, time_out );


    If I suspect that SIGFOO may be be delivered soon, is it sufficient to do:

    while( ! timed_out )
    do_something_ta king_finite_tim e();

    Or do I need to do something like:

    while( !*(volatile sig_atomic_t*)& timed_out )

    How does this change when:

    a) I am sure that timed_out is accessed only by this translation unit?
    b) A pointer to timed_out may be read from another translation unit?
    c) A pointer to timed_out may be written to by another translation unit?

    Is it necessary to use sig_atomic_t for this task, or could a non-atomic
    type be used, like bool or int?

    Thanks
    viza
  • jameskuyper@verizon.net

    #2
    Re: when to use volatile for reading from objects written by signalhandlers

    viza wrote:
    Hi all
    >
    Suppose I have:
    >
    static sig_atomic_t timed_out= 0;
    >
    void time_out( int unrequired ){
    timed_out= 1;
    }
    >
    And I successfully call:
    >
    signal( SIGFOO, time_out );
    7.14.1.1p5: "If the signal occurs other than as the result of calling
    the abort or raise function, the
    behavior is undefined if the signal handler refers to any object with
    static storage duration
    other than by assigning a value to an object declared as volatile
    sig_atomic_t ..."

    Unless you are certain that SIGFOO will only be raised by abort() or
    raise(), your code already has undefined behavior.
    If I suspect that SIGFOO may be be delivered soon, is it sufficient to do:
    >
    while( ! timed_out )
    do_something_ta king_finite_tim e();
    >
    Or do I need to do something like:
    >
    while( !*(volatile sig_atomic_t*)& timed_out )
    That won't help - the problem lies in time_out().
    How does this change when:
    >
    a) I am sure that timed_out is accessed only by this translation unit?
    b) A pointer to timed_out may be read from another translation unit?
    c) A pointer to timed_out may be written to by another translation unit?
    Not at all.
    Is it necessary to use sig_atomic_t for this task, or could a non-atomic
    type be used, like bool or int?
    The only way that you can get away with not declaring timed_out
    volatile, is If the signal will only be raised by a call to abort() or
    raise(); in that case, you can use any type you like.

    Comment

    Working...