2002-07-04 02:02:10 +02:00
|
|
|
# Copyright (C) 2002 John Goerzen
|
|
|
|
# Thread support module
|
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2002-07-04 03:35:05 +02:00
|
|
|
from threading import *
|
2002-07-05 04:34:39 +02:00
|
|
|
from StringIO import StringIO
|
2002-07-11 08:53:09 +02:00
|
|
|
import sys, traceback, thread
|
2002-07-05 04:34:39 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# General utilities
|
|
|
|
######################################################################
|
2002-07-04 02:02:10 +02:00
|
|
|
|
2002-07-04 03:35:05 +02:00
|
|
|
def semaphorereset(semaphore, originalstate):
|
2002-07-04 02:02:10 +02:00
|
|
|
"""Wait until the semaphore gets back to its original state -- all acquired
|
|
|
|
resources released."""
|
|
|
|
for i in range(originalstate):
|
|
|
|
semaphore.acquire()
|
|
|
|
# Now release these.
|
|
|
|
for i in range(originalstate):
|
|
|
|
semaphore.release()
|
|
|
|
|
2002-07-04 03:35:05 +02:00
|
|
|
def semaphorewait(semaphore):
|
|
|
|
semaphore.acquire()
|
|
|
|
semaphore.release()
|
|
|
|
|
|
|
|
def threadsreset(threadlist):
|
2002-07-11 08:53:09 +02:00
|
|
|
for thr in threadlist:
|
|
|
|
thr.join()
|
2002-07-04 05:59:19 +02:00
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
######################################################################
|
|
|
|
# Exit-notify threads
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
exitcondition = Condition(Lock())
|
2002-07-11 11:42:27 +02:00
|
|
|
exitthreads = []
|
2002-07-05 04:34:39 +02:00
|
|
|
inited = 0
|
|
|
|
|
|
|
|
def initexitnotify():
|
|
|
|
"""Initialize the exit notify system. This MUST be called from the
|
|
|
|
SAME THREAD that will call monitorloop BEFORE it calls monitorloop.
|
|
|
|
This SHOULD be called before the main thread starts any other
|
|
|
|
ExitNotifyThreads, or else it may miss the ability to catch the exit
|
|
|
|
status from them!"""
|
2002-07-11 11:42:27 +02:00
|
|
|
pass
|
2002-07-05 04:34:39 +02:00
|
|
|
|
|
|
|
def exitnotifymonitorloop(callback):
|
|
|
|
"""Enter an infinite "monitoring" loop. The argument, callback,
|
|
|
|
defines the function to call when an ExitNotifyThread has terminated.
|
|
|
|
That function is called with a single argument -- the ExitNotifyThread
|
|
|
|
that has terminated. The monitor will not continue to monitor for
|
|
|
|
other threads until the function returns, so if it intends to perform
|
|
|
|
long calculations, it should start a new thread itself -- but NOT
|
|
|
|
an ExitNotifyThread, or else an infinite loop may result. Furthermore,
|
|
|
|
the monitor will hold the lock all the while the other thread is waiting.
|
|
|
|
"""
|
2002-07-11 11:42:27 +02:00
|
|
|
global exitcondition, exitthreads
|
2002-07-05 04:34:39 +02:00
|
|
|
while 1: # Loop forever.
|
2002-07-11 11:42:27 +02:00
|
|
|
exitcondition.acquire()
|
|
|
|
while not len(exitthreads):
|
2002-07-05 04:34:39 +02:00
|
|
|
exitcondition.wait(1)
|
2002-07-11 11:42:27 +02:00
|
|
|
|
|
|
|
while len(exitthreads):
|
|
|
|
callback(exitthreads.pop())
|
|
|
|
exitcondition.release()
|
2002-07-05 04:34:39 +02:00
|
|
|
|
|
|
|
class ExitNotifyThread(Thread):
|
|
|
|
"""This class is designed to alert a "monitor" to the fact that a thread has
|
|
|
|
exited and to provide for the ability for it to find out why."""
|
|
|
|
def run(self):
|
2002-07-11 11:42:27 +02:00
|
|
|
global exitcondition, exitthreads
|
2002-07-11 08:53:09 +02:00
|
|
|
self.threadid = thread.get_ident()
|
2002-07-05 04:34:39 +02:00
|
|
|
try:
|
|
|
|
Thread.run(self)
|
|
|
|
except:
|
|
|
|
self.setExitCause('EXCEPTION')
|
|
|
|
self.setExitException(sys.exc_info()[1])
|
|
|
|
sbuf = StringIO()
|
|
|
|
traceback.print_exc(file = sbuf)
|
|
|
|
self.setExitStackTrace(sbuf.getvalue())
|
|
|
|
else:
|
|
|
|
self.setExitCause('NORMAL')
|
|
|
|
if not hasattr(self, 'exitmessage'):
|
|
|
|
self.setExitMessage(None)
|
|
|
|
exitcondition.acquire()
|
2002-07-11 11:42:27 +02:00
|
|
|
exitthreads.append(self)
|
2002-07-05 04:34:39 +02:00
|
|
|
exitcondition.notify()
|
|
|
|
exitcondition.release()
|
|
|
|
|
|
|
|
def setExitCause(self, cause):
|
|
|
|
self.exitcause = cause
|
|
|
|
def getExitCause(self):
|
|
|
|
"""Returns the cause of the exit, one of:
|
|
|
|
'EXCEPTION' -- the thread aborted because of an exception
|
|
|
|
'NORMAL' -- normal termination."""
|
|
|
|
return self.exitcause
|
|
|
|
def setExitException(self, exc):
|
|
|
|
self.exitexception = exc
|
|
|
|
def getExitException(self):
|
|
|
|
"""If getExitCause() is 'EXCEPTION', holds the value from
|
|
|
|
sys.exc_info()[1] for this exception."""
|
|
|
|
return self.exitexception
|
|
|
|
def setExitStackTrace(self, st):
|
|
|
|
self.exitstacktrace = st
|
|
|
|
def getExitStackTrace(self):
|
|
|
|
"""If getExitCause() is 'EXCEPTION', returns a string representing
|
|
|
|
the stack trace for this exception."""
|
|
|
|
return self.exitstacktrace
|
|
|
|
def setExitMessage(self, msg):
|
|
|
|
"""Sets the exit message to be fetched by a subsequent call to
|
|
|
|
getExitMessage. This message may be any object or type except
|
|
|
|
None."""
|
|
|
|
self.exitmessage = msg
|
|
|
|
def getExitMessage(self):
|
|
|
|
"""For any exit cause, returns the message previously set by
|
|
|
|
a call to setExitMessage(), or None if there was no such message
|
|
|
|
set."""
|
|
|
|
return self.exitmessage
|
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Instance-limited threads
|
|
|
|
######################################################################
|
|
|
|
|
2002-07-04 05:59:19 +02:00
|
|
|
instancelimitedsems = {}
|
|
|
|
instancelimitedlock = Lock()
|
|
|
|
|
|
|
|
def initInstanceLimit(instancename, instancemax):
|
2002-07-05 04:34:39 +02:00
|
|
|
"""Initialize the instance-limited thread implementation to permit
|
|
|
|
up to intancemax threads with the given instancename."""
|
2002-07-04 05:59:19 +02:00
|
|
|
instancelimitedlock.acquire()
|
|
|
|
if not instancelimitedsems.has_key(instancename):
|
|
|
|
instancelimitedsems[instancename] = BoundedSemaphore(instancemax)
|
|
|
|
instancelimitedlock.release()
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
class InstanceLimitedThread(ExitNotifyThread):
|
2002-07-04 05:59:19 +02:00
|
|
|
def __init__(self, instancename, *args, **kwargs):
|
|
|
|
self.instancename = instancename
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
apply(ExitNotifyThread.__init__, (self,) + args, kwargs)
|
2002-07-04 05:59:19 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
instancelimitedsems[self.instancename].acquire()
|
2002-07-05 04:34:39 +02:00
|
|
|
ExitNotifyThread.start(self)
|
2002-07-04 05:59:19 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
2002-07-05 04:34:39 +02:00
|
|
|
ExitNotifyThread.run(self)
|
2002-07-04 05:59:19 +02:00
|
|
|
finally:
|
|
|
|
instancelimitedsems[self.instancename].release()
|
|
|
|
|
|
|
|
|