"""Python Microthread Library, version 0.1 Microthreads are useful when you want to program many behaviors happening simultaneously. Simulations and games often want to model the simultaneous and independent behavior of many people, many businesses, many monsters, many physical objects, many spaceships, and so forth. With microthreads, you can code these behaviors as Python functions. Microthreads use Stackless Python. For more details, see http://world.std.com/~wware/uthread.html""" __version__ = "0.1" __license__ = \ """Python Microthread Library version 0.1 Copyright (C)2000 Will Ware, Christian Tismer Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of the authors not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. WILL WARE AND CHRISTIAN TISMER DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL WILL WARE OR CHRISTIAN TISMER BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.""" import stackless MAX_TIMESLICE = 20000000 # handled internally schedule = stackless.schedule def new(func, *args, **kw): return stackless.tasklet(func)(*args, **kw) idIndex = 0 def uniqueId(): """Microthread-safe way to get unique numbers, handy for giving things unique ID numbers""" global idIndex tmp = stackless.atomic() z = idIndex idIndex = z + 1 return z def irandom(n): """Microthread-safe version of random.randrange(0,n)""" import random tmp = stackless.atomic() n = random.randrange(0, n) return n class Semaphore: """Semaphores protect globally accessible resources from the effects of context switching.""" def __init__(self, maxcount=1): self.count = maxcount self.waiting = stackless.channel() def acquire(self): tmp = stackless.atomic() if self.count == 0: self.waiting.receive() else: self.count = self.count - 1 claim = acquire def release(self): tmp = stackless.atomic() if self.waiting.queue: self.waiting.send(None) else: self.count = self.count + 1 class Queue: """A queue is a microthread-safe FIFO.""" def __init__(self): self.contents = [ ] self.channel = stackless.channel() def put(self, x): tmp = stackless.atomic() self.contents.append(x) while self.channel.queue and self.contents: self.channel.send(self.contents.pop(0)) def get(self): tmp = stackless.atomic() if self.contents: return self.contents.pop(0) return self.channel.receive() def unget(self, x): tmp = stackless.atomic() self.contents.insert(0, x) def cget(self): return self.contents.pop(0)