The generalization to use n two-thread Peterson locks and arrange them in a binary tree works as follows:
To acquire the lock:
- Suppose that there are n threads that want to access to a critical region.
- The first step uses n/2 two-threads Peterson locks. Then two threads are assigned for each two-threads Peterson lock. At the end of this step only n/2 threads acquired the lock. Those n/2 two-threads Peterson locks are the leaves of the binary tree
- Similar to the first step, the second step uses n/4 two-threads Peterson locks and two threads are assigned for each Peterson lock (those threads are the "winners" in the first step). Those n/4 Peterson locks are the new nodes of the tree
- This procedure continues up to it reaches the root, where it's necessary only one Peterson lock. The thread that acquires the last Peterson lock can enter to the critical region.
To release the lock
The thread that acquired the lock must release each Peterson lock in the path that it followed from the corresponding leaf to the root.
I hope this explanation will serve you.