/
/
usr/
lib64/
lib64/
/
lib64/
/
python2.7/ó
V
Qc @ sÈ d Z d d l Z d d d „ ƒ YZ d „ Z d d l m Z m Z m Z d e f d „ ƒ YZ d e f d
„ ƒ YZ d e f d „ ƒ YZ
e
d
d i ƒ Z d „ Z e
d k rÄ e ƒ e ƒ n d S( s` Synchronization metaclass.
This metaclass makes it possible to declare synchronized methods.
iÿÿÿÿNt Lockc B s, e Z d Z d „ Z d d „ Z d „ Z RS( s Reentrant lock.
This is a mutex-like object which can be acquired by the same
thread more than once. It keeps a reference count of the number
of times it has been acquired by the same thread. Each acquire()
call must be matched by a release() call and only the last
release() call actually releases the lock for acquisition by
another thread.
The implementation uses two locks internally:
__mutex is a short term lock used to protect the instance variables
__wait is the lock for which other threads wait
A thread intending to acquire both locks should acquire __wait
first.
The implementation uses two other instance variables, protected by
locking __mutex:
__tid is the thread ID of the thread that currently has the lock
__count is the number of times the current thread has acquired it
When the lock is released, __tid is None and __count is zero.
c C s4 t j ƒ | _ t j ƒ | _ d | _ d | _ d S( s0 Constructor. Initialize all instance variables.i N( t threadt
allocate_lockt _Lock__mutext _Lock__waitt Nonet
_Lock__tidt _Lock__count( t self( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt __init__* s i c C sÛ | j j ƒ z- | j t j ƒ k r9 | j d | _ d SWd | j j ƒ X| j j | ƒ } | ro | ro d SzW | j j ƒ | j d k s” t ‚ | j d k s© t ‚ t j ƒ | _ d | _ d SWd | j j ƒ Xd S( s} 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).
The return value is only relevant when the flag argument is
false; it is 1 if the lock is acquired, 0 if not.
i Ni (
R t acquireR R t get_identR t releaseR R t AssertionError( R t flagt locked( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR
1 s"
c C sŽ | j j ƒ zl | j t j ƒ k s+ t ‚ | j d k s@ t ‚ | j d | _ | j d k rx d | _ | j j ƒ n Wd | j j ƒ Xd S( sì Release the lock.
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.
i i N(
R R
R R R R
R R R R ( R ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR Q s
( t __name__t
__module__t __doc__R R
R ( ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR
s c C s÷ g } | d „ } | | d „ } t ƒ } | j ƒ | | ƒ | j ƒ | j ƒ t j | | f ƒ t j | | | f ƒ t j | | f ƒ t j | | f ƒ | j ƒ d d l } x. t | ƒ d k rç t | ƒ GH| j d ƒ qº Wt | ƒ GHd S( Nc S s3 | j ƒ d t j ƒ G| j ƒ | j d ƒ d S( Ns f2 running in thread %d
i ( R
R R R t append( t lockt done( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt f2k s
c S sE | j ƒ d t j ƒ Gz | | ƒ Wd | j ƒ X| j d ƒ d S( Ns f1 running in thread %d
i ( R
R R R R ( R R R ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt f1q s
iÿÿÿÿi gü©ñÒMbP?( R R
R R t start_new_threadt timet lent sleep( R R R R R ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt _testLockg s$
( t MetaClasst
MetaHelpert MetaMethodWrappert LockingMethodWrapperc B s e Z d „ Z RS( c O sŒ | j d d k rC | j d d k rC t | j | j f | | ƒ S| j j j ƒ z! t | j | j f | | ƒ SWd | j j j ƒ Xd S( Ni t _( R t applyt funct instt __lock__R
R ( R t argst kw( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt __call__• s &!( R R R( ( ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR ” s t
LockingHelperc B s e Z e Z d „ Z RS( c C s t j | | ƒ t ƒ | _ d S( N( R t __helperinit__R R% ( R t formalclass( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR* s ( R R R t __methodwrapper__R* ( ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR) ž s t LockingMetaClassc B s e Z e Z RS( ( R R R) t
__helper__( ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR- ¤ s t Lockingc C sÒ d t f d „ ƒ Y} d d „ } d d „ } t j ƒ } | j ƒ t j ƒ } | j ƒ | d ƒ } d } t j | | | | f ƒ t j | | | | f ƒ | j ƒ d GH| j ƒ d GHd Gt | j ƒ GHd S(
Nt Bufferc B s# e Z d „ Z d „ Z d „ Z RS( c S sB | d k s t ‚ | | _ d g | j | _ d | _ | _ d S( Ni ( R
t sizeR t buffert firstt last( R t initialsize( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR ¬ s c S sI | j d | j | j k rH | | j | j <| j d | j | _ d Sd G| j GHd | j | j | j f GH| j | j k rš | j | j | j !} n | j | j | j | j } d G| GH| d g | j d | _ d | _ | j d | _ | j d | _ d G| j GHd G| j GHd | j | j | j f GH| j | ƒ d S( Ni s buffer =s first = %d, last = %d, size = %ds temp =i i s Buffer size doubled tos new buffer =( R4 R1 R3 R2 R t put( R t itemt temp( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR6 ± s( c S sF | j | j k r t ‚ n | j | j } | j d | j | _ | S( Ni ( R3 R4 t EOFErrorR2 R1 ( R R7 ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt getË s
( R R R R6 R: ( ( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyR0 « s iè c S s` d d l } d } x0 | | k rD d G| GH| j | ƒ | d } q Wd G| Gd GH| j ƒ d S( Niÿÿÿÿi R6 i s Producer: done producingt items( R R6 R ( R2 t waitt nR t i( ( s. /usr/lib64/python2.7/Demo/metaclasses/Synch.pyt producerÓ s
c S s¼ d d l } d } d } x† | | k r yK | j ƒ } | | k rX t d | | f ‚ n d G| GH| d } d } Wq t k
rœ | j | ƒ | d } q Xq Wd G| Gd GH| j ƒ d S(
Niÿÿÿÿi gü©ñÒMbP?s get() returned %s, expected %st goti i s Consumer: done consumingR; ( R R: R
R9 R R ( R2 R<