develooper Front page | perl.perl5.porters | Postings from December 2001

[PATCH @13811] threads::shared disabling

From:
=?iso-8859-1?q?Andy=20Bussey?=
Date:
December 20, 2001 17:20
Subject:
[PATCH @13811] threads::shared disabling
Message ID:
20011221012031.52908.qmail@web21003.mail.yahoo.com
This is a patch to:
- make the documentation in threads.pm and threads::shared.pm
  more explicit if threads::shared is 'use'd first
- produce a warning if threads is 'use'd after threads::shared
- test for this warning and some simple behaviour if
  threads::shared is supposed to be disabled.
- correct what I think is slight typo (dosabled for disabled) -
  though some may want to argue with that ;-)

I have to Carp::confess("I'm not very familiar with tests as 
run at perl installation and don't know if I'm really allowed 
to set up a __WARN__ hook etc").

Even if everything is OK with it, I think Arthur should say if
he's happy with it.

Andy


#### Patch data follows ####
diff -u 'perl@13811.orig/MANIFEST' 'perl@13811/MANIFEST'
Index: ./MANIFEST
--- ./MANIFEST	Wed Dec 19 15:45:42 2001
+++ ./MANIFEST	Fri Dec 21 00:21:24 2001
@@ -599,6 +599,7 @@
 ext/threads/shared/t/av_simple.t	Tests for basic shared array functionality.
 ext/threads/shared/t/hv_refs.t	Test shared hashes containing references
 ext/threads/shared/t/hv_simple.t	Tests for basic shared hash functionality.
+ext/threads/shared/t/no_share.t	Tests for disabled share on variables
 ext/threads/shared/t/sv_refs.t	thread shared variables
 ext/threads/shared/t/sv_simple.t	thread shared variables
 ext/threads/t/basic.t		ithreads
diff -u 'perl@13811.orig/ext/threads/shared/shared.pm' 'perl@13811/ext/threads/shared/shared.pm'
Index: ./ext/threads/shared/shared.pm
--- ./ext/threads/shared/shared.pm	Sun Dec 16 05:43:55 2001
+++ ./ext/threads/shared/shared.pm	Thu Dec 20 23:25:49 2001
@@ -17,7 +17,7 @@
 	*share = \&share_disabled;
 	*cond_wait = \&cond_wait_disabled;
 	*cond_signal = \&cond_signal_disabled;
-	*cond_broadcast = \&cond_broadcast_dosabled;
+	*cond_broadcast = \&cond_broadcast_disabled;
 	*unlock = \&unlock_disabled;
     }
 }
@@ -99,6 +99,8 @@
     return $self;
 }
 
+$threads::shared::threads_shared = 1;
+
 package threads::shared;
 bootstrap threads::shared $VERSION;
 
@@ -204,6 +206,13 @@
 The C<cond_broadcast> function works similarly to C<cond_signal>.
 C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
 in a C<cond_wait> on the locked variable, rather than only one.
+
+=head1 NOTES
+
+threads::shared is designed is disable itself silently if threads are
+not available. If you want access to threads, you must C<use threads>
+before you C<use threads::shared>.  threads will emit a warning if you
+use it before threads::shared.
 
 =head1 BUGS
 
diff -u /dev/null 'perl@13811/ext/threads/shared/t/no_share.t'
Index: ./ext/threads/shared/t/no_share.t
--- ./ext/threads/shared/t/no_share.t	Thu Jan  1 01:00:00 1970
+++ ./ext/threads/shared/t/no_share.t	Fri Dec 21 00:33:51 2001
@@ -0,0 +1,52 @@
+
+
+
+
+BEGIN {
+#    chdir 't' if -d 't';
+#    push @INC ,'../lib';
+    require Config; import Config;
+    unless ($Config{'useithreads'}) {
+        print "1..0 # Skip: no useithreads\n";
+        exit 0;
+    }
+    $SIG{__WARN__} = sub { $warnmsg = shift; };
+}
+
+
+sub ok {
+    my ($id, $ok, $name) = @_;
+
+    # You have to do it this way or VMS will get confused.
+    print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
+
+    printf "# Failed test at line %d\n", (caller)[2] unless $ok;
+
+    return $ok;
+}
+
+our $warnmsg;
+use ExtUtils::testlib;
+use strict;
+BEGIN { print "1..5\n" };
+use threads::shared;
+use threads;
+ok(1,1,"loaded");
+ok(2,$warnmsg =~ /Warning, threads::shared has already been loaded/,
+    "threads has warned us"); 
+my $test = "bar";
+share($test);
+ok(3,$test eq "bar","Test disabled share not interfering");
+threads->create(
+		sub {
+		    ok(4,$test eq "bar","Test disabled share after thread");
+		    $test = "baz";
+		    })->join();
+ok(5,$test eq "bar","Test that value hasn't changed in another thread");
+
+
+
+
+
+
+
diff -u 'perl@13811.orig/ext/threads/threads.pm' 'perl@13811/ext/threads/threads.pm'
Index: ./ext/threads/threads.pm
--- ./ext/threads/threads.pm	Sun Dec 16 05:43:55 2001
+++ ./ext/threads/threads.pm	Fri Dec 21 00:22:22 2001
@@ -10,6 +10,13 @@
 
 #use threads::Shared;
 
+BEGIN {
+    warn "Warning, threads::shared has already been loaded. ".
+	"To enable shared variables for these modules 'use threads' ".
+	"must be called before any of those modules are loaded\n"
+		if($threads::shared::threads_shared);
+}
+
 require Exporter;
 require DynaLoader;
 
@@ -83,8 +90,11 @@
 
 It is also important to note that you preferably enable threads by
 doing C<use threads> as early as possible and that it is not possible
-to enable threading inside an eval "";
-
+to enable threading inside an eval "";  In particular, if you are
+intending to share variables with threads::shared, you must
+C<use threads> before you C<use threads::shared> and threads will emit
+a warning if you do it the other way around.
+ 
 =over
 
 =item $thread = new(function, LIST)
#### End of Patch data ####


=====
-------------------------------------------------------
Andy Bussey
andybussey@yahoo.com

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About