V Qc@sdZddlZdddYZdZddlmZmZmZdefdYZd efd YZ d efd YZ e d diZ dZ e dkree ndS(s`Synchronization metaclass. This metaclass makes it possible to declare synchronized methods. iNtLockcBs,eZdZdZddZdZRS(sReentrant 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. cCs4tj|_tj|_d|_d|_dS(s0Constructor. Initialize all instance variables.iN(tthreadt allocate_lockt _Lock__mutext _Lock__waittNonet _Lock__tidt _Lock__count(tself((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyt__init__*s icCs|jjz-|jtjkr9|jd|_dSWd|jjX|jj|}| ro| rodSz-|jjtj|_d|_dSWd|jjXdS(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. iNi(RtacquireRRt get_identRtreleaseR(Rtflagtlocked((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR 1s   cCs^|jjz<|jd|_|jdkrHd|_|jjnWd|jjXdS(sRelease 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. iiN(RR RRRRR (R((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR Qs  (t__name__t __module__t__doc__R R R (((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR s  cCsg}|d}||d}t}|j|||j|jtj||ftj|||ftj||ftj||f|jddl}x.t|dkrt|GH|jdqWt|GHdS(NcSs3|jdtjG|j|jddS(Nsf2 running in thread %d i(R RR R tappend(tlocktdone((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pytf2ks  cSsE|jdtjGz||Wd|jX|jddS(Nsf1 running in thread %d i(R RR R R(RRR((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pytf1qs   ii gMbP?(RR R Rtstart_new_threadttimetlentsleep(RRRRR((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyt _testLockgs$         (t MetaClasst MetaHelpertMetaMethodWrappertLockingMethodWrappercBseZdZRS(cOs|jd dkrC|jddkrCt|j|jf||S|jjjz!t|j|jf||SWd|jjjXdS(Nit_(Rtapplytfunctinstt__lock__R R (Rtargstkw((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyt__call__s &!(RRR'(((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyRst LockingHelpercBseZeZdZRS(cCs tj||t|_dS(N(Rt__helperinit__RR$(Rt formalclass((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR)s(RRRt__methodwrapper__R)(((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR(stLockingMetaClasscBseZeZRS((RRR(t __helper__(((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR,stLockingcCsdtfdY}dd}dd}tj}|jtj}|j|d}d}tj||||ftj||||f|jdGH|jdGHd Gt|jGHdS( NtBuffercBs#eZdZdZdZRS(cSs0||_dg|j|_d|_|_dS(Ni(tsizeRtbuffertfirsttlast(Rt initialsize((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR s cSsI|jd|j|jkrH||j|j<|jd|j|_dSdG|jGHd|j|j|jfGH|j|jkr|j|j|j!}n|j|j|j|j }dG|GH|dg|jd|_d|_|jd|_|jd|_dG|jGHdG|jGHd|j|j|jfGH|j|dS( Nisbuffer =s first = %d, last = %d, size = %dstemp =iisBuffer size doubled tos new buffer =(R3R0R2R1Rtput(Rtitemttemp((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR5s(     cSsF|j|jkrtn|j|j}|jd|j|_|S(Ni(R2R3tEOFErrorR1R0(RR6((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pytgets  (RRR R5R9(((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyR/s  icSs`ddl}d}x0||krDdG|GH|j||d}qWdG|GdGH|jdS(NiiR5isProducer: done producingtitems(RR5R (R1twaittnRti((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pytproducers    cSsddl}d}d}x||kryK|j}||krXtd||fndG|GH|d}d}Wqtk r|j||d}qXqWdG|Gd GH|jdS( NiigMbP?sget() returned %s, expected %stgotiisConsumer: done consumingR:(RR9tAssertionErrorR8RR (R1R;R<RR=ttouttx((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pytconsumers"         is Producer donesAll donesbuffer size ==(R.RRR RRR1(R/R>RCtpwaittcwaitR1R<((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyt_tests (        t__main__(((RRRRtMetaRRRRR(R,R.RFR(((s./usr/lib64/python2.7/Demo/metaclasses/Synch.pyts Z +  U