# alg_dead_lock2.py
#
# Deadlock by acquiring waiting on the same lock twice

# We want all the symbols from sim161
from sim161 import *

lock0 = None
lock1 = None

# Acquire locks in one order
def thread_code_one( arg ):
    lock_acquire( lock0 )
    lock_acquire( lock1 )

# Acquire locks in a different order
def thread_code_two( arg ):
    lock_acquire( lock1 )
    lock_acquire( lock0 )

def main( args ):

    # We need global to write to the global variables
    global lock0, lock1
    lock0 = lock_create( "lock0" )
    lock1 = lock_create( "lock1" )

    # Create two threads to deadlock
    thread_create( "thread0", thread_code_one, None )
    thread_create( "thread1", thread_code_two, None )
