using objects in signal handler? - SGI

This is a discussion on using objects in signal handler? - SGI ; Hi, Just wondering if its possible/a good idea to make method calls on static (global) objects inside a signal handler? (just for checking the values of some variables) Also, does anyone know what would the point be of blocking signals ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: using objects in signal handler?

  1. using objects in signal handler?

    Hi,

    Just wondering if its possible/a good idea to make method calls on
    static (global) objects inside a signal handler? (just for checking
    the values of some variables)

    Also, does anyone know what would the point be of blocking signals
    before entering a critical section? (assuming that variables are just
    being accessed in the CS), then unblocking them after exiting the CS?

    Thanks

  2. Re: using objects in signal handler?

    In article ,
    Steven Wong wrote:
    :Just wondering if its possible/a good idea to make method calls on
    :static (global) objects inside a signal handler? (just for checking
    :the values of some variables)

    The C++ Standard is about |<------------------------------->| thick
    and I haven't gotten around to reading that section yet, so I don't
    know what the official word is.

    Based upon my knowledge of POSIX.1, my thought would be that
    such accesses could only be safe if there the access process
    did not involve any constructors or implicit conversions -- e.g.,
    you'd want to call with *exactly* matching arguments, not just
    compatible arguments. If there are any constructors or conversions
    involved, then those constructors or conversions are going to
    need to allocate memory -- which is going to be a problem if you
    happen to be within a memory-allocation routine at the time of the
    signal.

    POSIX.1 has a list of which routines are considered to be
    safe to call in a signal handler, and it's not a very big list.
    The closer you can get to really just reading an existing value,
    the better off you are.


    :Also, does anyone know what would the point be of blocking signals
    :before entering a critical section? (assuming that variables are just
    :being accessed in the CS), then unblocking them after exiting the CS?

    Imagine code optimization. Code that is written as one variable access
    might optimize out to several accesses to the same variable;
    if the variables get changed underneath you then you can end up
    executing the wrong code for the current value. For example,

    if (x != 0) foo /= x;

    and suppose x gets overwritten with 0 between the time of the test
    and the time of the division...


    You can, of course, work around this problem by using 'volatile'
    as appropriate, and by taking copies of critical variables before
    working with them.

    x_copy = x; if (x_copy != 0) foo /= x_copy;
    --
    Most Windows users will run any old attachment you send them, so if
    you want to implicate someone you can just send them a Trojan
    -- Adam Langley

  3. Re: using objects in signal handler?

    In article ,
    Steven Wong wrote:
    >Hi,
    >
    >Just wondering if its possible/a good idea to make method calls on
    >static (global) objects inside a signal handler? (just for checking
    >the values of some variables)


    The only things it is *safe* to do are listed in the
    language standards (C/C++) and posix. Other than
    C/C++ code which does not do library calls it's a very small list.

    Obviously anything which might do malloc/new is a no-no,
    so that rules out much C++ code as unsafe.
    Worse, it may work most of the time...

    But I suspect you know all that already, so I probably
    don't understand the question.


    >Also, does anyone know what would the point be of blocking signals
    >before entering a critical section? (assuming that variables are just
    >being accessed in the CS), then unblocking them after exiting the CS?



    THis one I'll leave for someone else.. Sorry.

    David Anderson

  4. Re: using objects in signal handler?

    gndm_2001@yahoo.com (Steven Wong) writes:
    > Just wondering if its possible/a good idea to make method calls on
    > static (global) objects inside a signal handler? (just for checking
    > the values of some variables)


    The only safe type of data to access from both, the normal stream of
    execution and a signal handler is a variable declared as:

    volatile sig_atomic_t pumpkin;

    C++ methods and objects are meant to virtualize away the
    implementation and focus on the interface. Although it's possible to
    do anything in static methods that can be done in a C function, I
    consider it a good idea to keep signal handling as simple and
    transparent (and thereby simple to understand) as possible.

    > Also, does anyone know what would the point be of blocking signals
    > before entering a critical section? (assuming that variables are just
    > being accessed in the CS), then unblocking them after exiting the CS?


    The access to said variables would then become an atomic transaction
    from the point of view of the signal handler. Finishing such a
    transaction could be flagged in a sig_atomic_t variable as shown
    above, which could in turn be tested by the signal handler.

    Thomas Jahns
    --
    "Computers are good at following instructions,
    but not at reading your mind."
    D. E. Knuth, The TeXbook, Addison-Wesley 1984, 1986, 1996, p. 9

+ Reply to Thread