_thread: timeout parameter is not implemented in Lock.acquire()
https://github.com/micropython/micropython/blob/62849b7010abffb7b0a9c9875930efe7cb77519c/py/modthread.c#L69
Background:
I'm looking for a way to implement a cancelable timeout on Linux, e.g something like threading.Timer from the standard library.
Being on Linux, I'm thinking of trying a timerfd with uasyncio instead. But if implementing the timeout parameter here is not too hard, I could give it a try. It looks to be platform dependent though, so it might be beyond my capabilities.
py/_thread: Add support for lock.acquire timeout.
This working on https://github.com/micropython/micropython-lib/pull/503 I ran into the limitation that while the _thread.Lock.acquire() function supports the waitflag argument, it doesn't handle the timeout arg - while you can pass it it's silently ignored and instead blocks forever.
This PR adds support for the timeout arg. It does so in a fairly naive way I feel... but it does work.
- try to get lock (unblocking)
- if success, return True
- if fail, sleep 1ms
- if timeout expired, return False
- else go back to 1.
Ref: https://docs.python.org/3/library/_thread.html#thread.lock.acquire
Similarly the test is not exactly best practice, what with having a static sleep in the middle, but it does test the behaviour.