Changeset bcde7926


Ignore:
Timestamp:
Jun 22, 2011, 12:37:21 AM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
bbb7876
Parents:
074bdaa
git-author:
David Benjamin <davidben@mit.edu> (05/24/11 01:10:12)
git-committer:
David Benjamin <davidben@mit.edu> (06/22/11 00:37:21)
Message:
Reimplement BarnOwl::add_io_dispatch with AnyEvent

We can emulate the interesting semantics with perl. The one difference
is that perl code can now register an IO dispatch on file descriptors C
code was interested in. This isn't a big deal was Glib can handle
multiple watches on the same FD. Granted, more than one reader on an FD
would cause trouble, but there was nothing stopping perl code from
reading from an FD we cared about anyway.

AnyEvent also does not support select's exceptfd, so this is a slight
behavior change, but probably an uninteresting one.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    r9179fd7 rbcde7926  
    166166read from C<FD>.
    167167
    168 C<add_dispatch> has been deprecated in favor of C<add_io_dispatch>,
    169 and is now a wrapper for it called with C<mode> set to C<'r'>.
     168C<add_dispatch> has been deprecated in favor of C<AnyEvent>, and is
     169now a wrapper for C<add_io_dispatch> called with C<mode> set to
     170C<'r'>.
    170171
    171172=cut
     
    181182Remove a file descriptor previously registered via C<add_dispatch>
    182183
    183 C<remove_dispatch> has been deprecated in favor of
    184 C<remove_io_dispatch>.
     184C<remove_dispatch> has been deprecated in favor of C<AnyEvent>.
    185185
    186186=cut
     
    197197registered, the old one is removed.
    198198
    199 =cut
     199C<add_io_dispatch> has been deprecated in favor of C<AnyEvent>.
     200
     201=cut
     202
     203our %_io_dispatches;
    200204
    201205sub add_io_dispatch {
     
    203207    my $modeStr = shift;
    204208    my $cb = shift;
    205     my $mode = 0;
    206 
    207     $mode |= 0x1 if ($modeStr =~ /r/i); # Read
    208     $mode |= 0x2 if ($modeStr =~ /w/i); # Write
    209     if ($mode) {
    210         $mode |= 0x4;                  # Exceptional
    211         BarnOwl::Internal::add_io_dispatch($fd, $mode, $cb);
     209    my @modes;
     210
     211    push @modes, 'r' if $modeStr =~ /r/i; # Read
     212    push @modes, 'w' if $modeStr =~ /w/i; # Write
     213    if (@modes) {
     214        BarnOwl::remove_io_dispatch($fd);
     215        for my $mode (@modes) {
     216            push @{$_io_dispatches{$fd}}, AnyEvent->io(fh => $fd,
     217                                                       poll => $mode,
     218                                                       cb => $cb);
     219        }
    212220    } else {
    213221        die("Invalid I/O Dispatch mode: $modeStr");
     
    218226
    219227Remove a file descriptor previously registered via C<add_io_dispatch>
     228
     229C<remove_io_dispatch> has been deprecated in favor of C<AnyEvent>.
     230
     231=cut
     232
     233sub remove_io_dispatch {
     234    my $fd = shift;
     235    undef $_ foreach @{$_io_dispatches{$fd}};
     236    delete $_io_dispatches{$fd};
     237}
    220238
    221239=head2 create_style NAME OBJECT
  • perlconfig.c

    r074bdaa rbcde7926  
    548548}
    549549
    550 void owl_perlconfig_io_dispatch_destroy(const owl_io_dispatch *d)
    551 {
    552   SvREFCNT_dec(d->data);
    553 }
    554 
    555550void owl_perlconfig_edit_callback(owl_editwin *e)
    556551{
     
    587582  SvREFCNT_dec(v);
    588583}
    589 
    590 void owl_perlconfig_io_dispatch(const owl_io_dispatch *d, void *data)
    591 {
    592   SV *cb = data;
    593   dSP;
    594   if(cb == NULL) {
    595     owl_function_error("Perl callback is NULL!");
    596     return;
    597   }
    598 
    599   ENTER;
    600   SAVETMPS;
    601 
    602   PUSHMARK(SP);
    603   PUTBACK;
    604 
    605   call_sv(cb, G_DISCARD|G_EVAL);
    606 
    607   if(SvTRUE(ERRSV)) {
    608     owl_function_error("%s", SvPV_nolen(ERRSV));
    609   }
    610 
    611   FREETMPS;
    612   LEAVE;
    613 }
  • perlglue.xs

    r074bdaa rbcde7926  
    326326                g_free(rv);
    327327
    328 void
    329 remove_io_dispatch(fd)
    330         int fd
    331         CODE:
    332         owl_select_remove_perl_io_dispatch(fd);
    333 
    334328AV*
    335329all_filters()
     
    507501                                      ival);
    508502
    509 void
    510 add_io_dispatch(fd, mode, cb)
    511         int fd
    512         int mode
    513         SV * cb
    514         CODE:
    515         owl_select_add_perl_io_dispatch(fd, mode, newSVsv(cb));
    516 
    517503MODULE = BarnOwl                PACKAGE = BarnOwl::Editwin
    518504
  • select.c

    r074bdaa rbcde7926  
    175175};
    176176
    177 int owl_select_add_perl_io_dispatch(int fd, int mode, SV *cb)
    178 {
    179   const owl_io_dispatch *d = owl_select_find_valid_io_dispatch_by_fd(fd);
    180   if (d != NULL && d->callback != owl_perlconfig_io_dispatch) {
    181     /* Don't mess with non-perl dispatch functions from here. */
    182     return 1;
    183   }
    184   /* Also remove any invalidated perl dispatch functions that may have
    185    * stuck around. */
    186   owl_select_remove_perl_io_dispatch(fd);
    187   owl_select_add_io_dispatch(fd, mode, owl_perlconfig_io_dispatch, owl_perlconfig_io_dispatch_destroy, cb);
    188   return 0;
    189 }
    190 
    191 static owl_io_dispatch *owl_select_find_perl_io_dispatch(int fd)
    192 {
    193   int i, len;
    194   const owl_list *dl;
    195   owl_io_dispatch *d;
    196   dl = owl_global_get_io_dispatch_list(&g);
    197   len = owl_list_get_size(dl);
    198   for(i = 0; i < len; i++) {
    199     d = owl_list_get_element(dl, i);
    200     if (d->fd == fd && d->callback == owl_perlconfig_io_dispatch)
    201       return d;
    202   }
    203   return NULL;
    204 }
    205 
    206 int owl_select_remove_perl_io_dispatch(int fd)
    207 {
    208   owl_io_dispatch *d = owl_select_find_perl_io_dispatch(fd);
    209   if (d != NULL) {
    210     /* Only remove perl io dispatchers from here. */
    211     owl_select_remove_io_dispatch(d);
    212     return 0;
    213   }
    214   return 1;
    215 }
    216 
    217177void owl_select_init(void)
    218178{
Note: See TracChangeset for help on using the changeset viewer.