threads::shared disabling
Andy Bussey [Thu, 3 Jan 2002 00:46:58 +0000 (00:46 +0000)]
   Message-Id: <20020103004658.97969.qmail@web21006.mail.yahoo.com>

p4raw-link: @14015 on //depot/perl: 38b0d3dc02713f3c8313d8d82d06f680810942d4

p4raw-id: //depot/perl@14030

MANIFEST
ext/threads/shared/shared.pm
ext/threads/shared/t/no_share.t [new file with mode: 0644]
ext/threads/threads.pm

index 7420fc5..0113534 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -604,6 +604,7 @@ ext/threads/shared/shared.xs    thread shared variables
 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
index f8d04e2..1f2ad9d 100644 (file)
@@ -17,7 +17,7 @@ BEGIN {
        *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;
     }
 }
@@ -100,6 +100,9 @@ sub TIEHASH {
 }
 
 package threads::shared;
+
+$threads::shared::threads_shared = 1;
+
 bootstrap threads::shared $VERSION;
 
 __END__
@@ -205,6 +208,14 @@ 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
 
 C<bless> is not supported on shared references, in the current version
diff --git a/ext/threads/shared/t/no_share.t b/ext/threads/shared/t/no_share.t
new file mode 100644 (file)
index 0000000..519d9cb
--- /dev/null
@@ -0,0 +1,47 @@
+
+
+
+
+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");
+
index e92f1c9..444ec5b 100755 (executable)
@@ -10,6 +10,13 @@ use overload
 
 #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,7 +90,10 @@ must use threads::shared.
 
 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