All of lore.kernel.org
 help / color / mirror / Atom feed
* poll() and select()
@ 2003-10-13 17:53 Ryan Pavlik
  2003-10-14  9:49 ` Takashi Iwai
  0 siblings, 1 reply; 9+ messages in thread
From: Ryan Pavlik @ 2003-10-13 17:53 UTC (permalink / raw)
  To: alsa-devel

Hey, I'm currently pondering an ALSA interface for Ruby
(http://ruby-lang.org).  Unfortunately, to do what I want, I need to
be able to have a file descriptor for select().  I've tried using the
fds returned by snd_seq_poll_descriptors(), but (not suprisingly) they
didn't work: select blocks indefinitely.

The interface would be for writing a MIDI sequencer.  Any thoughts on
the issue?

-- 
Ryan Pavlik <rpav@mephle.com>

"They don't *appear* to be in league with some
 nameless *horror* from the nether-dimensions I
 inadvertendtly pissed off by trying to enslave." - 8BT


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-13 17:53 poll() and select() Ryan Pavlik
@ 2003-10-14  9:49 ` Takashi Iwai
  2003-10-14 18:30   ` Ryan Pavlik
  0 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2003-10-14  9:49 UTC (permalink / raw)
  To: Ryan Pavlik; +Cc: alsa-devel

At Mon, 13 Oct 2003 10:53:24 -0700,
Ryan Pavlik wrote:
> 
> Hey, I'm currently pondering an ALSA interface for Ruby
> (http://ruby-lang.org).  Unfortunately, to do what I want, I need to
> be able to have a file descriptor for select().  I've tried using the
> fds returned by snd_seq_poll_descriptors(), but (not suprisingly) they
> didn't work: select blocks indefinitely.

if i understand correctly, it should work.  fd field in the returned
pollfd struct is the file descriptor for select().


Takashi


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-14  9:49 ` Takashi Iwai
@ 2003-10-14 18:30   ` Ryan Pavlik
  2003-10-15  1:06     ` Ryan Underwood
  0 siblings, 1 reply; 9+ messages in thread
From: Ryan Pavlik @ 2003-10-14 18:30 UTC (permalink / raw)
  To: alsa-devel

[-- Attachment #1: Type: text/plain, Size: 1996 bytes --]

On Tue, 14 Oct 2003 11:49:37 +0200
Takashi Iwai <tiwai@suse.de> wrote:

> At Mon, 13 Oct 2003 10:53:24 -0700,
> Ryan Pavlik wrote:
> > 
> > Hey, I'm currently pondering an ALSA interface for Ruby
> > (http://ruby-lang.org).  Unfortunately, to do what I want, I need to
> > be able to have a file descriptor for select().  I've tried using the
> > fds returned by snd_seq_poll_descriptors(), but (not suprisingly) they
> > didn't work: select blocks indefinitely.
> 
> if i understand correctly, it should work.  fd field in the returned
> pollfd struct is the file descriptor for select().

This is what I had hoped.  However, it doesn't seem to work correctly,
and I'm thinking it's because the file descriptor isn't actually for
reading and writing, but some other form of communication I'm not
familiar with.

I'm using the "seqdemo.c" example from the HOWTO as a testbase.
The main bit of code looks like this:

    while (1) {
        FD_ZERO(&rfds);
        FD_SET(pfd[0].fd, &rfds);

        if (select(npfd, &rfds, NULL, NULL, NULL)) {
            midi_action(seq_handle);
        }  
    }

Basically, a replacement of the old while() loop with a bridge from
the poll() structs to select().  I've used select() before with no
problems, but this simply hangs indefinitely, while the poll() version
works as advertised.

>From the select manpage:

       Three independent sets of descriptors are  watched.   Those  listed  in
       readfds will be watched to see if characters become available for read-
       ing (more precisely, to see if a read will not block - in particular, a
       file  descriptor  is also ready on end-of-file), [...]

I'm thinking it may not be the case that /proc/snd/dev/seq may not be
something that you read() from, but I'm not sure.  I've attached my
version of seqdemo, perhaps I'm doing something wrong.

thanks,

-- 
Ryan Pavlik <rpav@mephle.com>

"I'll *prove* to you that sword-chucks are the
 single greatest advancement in all weaponology." - 8BT

[-- Attachment #2: seqdemo.c --]
[-- Type: application/octet-stream, Size: 2566 bytes --]

/* seqdemo.c by Matthias Nagorni */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <alsa/asoundlib.h>

snd_seq_t *open_seq();
void midi_action(snd_seq_t *seq_handle);

snd_seq_t *open_seq() {

    snd_seq_t *seq_handle;
    int portid;

    if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0) < 0) {
        fprintf(stderr, "Error opening ALSA sequencer.\n");
        exit(1);
    }
    snd_seq_set_client_name(seq_handle, "ALSA Sequencer Demo");
    if ((portid = snd_seq_create_simple_port(seq_handle, "ALSA Sequencer Demo",
                                             SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
                                             SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
        fprintf(stderr, "Error creating sequencer port.\n");
        exit(1);
    }
    return(seq_handle);
}

void midi_action(snd_seq_t *seq_handle) {

    snd_seq_event_t *ev;

    do {
        snd_seq_event_input(seq_handle, &ev);
        switch (ev->type) {
            case SND_SEQ_EVENT_CONTROLLER: 
                fprintf(stderr, "Control event on Channel %2d: %5d       \r",
                        ev->data.control.channel, ev->data.control.value);
                break;
            case SND_SEQ_EVENT_PITCHBEND:
                fprintf(stderr, "Pitchbender event on Channel %2d: %5d   \r", 
                        ev->data.control.channel, ev->data.control.value);
                break;
            case SND_SEQ_EVENT_NOTEON:
                fprintf(stderr, "Note On event on Channel %2d: %5d       \r",
                        ev->data.control.channel, ev->data.note.note);
                break;        
            case SND_SEQ_EVENT_NOTEOFF: 
                fprintf(stderr, "Note Off event on Channel %2d: %5d      \r",         
                        ev->data.control.channel, ev->data.note.note);           
                break;        
        }
        snd_seq_free_event(ev);
    } while (snd_seq_event_input_pending(seq_handle, 0) > 0);
}

int main(int argc, char *argv[]) {

    snd_seq_t *seq_handle;
    int npfd;
    struct pollfd *pfd;
  
    fd_set rfds;
    
    seq_handle = open_seq();

    npfd = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
    pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
    snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN);

    while (1) {
        FD_ZERO(&rfds);
        FD_SET(pfd[0].fd, &rfds);

        if (select(npfd, &rfds, NULL, NULL, NULL)) {
            midi_action(seq_handle);
        }  
    }
}

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-14 18:30   ` Ryan Pavlik
@ 2003-10-15  1:06     ` Ryan Underwood
  2003-10-15  2:41       ` Ryan Pavlik
  0 siblings, 1 reply; 9+ messages in thread
From: Ryan Underwood @ 2003-10-15  1:06 UTC (permalink / raw)
  To: alsa-devel


Hi,

On Tue, Oct 14, 2003 at 11:30:31AM -0700, Ryan Pavlik wrote:
> 
> I'm using the "seqdemo.c" example from the HOWTO as a testbase.
> The main bit of code looks like this:
> 
>     while (1) {
>         FD_ZERO(&rfds);
>         FD_SET(pfd[0].fd, &rfds);
> 
>         if (select(npfd, &rfds, NULL, NULL, NULL)) {
>             midi_action(seq_handle);
>         }  
>     }

Silly question, but is 'npfd' the file descriptor you are polling + 1?

-- 
Ryan Underwood, <nemesis at icequake.net>, icq=10317253


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-15  1:06     ` Ryan Underwood
@ 2003-10-15  2:41       ` Ryan Pavlik
  2003-10-15 21:36         ` Ryan Underwood
  2003-10-16 17:54         ` Jaroslav Kysela
  0 siblings, 2 replies; 9+ messages in thread
From: Ryan Pavlik @ 2003-10-15  2:41 UTC (permalink / raw)
  To: alsa-devel

On Tue, 14 Oct 2003 20:06:00 -0500
Ryan Underwood <nemesis-lists@icequake.net> wrote:

> Silly question, but is 'npfd' the file descriptor you are polling + 1?

I AM DUMB.

I totally overlooked this.  Simple fix, and it works great.
Thanks. :-)  You'd think I had never done this before.

So the select() version works fine, with the bridge from the poll()
file structure.  This should integrate with ruby just fine then.

thanks,

-- 
Ryan Pavlik <rpav@mephle.com>

"I'll *prove* to you that sword-chucks are the
 single greatest advancement in all weaponology." - 8BT


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-15  2:41       ` Ryan Pavlik
@ 2003-10-15 21:36         ` Ryan Underwood
  2003-10-16 17:54         ` Jaroslav Kysela
  1 sibling, 0 replies; 9+ messages in thread
From: Ryan Underwood @ 2003-10-15 21:36 UTC (permalink / raw)
  To: alsa-devel


Hi,

On Tue, Oct 14, 2003 at 07:41:05PM -0700, Ryan Pavlik wrote:
> On Tue, 14 Oct 2003 20:06:00 -0500
> Ryan Underwood <nemesis-lists@icequake.net> wrote:
> 
> > Silly question, but is 'npfd' the file descriptor you are polling + 1?
> 
> I AM DUMB.
> 
> I totally overlooked this.  Simple fix, and it works great.
> Thanks. :-)  You'd think I had never done this before.

I guess there is after all some benefit to asking the silly question
from time to time... ;)  At least, I know I also made that particular
stupid mistake in past code.

Good luck!

-- 
Ryan Underwood, <nemesis at icequake.net>, icq=10317253


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-15  2:41       ` Ryan Pavlik
  2003-10-15 21:36         ` Ryan Underwood
@ 2003-10-16 17:54         ` Jaroslav Kysela
  2003-10-16 18:28           ` Ryan Pavlik
  1 sibling, 1 reply; 9+ messages in thread
From: Jaroslav Kysela @ 2003-10-16 17:54 UTC (permalink / raw)
  To: Ryan Pavlik; +Cc: alsa-devel

On Tue, 14 Oct 2003, Ryan Pavlik wrote:

> On Tue, 14 Oct 2003 20:06:00 -0500
> Ryan Underwood <nemesis-lists@icequake.net> wrote:
>
> > Silly question, but is 'npfd' the file descriptor you are polling + 1?
>
> I AM DUMB.
>
> I totally overlooked this.  Simple fix, and it works great.
> Thanks. :-)  You'd think I had never done this before.
>
> So the select() version works fine, with the bridge from the poll()
> file structure.  This should integrate with ruby just fine then.

Don't use select() if possible. You don't know which direction is required
for a file descriptor. You must use snd_seq_poll_descriptors_revents()
which mangles the used direction and reports the correct one to
the application (and yes, it might be an opposite or both
directions in real if we implementing some complex devices behind this
API).

						Jaroslav

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-16 17:54         ` Jaroslav Kysela
@ 2003-10-16 18:28           ` Ryan Pavlik
  2003-10-16 18:35             ` Jaroslav Kysela
  0 siblings, 1 reply; 9+ messages in thread
From: Ryan Pavlik @ 2003-10-16 18:28 UTC (permalink / raw)
  To: alsa-devel

On Thu, 16 Oct 2003 19:54:20 +0200 (CEST)
Jaroslav Kysela <perex@suse.cz> wrote:

<snip>
> Don't use select() if possible. You don't know which direction is required
> for a file descriptor. You must use snd_seq_poll_descriptors_revents()
> which mangles the used direction and reports the correct one to
> the application (and yes, it might be an opposite or both
> directions in real if we implementing some complex devices behind this
> API).
<snip>

Unfortunately it's necessary...  I'm trying to write a ruby module,
and ruby uses soft threads for portability's sake.  It's also
unfortunately not threadsafe, or I could just dump the sequencer stuff
in a pthread and go from there.

It does provide rb_thread_select(), which uses select() but schedules
its own threads while blocking.

The pollfd struct does provide the 'events' member which can have
POLLIN and/or POLLOUT, so I should still be able to put the
descriptors in the right group for select, no?

thanks,

-- 
Ryan Pavlik <rpav@mephle.com>

"Prepare to face the unparalleled force of a master
 of all twelve zodiac kenshido styles!" - 8BT


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: poll() and select()
  2003-10-16 18:28           ` Ryan Pavlik
@ 2003-10-16 18:35             ` Jaroslav Kysela
  0 siblings, 0 replies; 9+ messages in thread
From: Jaroslav Kysela @ 2003-10-16 18:35 UTC (permalink / raw)
  To: Ryan Pavlik; +Cc: alsa-devel

On Thu, 16 Oct 2003, Ryan Pavlik wrote:

> On Thu, 16 Oct 2003 19:54:20 +0200 (CEST)
> Jaroslav Kysela <perex@suse.cz> wrote:
>
> <snip>
> > Don't use select() if possible. You don't know which direction is required
> > for a file descriptor. You must use snd_seq_poll_descriptors_revents()
> > which mangles the used direction and reports the correct one to
> > the application (and yes, it might be an opposite or both
> > directions in real if we implementing some complex devices behind this
> > API).
> <snip>
>
> Unfortunately it's necessary...  I'm trying to write a ruby module,
> and ruby uses soft threads for portability's sake.  It's also
> unfortunately not threadsafe, or I could just dump the sequencer stuff
> in a pthread and go from there.
>
> It does provide rb_thread_select(), which uses select() but schedules
> its own threads while blocking.
>
> The pollfd struct does provide the 'events' member which can have
> POLLIN and/or POLLOUT, so I should still be able to put the
> descriptors in the right group for select, no?

Yes, but you have to create revents for snd_seq_poll_descriptors_revents()
to get the right event then. Don't use values from select() directly.

						Jaroslav

-----
Jaroslav Kysela <perex@suse.cz>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-10-16 18:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-13 17:53 poll() and select() Ryan Pavlik
2003-10-14  9:49 ` Takashi Iwai
2003-10-14 18:30   ` Ryan Pavlik
2003-10-15  1:06     ` Ryan Underwood
2003-10-15  2:41       ` Ryan Pavlik
2003-10-15 21:36         ` Ryan Underwood
2003-10-16 17:54         ` Jaroslav Kysela
2003-10-16 18:28           ` Ryan Pavlik
2003-10-16 18:35             ` Jaroslav Kysela

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.