Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1112

Re: Safe accesses of global arrays in signal handlers?

From scott@slp53.sl.home (Scott Lurndal)
Subject Re: Safe accesses of global arrays in signal handlers?
Newsgroups comp.programming.threads, comp.unix.programmer
References (1 earlier) <pan.2012.09.25.11.15.59.228000@nowhere.com> <acik2gF3panU1@mid.individual.net> <pan.2012.09.27.12.35.52.872000@nowhere.com> <aclh4pFol46U1@mid.individual.net> <5065d183$0$1569$91cee783@newsreader04.highway.telekom.at>
Message-ID <xvl9s.6403$5P7.3922@fe13.iad> (permalink)
Organization UseNetServer - www.usenetserver.com
Date 2012-09-28 17:54 +0000

Cross-posted to 2 groups.

Show all headers | View raw


Johann Klammer <klammerj@NOSPAM.a1.net> writes:
>Markus Elfring wrote:
>>> Doing anything substantial in a signal handler is problematic.
>>
>> Are the following approaches valid software design options for the discussed use
>> case?
>>
>> 1. "Self-pipe trick" that was described by Daniel J. Bernstein.
>>     http://cr.yp.to/docs/selfpipe.html

A common method used to awaken a poll loop, when for example, one needs
to add "POLLOUT" to the events mask for a network file descriptor.  I usually
pass a single byte through the pipe, and a POLLIN event on the read-end of
the pipe will fire.   Consume the byte, then 'continue' to the top of the
poll loop.    To terminate the loop, set the boolean condition on the loop
to false[*] prior to writing to the pipe.   Otherwise, the loop will examine
the outbound queue, and if not empty, set POLLOUT on the appropriate
network connection file descriptor which, when tripped, will result in
a send/write/sendto/writev of the outbound packet.

>>
>This should work nicely, as long as you need some kind of FIFO. The 
>write() is usable in signal handler.
>> 2. Are lock-free algorithms applicable like an implementation is described in
>> the article "Signal-Safe Locks"?
>>     http://locklessinc.com/articles/signalsafe_locks/
>>
>>     http://en.wikipedia.org/wiki/Non-blocking_algorithm
>>
>It depends if your architecture allows atomic increments. Better avoid that.

gcc provides atomic primitives that are portable across all architectures
(and C11/C++11 have now specified atomic primitives as well).  On architectures
that don't support atomics in hardware, the compiler will generate calls to
library functions that will emulate them with LL/SC, compare and swap, et. al.

>
>With both approaches you'll have to consider that under some 
>circumstances, multiple signals may be merged to one even before your 
>handler will see them.

Unless one is using queued aka real-time signals.

scott

[*] Use the following macro to test the condition at the top of the loop
    to avoid compiler optimization related issues when using threads or signals.

#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))

Back to comp.programming.threads | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-25 08:01 +0200
  Re: Safe accesses of global arrays in signal handlers? Nobody <nobody@nowhere.com> - 2012-09-25 12:15 +0100
    Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-27 11:20 +0200
      Re: Safe accesses of global arrays in signal handlers? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-09-27 13:23 +0100
        Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-28 13:25 +0200
      Re: Safe accesses of global arrays in signal handlers? Nobody <nobody@nowhere.com> - 2012-09-27 13:35 +0100
        Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-28 13:48 +0200
          Re: Safe accesses of global arrays in signal handlers? Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-09-28 18:34 +0200
            Re: Safe accesses of global arrays in signal handlers? scott@slp53.sl.home (Scott Lurndal) - 2012-09-28 17:54 +0000
          Re: Safe accesses of global arrays in signal handlers? Nobody <nobody@nowhere.com> - 2012-09-28 18:26 +0100
            Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-29 13:44 +0200
              Re: Safe accesses of global arrays in signal handlers? Nobody <nobody@nowhere.com> - 2012-09-29 20:37 +0100
                Re: Safe accesses of global arrays in signal handlers? William Ahern <william@wilbur.25thandClement.com> - 2012-09-29 17:31 -0700
                Signal handlers writing into pipes Markus Elfring <Markus.Elfring@web.de> - 2012-09-30 13:21 +0200
                Re: Safe accesses of global arrays in signal handlers? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-10-01 18:04 +0100
                Signal handlers writing into pipes Markus Elfring <Markus.Elfring@web.de> - 2012-10-03 16:36 +0200
                Re: Signal handlers writing into pipes Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-10-03 17:07 +0100
                Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-30 13:12 +0200
                Re: Safe accesses of global arrays in signal handlers? Nobody <nobody@nowhere.com> - 2012-09-30 20:46 +0100
        Re: Safe accesses of global arrays in signal handlers? Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2012-09-28 13:39 +0100
        Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-29 13:15 +0200
          Re: Safe accesses of global arrays in signal handlers? William Ahern <william@wilbur.25thandClement.com> - 2012-09-29 17:42 -0700
            Re: Safe accesses of global arrays in signal handlers? Markus Elfring <Markus.Elfring@web.de> - 2012-09-30 14:04 +0200
  Re: Safe accesses of global arrays in signal handlers? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-09-25 13:27 +0100

csiph-web