* src/pbc_merge.c:
[parrot.git] / examples / io / async_select.pir
blobd23a7972fe5804743661b74f082d98bfd237cfb8
1 # Copyright (C) 2006-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 examples/io/async_select.pir - Asynchronous Select Example
8 =head1 SYNOPSIS
10   $ ./parrot examples/io/async_select.pir
12 =head1 DESCRIPTION
14 This program is showing async select utilizing the io_thread event handler.
15 After you started the program, you will see a spinning progress marker,
16 showing that the program is idle. Type some chars at the console (due
17 to console buffering, you have to press <enter> too).
18 These chars will be displayed in bunches of four, as long as more chars are
19 available.
21 =cut
23 .include 'io_thr_msg.pasm'
25 .sub main :main
26     .local pmc handler, pio, data, pio_out
27     .local string idles
28     .local int i
30     pio = getstdin
31     $S0 = pop pio   # unbuffer
32     data = new 'Integer'
33     data = 42
34     .const .Sub handler = "io_handler"
35     pio_out = getstdout
36     idles = '|/-\'
37     i = 0
38 spin:
39     # the IO event is inactive, after it fired, just reattach always
40     add_io_event pio, handler, data, .IO_THR_MSG_ADD_SELECT_RD
41     sleep 0.2
42     $S0 = idles[i]
43     inc i
44     $I0 = length idles
45     i %= $I0
46     print $S0
47     print "\r"
48     pio_out.'flush'()
49     goto spin
50 .end
52 # io_handler called by the select ready condition from event code
53 .sub io_handler
54     .param pmc pio
55     .param pmc data
56     $S0 = read pio, 4   # arbitray - could of course read more
57     $I0 = length $S0
58     unless $I0 goto ex
59     print $S0
60     print " - "
61     print data
62     print "\n"
63     .return()
64 ex:
65     exit 0
66 .end
68 # Local Variables:
69 #   mode: pir
70 #   fill-column: 100
71 # End:
72 # vim: expandtab shiftwidth=4 ft=pir: