Signal and slot trouble

Johannes Pfau spam at example.com
Mon Nov 22 08:56:03 PST 2010


Am 21.11.2010, 19:47 Uhr, schrieb Peter Federighi <pfederighi at yahoo.com>:

> I'd still like to know if there's a better way/native way to trap C  
> style signals
> in D.  Or is there a D way of programming a timer and finding terminal  
> window size
> changes?

I don't think there are any D libraries to handle posix signals. Posix  
signals are a pita. In theory all functions you call in posix signal  
handlers must be signal safe and the requirements for that are very  
strict. Allocating memory in a signal handler is not safe for example. In  
practice, Linux for example is way more tolerant that the posix  
specification, so you often won't experience any problems with faulty  
signal handlers. Problems start to appear when you port to different OS  
which might be less forgiving. And as you're relying on unspecified  
features, a future update in kernel / external libraries could break your  
application.
(http://en.wikipedia.org/wiki/Signal_(computing)#Risks)

Because of these issues many people try to avoid signals whenever  
possible. If signals are needed the recommendation often is to use  
synchronous signal handling where all those issues don't exist. The key to  
synchronous signal handling is the sigwait  
function(http://linux.die.net/man/3/sigwait) but it blocks one thread all  
the time.
Other possible solutions: signals can be managed with libev. I guess it  
uses synchronous signal handling internally, but it allows you to wait for  
an io event, signal, and other things in one thread. libev also has timers.

I've also seen an timer implementation which uses a mutex and condition  
variables with timeouts to simulate a timer. Works without signals or any  
special system support, but I don't know of an implementation for D.

The simplest(and least effective) possibility is to use a loop,  
Thread.sleep() and polling
//Pseudo code, functions might have other names ;-)
Size lastSize;
while(true)
{
     auto size = getWindowSize();
     if(size != lastSize)
     ...
     Thread.sleep(1000);
}

-- 
Johannes Pfau


More information about the Digitalmars-d-learn mailing list