pelase verify and cehck deeply if this is thread safe and causes on deadlock on cases no edge cases and exlaon to me thourgly deeply 
class ReadWriteLock:
    def __init__(self):
        self.readers = 0        # Number of active readers
        self.writer = False     # Is there an active writer
        self.write_requests = 0 # Number of writers waiting

    def acquire_read(self):
        # Busy-wait until there are no active writers or write requests
        while self.writer or self.write_requests > 0:
            pass  # In practice, use sleep or other wait mechanisms

        self.readers += 1

    def release_read(self):
        self.readers -= 1

    def acquire_write(self):
        self.write_requests += 1

        # Busy-wait until there are no active readers or writers
        while self.readers > 0 or self.writer:
            pass  # In practice, use sleep or other wait mechanisms

        self.write_requests -= 1
        self.writer = True

    def release_write(self):
        self.writer = False

class Node:
    def __init__(self, val):
        self.val = val
        self.uid = None
        self.parent = None
        self.children = []
        self.locked_descendant_count = 0
        self.isLocked = False
        self.lock = ReadWriteLock()  # Each node has its own ReadWriteLock

class M_Arr_Tree:
    def __init__(self, n, m, name_list):
        self.node_list = []
        self.node_map = {}

        # Initialize nodes
        for name in name_list:
            node = Node(name)
            self.node_list.append(node)
            self.node_map[name] = node

        # Build tree structure
        for i in range(n):
            for j in range(m):
                child_idx = m * i + j + 1
                if child_idx < n:
                    self.node_list[i].children.append(self.node_list[child_idx])
                    self.node_list[child_idx].parent = self.node_list[i]

        self.root = self.node_list[0] if self.node_list else None

    def _get_path_to_root(self, node):
        path = []
        current = node
        while current:
            path.append(current)  
            current = current.parent
        return path[::-1]  # Reverse to have root first

    def _release_locks(self, acquired_locks):
        for lock_type, anc in reversed(acquired_locks):
            if lock_type == 'read':
                anc.lock.release_read()
            elif lock_type == 'write':
                anc.lock.release_write()

    def Lock(self, node, uid):
        path = self._get_path_to_root(node)
        acquired_locks = []

        try:
            # Step 1: Acquire read locks on all ancestors (excluding the target node)
            for anc in path[:-1]:
                anc.lock.acquire_read()
                acquired_locks.append(('read', anc))

                if anc.isLocked:
                    # Release all acquired locks before returning
                    self._release_locks(acquired_locks)
                    return 'false'

            # Step 2: Release all read locks before acquiring write locks
            self._release_locks(acquired_locks)
            acquired_locks.clear()

            # Step 3: Re-acquire write locks on all ancestors (excluding the target node) in order
            for anc in path[:-1]:
                anc.lock.acquire_write()
                acquired_locks.append(('write', anc))

            # Step 4: Acquire write lock on the target node
            target = path[-1]
            target.lock.acquire_write()
            acquired_locks.append(('write', target))

            # Step 5: Check if the target node is already locked or has locked descendants
            if target.isLocked or target.locked_descendant_count > 0:
                self._release_locks(acquired_locks)
                return 'false'

            # Step 6: Lock the node
            target.isLocked = True
            target.uid = uid

            # Step 7: Update locked_descendant_count for all ancestors
            for anc in path[:-1]:
                anc.locked_descendant_count += 1

            return 'true'
        finally:
            # Step 8: Release all locks
            self._release_locks(acquired_locks)