<s> from test . test_support import verbose <EOL> import random <EOL> import thread <EOL> import time <EOL> mutex = thread . allocate_lock ( ) <EOL> rmutex = thread . allocate_lock ( ) <EOL> running = <NUM_LIT:0> <EOL> done = thread . allocate_lock ( ) <EOL> done . acquire ( ) <EOL> numtasks = <NUM_LIT:10> <EOL> def task ( ident ) : <EOL> global running <EOL> rmutex . acquire ( ) <EOL> delay = random . random ( ) * numtasks <EOL> rmutex . release ( ) <EOL> if verbose : <EOL> print '<STR_LIT>' , ident , '<STR_LIT>' , round ( delay , <NUM_LIT:1> ) , '<STR_LIT>' <EOL> time . sleep ( delay ) <EOL> if verbose : <EOL> print '<STR_LIT>' , ident , '<STR_LIT>' <EOL> mutex . acquire ( ) <EOL> running = running - <NUM_LIT:1> <EOL> if running == <NUM_LIT:0> : <EOL> done . release ( ) <EOL> mutex . release ( ) <EOL> next_ident = <NUM_LIT:0> <EOL> def newtask ( ) : <EOL> global next_ident , running <EOL> mutex . acquire ( ) <EOL> next_ident = next_ident + <NUM_LIT:1> <EOL> if verbose : <EOL> print '<STR_LIT>' , next_ident <EOL> thread . start_new_thread ( task , ( next_ident , ) ) <EOL> running = running + <NUM_LIT:1> <EOL> mutex . release ( ) <EOL> for i in range ( numtasks ) : <EOL> newtask ( ) <EOL> print '<STR_LIT>' <EOL> done . acquire ( ) <EOL> print '<STR_LIT>' <EOL> class barrier : <EOL> def __init__ ( self , n ) : <EOL> self . n = n <EOL> self . waiting = <NUM_LIT:0> <EOL> self . checkin = thread . allocate_lock ( ) <EOL> self . checkout = thread . allocate_lock ( ) <EOL> self . checkout . acquire ( ) <EOL> def enter ( self ) : <EOL> checkin , checkout = self . checkin , self . checkout <EOL> checkin . acquire ( ) <EOL> self . waiting = self . waiting + <NUM_LIT:1> <EOL> if self . waiting == self . n : <EOL> self . waiting = self . n - <NUM_LIT:1> <EOL> checkout . release ( ) <EOL> return <EOL> checkin . release ( ) <EOL> checkout . acquire ( ) <EOL> self . waiting = self . waiting - <NUM_LIT:1> <EOL> if self . waiting == <NUM_LIT:0> : <EOL> checkin . release ( ) <EOL> return <EOL> checkout . release ( ) <EOL> numtrips = <NUM_LIT:3> <EOL> def task2 ( ident ) : <EOL> global running <EOL> for i in range ( numtrips ) : <EOL> if ident == <NUM_LIT:0> : <EOL> delay = <NUM_LIT> <EOL> else : <EOL> rmutex . acquire ( ) <EOL> delay = random . random ( ) * numtasks <EOL> rmutex . release ( ) <EOL> if verbose : <EOL> print '<STR_LIT>' , ident , '<STR_LIT>' , round ( delay , <NUM_LIT:1> ) , '<STR_LIT>' <EOL> time . sleep ( delay ) <EOL> if verbose : <EOL> print '<STR_LIT>' , ident , '<STR_LIT>' , i <EOL> bar . enter ( ) <EOL> if verbose : <EOL> print '<STR_LIT>' , ident , '<STR_LIT>' , i <EOL> mutex . acquire ( ) <EOL> running -= <NUM_LIT:1> <EOL> finished = running == <NUM_LIT:0> <EOL> mutex . release ( ) <EOL> if finished : <EOL> done . release ( ) <EOL> print '<STR_LIT>' <EOL> if done . acquire ( <NUM_LIT:0> ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> bar = barrier ( numtasks ) <EOL> running = numtasks <EOL> for i in range ( numtasks ) : <EOL> thread . start_new_thread ( task2 , ( i , ) ) <EOL> done . acquire ( ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> if thread . stack_size ( ) != <NUM_LIT:0> : <EOL> raise ValueError , "<STR_LIT>" <EOL> thread . stack_size ( <NUM_LIT:0> ) <EOL> if thread . stack_size ( ) != <NUM_LIT:0> : <EOL> raise ValueError , "<STR_LIT>" <EOL> from os import name as os_name <EOL> if os_name in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> tss_supported = <NUM_LIT:1> <EOL> try : <EOL> thread . stack_size ( <NUM_LIT> ) <EOL> except ValueError : <EOL> print '<STR_LIT>' <EOL> except thread . error : <EOL> tss_supported = <NUM_LIT:0> <EOL> print '<STR_LIT>' <EOL> if tss_supported : <EOL> failed = lambda s , e : s != e <EOL> fail_msg = "<STR_LIT>" <EOL> for tss in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) : <EOL> thread . stack_size ( tss ) <EOL> if failed ( thread . stack_size ( ) , tss ) : <EOL> raise ValueError , fail_msg % tss <EOL> print '<STR_LIT>' % tss <EOL> for tss in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> print '<STR_LIT>' % tss <EOL> next_ident = <NUM_LIT:0> <EOL> for i in range ( numtasks ) : <EOL> newtask ( ) <EOL> print '<STR_LIT>' <EOL> done . acquire ( ) <EOL> print '<STR_LIT>' <EOL> thread . stack_size ( <NUM_LIT:0> ) </s>
