mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
nannified
This commit is contained in:
parent
f9a6d7d494
commit
4117e5428b
22 changed files with 1634 additions and 1633 deletions
|
@ -40,64 +40,64 @@ class Lock:
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor. Initialize all instance variables."""
|
||||
self.__mutex = thread.allocate_lock()
|
||||
self.__wait = thread.allocate_lock()
|
||||
self.__tid = None
|
||||
self.__count = 0
|
||||
"""Constructor. Initialize all instance variables."""
|
||||
self.__mutex = thread.allocate_lock()
|
||||
self.__wait = thread.allocate_lock()
|
||||
self.__tid = None
|
||||
self.__count = 0
|
||||
|
||||
def acquire(self, flag=1):
|
||||
"""Acquire the lock.
|
||||
"""Acquire the lock.
|
||||
|
||||
If the optional flag argument is false, returns immediately
|
||||
when it cannot acquire the __wait lock without blocking (it
|
||||
may still block for a little while in order to acquire the
|
||||
__mutex lock).
|
||||
If the optional flag argument is false, returns immediately
|
||||
when it cannot acquire the __wait lock without blocking (it
|
||||
may still block for a little while in order to acquire the
|
||||
__mutex lock).
|
||||
|
||||
The return value is only relevant when the flag argument is
|
||||
false; it is 1 if the lock is acquired, 0 if not.
|
||||
The return value is only relevant when the flag argument is
|
||||
false; it is 1 if the lock is acquired, 0 if not.
|
||||
|
||||
"""
|
||||
self.__mutex.acquire()
|
||||
try:
|
||||
if self.__tid == thread.get_ident():
|
||||
self.__count = self.__count + 1
|
||||
return 1
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
locked = self.__wait.acquire(flag)
|
||||
if not flag and not locked:
|
||||
return 0
|
||||
try:
|
||||
self.__mutex.acquire()
|
||||
assert self.__tid == None
|
||||
assert self.__count == 0
|
||||
self.__tid = thread.get_ident()
|
||||
self.__count = 1
|
||||
return 1
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
"""
|
||||
self.__mutex.acquire()
|
||||
try:
|
||||
if self.__tid == thread.get_ident():
|
||||
self.__count = self.__count + 1
|
||||
return 1
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
locked = self.__wait.acquire(flag)
|
||||
if not flag and not locked:
|
||||
return 0
|
||||
try:
|
||||
self.__mutex.acquire()
|
||||
assert self.__tid == None
|
||||
assert self.__count == 0
|
||||
self.__tid = thread.get_ident()
|
||||
self.__count = 1
|
||||
return 1
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
|
||||
def release(self):
|
||||
"""Release the lock.
|
||||
"""Release the lock.
|
||||
|
||||
If this thread doesn't currently have the lock, an assertion
|
||||
error is raised.
|
||||
If this thread doesn't currently have the lock, an assertion
|
||||
error is raised.
|
||||
|
||||
Only allow another thread to acquire the lock when the count
|
||||
reaches zero after decrementing it.
|
||||
Only allow another thread to acquire the lock when the count
|
||||
reaches zero after decrementing it.
|
||||
|
||||
"""
|
||||
self.__mutex.acquire()
|
||||
try:
|
||||
assert self.__tid == thread.get_ident()
|
||||
assert self.__count > 0
|
||||
self.__count = self.__count - 1
|
||||
if self.__count == 0:
|
||||
self.__tid = None
|
||||
self.__wait.release()
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
"""
|
||||
self.__mutex.acquire()
|
||||
try:
|
||||
assert self.__tid == thread.get_ident()
|
||||
assert self.__count > 0
|
||||
self.__count = self.__count - 1
|
||||
if self.__count == 0:
|
||||
self.__tid = None
|
||||
self.__wait.release()
|
||||
finally:
|
||||
self.__mutex.release()
|
||||
|
||||
|
||||
def _testLock():
|
||||
|
@ -105,23 +105,23 @@ def _testLock():
|
|||
done = []
|
||||
|
||||
def f2(lock, done=done):
|
||||
lock.acquire()
|
||||
print "f2 running in thread %d\n" % thread.get_ident(),
|
||||
lock.release()
|
||||
done.append(1)
|
||||
lock.acquire()
|
||||
print "f2 running in thread %d\n" % thread.get_ident(),
|
||||
lock.release()
|
||||
done.append(1)
|
||||
|
||||
def f1(lock, f2=f2, done=done):
|
||||
lock.acquire()
|
||||
print "f1 running in thread %d\n" % thread.get_ident(),
|
||||
try:
|
||||
f2(lock)
|
||||
finally:
|
||||
lock.release()
|
||||
done.append(1)
|
||||
lock.acquire()
|
||||
print "f1 running in thread %d\n" % thread.get_ident(),
|
||||
try:
|
||||
f2(lock)
|
||||
finally:
|
||||
lock.release()
|
||||
done.append(1)
|
||||
|
||||
lock = Lock()
|
||||
lock.acquire()
|
||||
f1(lock) # Adds 2 to done
|
||||
f1(lock) # Adds 2 to done
|
||||
lock.release()
|
||||
|
||||
lock.acquire()
|
||||
|
@ -134,8 +134,8 @@ def _testLock():
|
|||
lock.release()
|
||||
import time
|
||||
while len(done) < 9:
|
||||
print len(done)
|
||||
time.sleep(0.001)
|
||||
print len(done)
|
||||
time.sleep(0.001)
|
||||
print len(done)
|
||||
|
||||
|
||||
|
@ -147,19 +147,19 @@ from Meta import MetaClass, MetaHelper, MetaMethodWrapper
|
|||
|
||||
class LockingMethodWrapper(MetaMethodWrapper):
|
||||
def __call__(self, *args, **kw):
|
||||
if self.__name__[:1] == '_' and self.__name__[1:] != '_':
|
||||
return apply(self.func, (self.inst,) + args, kw)
|
||||
self.inst.__lock__.acquire()
|
||||
try:
|
||||
return apply(self.func, (self.inst,) + args, kw)
|
||||
finally:
|
||||
self.inst.__lock__.release()
|
||||
if self.__name__[:1] == '_' and self.__name__[1:] != '_':
|
||||
return apply(self.func, (self.inst,) + args, kw)
|
||||
self.inst.__lock__.acquire()
|
||||
try:
|
||||
return apply(self.func, (self.inst,) + args, kw)
|
||||
finally:
|
||||
self.inst.__lock__.release()
|
||||
|
||||
class LockingHelper(MetaHelper):
|
||||
__methodwrapper__ = LockingMethodWrapper
|
||||
def __helperinit__(self, formalclass):
|
||||
MetaHelper.__helperinit__(self, formalclass)
|
||||
self.__lock__ = Lock()
|
||||
MetaHelper.__helperinit__(self, formalclass)
|
||||
self.__lock__ = Lock()
|
||||
|
||||
class LockingMetaClass(MetaClass):
|
||||
__helper__ = LockingHelper
|
||||
|
@ -169,73 +169,73 @@ Locking = LockingMetaClass('Locking', (), {})
|
|||
def _test():
|
||||
# For kicks, take away the Locking base class and see it die
|
||||
class Buffer(Locking):
|
||||
def __init__(self, initialsize):
|
||||
assert initialsize > 0
|
||||
self.size = initialsize
|
||||
self.buffer = [None]*self.size
|
||||
self.first = self.last = 0
|
||||
def put(self, item):
|
||||
# Do we need to grow the buffer?
|
||||
if (self.last+1) % self.size != self.first:
|
||||
# Insert the new item
|
||||
self.buffer[self.last] = item
|
||||
self.last = (self.last+1) % self.size
|
||||
return
|
||||
# Double the buffer size
|
||||
# First normalize it so that first==0 and last==size-1
|
||||
print "buffer =", self.buffer
|
||||
print "first = %d, last = %d, size = %d" % (
|
||||
self.first, self.last, self.size)
|
||||
if self.first <= self.last:
|
||||
temp = self.buffer[self.first:self.last]
|
||||
else:
|
||||
temp = self.buffer[self.first:] + self.buffer[:self.last]
|
||||
print "temp =", temp
|
||||
self.buffer = temp + [None]*(self.size+1)
|
||||
self.first = 0
|
||||
self.last = self.size-1
|
||||
self.size = self.size*2
|
||||
print "Buffer size doubled to", self.size
|
||||
print "new buffer =", self.buffer
|
||||
print "first = %d, last = %d, size = %d" % (
|
||||
self.first, self.last, self.size)
|
||||
self.put(item) # Recursive call to test the locking
|
||||
def get(self):
|
||||
# Is the buffer empty?
|
||||
if self.first == self.last:
|
||||
raise EOFError # Avoid defining a new exception
|
||||
item = self.buffer[self.first]
|
||||
self.first = (self.first+1) % self.size
|
||||
return item
|
||||
def __init__(self, initialsize):
|
||||
assert initialsize > 0
|
||||
self.size = initialsize
|
||||
self.buffer = [None]*self.size
|
||||
self.first = self.last = 0
|
||||
def put(self, item):
|
||||
# Do we need to grow the buffer?
|
||||
if (self.last+1) % self.size != self.first:
|
||||
# Insert the new item
|
||||
self.buffer[self.last] = item
|
||||
self.last = (self.last+1) % self.size
|
||||
return
|
||||
# Double the buffer size
|
||||
# First normalize it so that first==0 and last==size-1
|
||||
print "buffer =", self.buffer
|
||||
print "first = %d, last = %d, size = %d" % (
|
||||
self.first, self.last, self.size)
|
||||
if self.first <= self.last:
|
||||
temp = self.buffer[self.first:self.last]
|
||||
else:
|
||||
temp = self.buffer[self.first:] + self.buffer[:self.last]
|
||||
print "temp =", temp
|
||||
self.buffer = temp + [None]*(self.size+1)
|
||||
self.first = 0
|
||||
self.last = self.size-1
|
||||
self.size = self.size*2
|
||||
print "Buffer size doubled to", self.size
|
||||
print "new buffer =", self.buffer
|
||||
print "first = %d, last = %d, size = %d" % (
|
||||
self.first, self.last, self.size)
|
||||
self.put(item) # Recursive call to test the locking
|
||||
def get(self):
|
||||
# Is the buffer empty?
|
||||
if self.first == self.last:
|
||||
raise EOFError # Avoid defining a new exception
|
||||
item = self.buffer[self.first]
|
||||
self.first = (self.first+1) % self.size
|
||||
return item
|
||||
|
||||
def producer(buffer, wait, n=1000):
|
||||
import time
|
||||
i = 0
|
||||
while i < n:
|
||||
print "put", i
|
||||
buffer.put(i)
|
||||
i = i+1
|
||||
print "Producer: done producing", n, "items"
|
||||
wait.release()
|
||||
import time
|
||||
i = 0
|
||||
while i < n:
|
||||
print "put", i
|
||||
buffer.put(i)
|
||||
i = i+1
|
||||
print "Producer: done producing", n, "items"
|
||||
wait.release()
|
||||
|
||||
def consumer(buffer, wait, n=1000):
|
||||
import time
|
||||
i = 0
|
||||
tout = 0.001
|
||||
while i < n:
|
||||
try:
|
||||
x = buffer.get()
|
||||
if x != i:
|
||||
raise AssertionError, \
|
||||
"get() returned %s, expected %s" % (x, i)
|
||||
print "got", i
|
||||
i = i+1
|
||||
tout = 0.001
|
||||
except EOFError:
|
||||
time.sleep(tout)
|
||||
tout = tout*2
|
||||
print "Consumer: done consuming", n, "items"
|
||||
wait.release()
|
||||
import time
|
||||
i = 0
|
||||
tout = 0.001
|
||||
while i < n:
|
||||
try:
|
||||
x = buffer.get()
|
||||
if x != i:
|
||||
raise AssertionError, \
|
||||
"get() returned %s, expected %s" % (x, i)
|
||||
print "got", i
|
||||
i = i+1
|
||||
tout = 0.001
|
||||
except EOFError:
|
||||
time.sleep(tout)
|
||||
tout = tout*2
|
||||
print "Consumer: done consuming", n, "items"
|
||||
wait.release()
|
||||
|
||||
pwait = thread.allocate_lock()
|
||||
pwait.acquire()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue