views:

25

answers:

2

I am porting an audio mixer from directsound on Windows to alsa on Linux. I am polling on, let's say, 16 file descriptors using the system call "poll". Now i need to be able to abort the polling somehow. On Windows i am using the WaitForMultipleObjects using events and when i need to abort the waiting i just SetEvent on one of the events causing the wait to return. Is there any way to mark a file descriptor in Linux "ready" so that the poll will return?

I have taken a look at ppoll but i am not familiar with signals and i don't want to handle unnecessary race conditions. I mean, if alsa can set the file descriptors to "ready" i should also be able to ;)

A: 

Use the timeout and check the exit condition.

while (not exit_condition):
    int poll(struct pollfd *fds, nfds_t nfds, int timeout);
fabrizioM
This will be user-mode polling.
Alex
+1  A: 

If you make a pipe using the pipe() function, you can add the output end into your poll() list. Then you can write something into the input end of the pipe and your poll will return. Much like your Windows version.

You'd need to be using something asynchronous like threads or signal handlers to make that work.

Another option would be to use sigaction() to set a signal handler without the SA_RESTART flag. You could use an unused signal like SIGUSR1 or one of the real-time signals. When you want to interrupt the poll() then you send that signal and poll() will return with -1 and errno set to EINTR.

It would be possible for a different signal to interrupt your poll() as well, unless you used sigmask() to block unwanted signals.

Zan Lynx
I like your solution with the extra descriptor as a way of exiting the poll but what about the system call "ppoll"? Quote "ppoll() allows an application to safely wait until either a file descriptor becomes ready or until a signal is caught." Using ppoll i should be able to send some kind of signal when i want to abort the poll, i just dont know a lot about signals as i really have never used them.
Alex
Hey i think i get that - the ppoll has a mask, blocking signals other than the one i use to abort the poll? If i specify 0 as the mask the poll wont be interrupted by any signals and if i make a mask for just the signal i use it will only abort on my signal? I think i have to look into signals then :)
Alex
I did this to abort the poll://Interrupt poll in the streaming threadsignal(SIGUSR1, ignore_handler);pthread_kill(thread->native_handle(), SIGUSR1);where ignore_handle is an empty void taking an int.
Alex