[234] in BarnOwl Developers
[D-O-H] r409 - in trunk/conf.asedeno/owl: . modules
daemon@ATHENA.MIT.EDU (asedeno@darkmatter.mit.edu)
Thu Oct 29 18:03:58 2009
Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
To: dirty-owl-hackers@mit.edu
From: asedeno@darkmatter.mit.edu
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Fri, 27 Oct 2006 11:48:27 -0400 (EDT)
Author: asedeno
Date: 2006-10-27 11:48:27 -0400 (Fri, 27 Oct 2006)
New Revision: 409
Modified:
trunk/conf.asedeno/owl/modules/thread_example_1.pl
trunk/conf.asedeno/owl/owlconf.pl
Log:
Updates to the test threading code.
Modified: trunk/conf.asedeno/owl/modules/thread_example_1.pl
===================================================================
--- trunk/conf.asedeno/owl/modules/thread_example_1.pl 2006-10-27 05:19:54 UTC (rev 408)
+++ trunk/conf.asedeno/owl/modules/thread_example_1.pl 2006-10-27 15:48:27 UTC (rev 409)
@@ -1,16 +1,49 @@
package thread_example_1;
use threads;
use Thread::Queue;
+use Thread::Semaphore;
+my $semaphore = new Thread::Semaphore(0);
+
sub onStart
{
- threads->new(sub {
- my $i = 0;
- while ($i++ < 10)
+ threads->new(\&threadLoop)->detach();
+}
+push @::onStartSubs, \&onStart;
+
+
+sub onMainLoop
+{
+ $semaphore->up();
+# threads->yield();
+ $semaphore->down();
+}
+push @::onMainLoop, \&onMainLoop;
+
+
+sub threadLoop()
+{
+ my $i = 0;
+ my $now = time();
+ my $next = $now + 3;
+
+ while (1)
+ {
+ $semaphore->down();
+ if ($::shutdown)
{
- $::queue->enqueue($i);
- sleep(5);
+ owl::command(qq/aperl "Thread exiting."/);
+ $semaphore->up();
+ return;
}
- })->detach();
+
+ $now = time();
+ if ($now > $next)
+ {
+ $next = $now + 3;
+ owl::command("aperl $now");
+ }
+ $semaphore->up();
+ }
}
-push @::onStartSubs, \&onStart;
+
Modified: trunk/conf.asedeno/owl/owlconf.pl
===================================================================
--- trunk/conf.asedeno/owl/owlconf.pl 2006-10-27 05:19:54 UTC (rev 408)
+++ trunk/conf.asedeno/owl/owlconf.pl 2006-10-27 15:48:27 UTC (rev 409)
@@ -49,28 +49,33 @@
###
################################################################################
+
+# Arrays of function pointers to be called at specific times.
+our @onStartSubs = undef;
+our @onReceiveMsg = undef;
+our @onModuleHelp = undef;
+our @onMainLoop = undef;
+
################################################################################
-# Module Event Queue and Mainloop hook.
-#
-# This is a prototype. I need to figure out how to do more clever things with
-# this. For now, it simply takes any scalars that have been enqueued and
-# outputs them using aperl. We're going to need real message injection into
-# owl to make this really useful. We're also probably going to want to enqueue
-# functions, I think.
+# Mainloop hook and threading.
################################################################################
use threads;
+use threads::shared;
use Thread::Queue;
-our $queue;
-$queue = new Thread::Queue if ($queue == undef);
+# Shared thread shutdown flag.
+# Consider adding a reload flag, so threads that should persist across reloads
+# can distinguish the two events. We wouldn't want a reload to cause us to
+# log out of and in to a perl-based IM session.
+our $shutdown : shared;
+$shutdown = 0;
sub owl::mainloop_hook
{
- if ($queue->pending)
+ foreach (@onMainLoop)
{
- my $qobj = $queue->dequeue_nb();
- owl::command("aperl $qobj");
+ &$_();
}
return;
}
@@ -89,11 +94,17 @@
onStart();
}
-# Arrays of function pointers to be called at specific times.
-@onStartSubs = undef;
-@onReceiveMsg = undef;
-@onModuleHelp = undef;
+sub owl::shutdown
+{
+# Modern versions of owl provides a great place to have shutdown stuff.
+# Put things in ~/.owl/shutdown
+# At this point I use owl::shutdown to tell any auxillary threads that they
+# should terminate.
+ $shutdown = 1;
+ owl::mainloop_hook();
+}
+
#Run this on start and reload. Adds modules and runs their startup functions.
sub onStart
{
@@ -110,6 +121,7 @@
@onStartSubs = ();
@onReceiveMsg = ();
@onModuleHelp = ();
+ @onMainLoop = ();
loadModules();
foreach (@onStartSubs)
@@ -117,8 +129,8 @@
&$_();
}
- # Hook ourselves into owl's main loop.
owl::command(qq/set -q perl_mainloop_hook "owl::mainloop_hook"/);
+
}
################################################################################
# Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
@@ -135,6 +147,12 @@
sub reload
{
+ # Shutdown existing threads.
+ $shutdown = 1;
+ owl::mainloop_hook();
+ $shutdown = 0;
+
+ # Do reload
if (do "$ENV{HOME}/.owlconf" && reload_hook(@_))
{
return "owlconf reloaded";