File Coverage

blib/lib/Future/XS.pm
Criterion Covered Total %
statement 22 28 78.5
branch 2 4 50.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 31 39 79.4


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2022 -- leonerd@leonerd.org.uk
5              
6             package Future::XS 0.10;
7              
8 3     3   204347 use v5.14;
  3         31  
9 3     3   16 use warnings;
  3         7  
  3         134  
10              
11 3     3   18 use Carp;
  3         5  
  3         268  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16 3     3   67 use Time::HiRes qw( tv_interval );
  3         6  
  3         24  
17              
18             # Future::_base is provided in Future.pm itself
19             require Future;
20             our @ISA = qw( Future::_base );
21              
22             our @CARP_NOT = qw( Future Future::_base );
23              
24             require Future::Exception;
25              
26             =head1 NAME
27              
28             C - experimental XS implementation of C
29              
30             =head1 SYNOPSIS
31              
32             my $future = Future::XS->new;
33              
34             perform_some_operation(
35             on_complete => sub {
36             $future->done( @_ );
37             }
38             );
39              
40             $future->on_ready( sub {
41             say "The operation is complete";
42             } );
43              
44             =head1 DESCRIPTION
45              
46             This module provides an XS-based implementation of the L class. It is
47             currently experimental and shipped in its own distribution for testing
48             purposes, though once it seems stable the plan is to move it into the main
49             C distribution and load it automatically in favour of the pureperl
50             implementation on supported systems.
51              
52             =head2 Future::XS and threads
53              
54             In a program not involving multiple threads, this module behaves entirely as
55             expected, following the behaviour of the regular pure-perl C
56             implementation (regardless of whether or not the perl interpreter is actually
57             built to support threads).
58              
59             When multiple threads are created, previous versions of this module would most
60             likely crash. The current version (0.10) fixes enough of the logic, that
61             future instances that are only ever accessed from one thread (either the
62             initial main thread, or any additional sidecar threads) will work fine.
63             However, future instances cannot currently cross the boundary between threads.
64             Any instances that were created before a new thread is made will no longer be
65             accessible within that thread, and instances may not be returned as the result
66             of the thread exit value. Some of these restrictions may be relaxed in later
67             versions.
68              
69             Attempts to access a future instance created in one thread from another thread
70             will raise an exception:
71              
72             Future::XS instance IO::Async::Future=SCALAR(0x...) is not available in this thread at ...
73              
74             As a special case for process cleanup activities, the C<< ->cancel >> method
75             does not throw this exception, but simply returns silently. This is because
76             cleanup code such as C methods or C blocks often attempt to
77             call this on existing instances.
78              
79             =cut
80              
81             sub import
82             {
83 3     3   22 my $pkg = shift;
84 3         8 my $caller = caller;
85              
86 3         7 my %syms = map { $_ => 1 } @_;
  0         0  
87              
88 3 50       12 if( delete $syms{"-default"} ) {
89 0         0 require Future;
90              
91 3     3   761 no warnings 'redefine';
  3         13  
  3         163  
92 0         0 foreach my $name (qw( new done fail )) {
93 3     3   17 no strict 'refs';
  3         5  
  3         445  
94 0         0 *{"Future::${name}"} = \&{__PACKAGE__."::${name}"};
  0         0  
  0         0  
95             }
96             }
97              
98 3 50       1869 croak "Unrecognised $pkg\->import symbols - " . join( ", ", sort keys %syms )
99             if %syms;
100             }
101              
102             =head1 AUTHOR
103              
104             Paul Evans
105              
106             =cut
107              
108             0x55AA;