Lock Wait Timeouts
EasySimulation · Oracle
Sessions request exclusive locks on resources. Each request is [session, resource, start, hold]: at time start, session asks for an exclusive lock on resource and, once it gets the lock, keeps it for hold time units. requests is sorted by start, and the lock is granted first come, first served in list order (so among requests with the same start, the earlier one in the list goes first).
For each request, in order:
- If the resource is free at
start(every earlier granted holder has released it, at or beforestart), the request acquires it atstart. - Otherwise it waits until the most recent granted holder releases it; that release time is its acquire time.
- Its wait is
acquire_time - start. If the wait is greater thantimeout, the request gives up: it never holds the lock and does not delay anyone else. Its result is-1. - Otherwise its result is its release time,
acquire_time + hold, and the resource is busy until then.
Different resources are independent. Return the results in the same order as requests.
Example 1
- Input:
- requests = [["s1", "orders", 0, 4], ["s2", "orders", 1, 3], ["s3", "orders", 2, 2], ["s4", "orders", 5, 1]], timeout = 4
- Output:
- [4, 7, -1, 8]
- Explanation
s1holdsordersfrom 0 to 4.s2waits 3 and holds from 4 to 7.s3would wait until 7, a wait of 5, more than 4, so it gives up with-1.s4arrives at 5, waits 2 fors2, and finishes at 8.
Example 2
- Input:
- requests = [["a", "r", 0, 5], ["b", "r", 2, 1]], timeout = 3
- Output:
- [5, 6]
- Explanation
bwaits from 2 to 5, exactly the timeout of 3, which is allowed. It holds from 5 to 6.
Example 3
- Input:
- requests = [["a", "r1", 0, 10], ["b", "r2", 1, 2]], timeout = 0
- Output:
- [10, 3]
- Explanation
r2is free, sobgets it at once even with a timeout of 0.
Constraints
- 0 ≤ requests.length ≤ 105
- 0 ≤ start ≤ 109
- 0 ≤ hold, timeout ≤ 109
requestsis sorted bystart.