py/_thread: Add support for lock.acquire timeout. #15099
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #3332. Based on previous work in #5599 and #8932.
Differences to previous PR's:
_thread.TIMEOUT_MAX
(see CPython docs), which in turn is used to raise an OverflowError if a too large timeout is given.MICROPY_PY_THREAD_LOCK_TIMEOUT
feature flag from @mcdeoliveira, since some ports do not support timeouts yet.pthread_mutex_timedlock
(see here)._thread.TIMEOUT_MAX
, which is used in the tests to determine whether timeouts are supported or the test should be skipped.mp_thread_mutex_lock
to add full timeout support instead of adding an extra function.int64_t
for the timeout value (see note below).It still has some open issues / questions:
_thread
class is wrong. Looking at other examples likemath.PI
andsys.maxsize
, it looks like currently all these are defined in the globalobj.h
/objint.h
so they can have separate implementations for each supported storage format?int64_t
because on the Unix port the max timeout would otherwise be a mere 35 minutes. However on many other ports the resolution method (see above) already allows for longer timeouts. We could add another per-port define for the type so we could use a smaller type on some ports? I'm not sure if that would be a meaningful improvement for the smaller microcontrollers.mp_int_t
size limit, or on the Unix port the fact that we cannot use the full uint32 range because the current time should be subtracted.Feedback is welcomed!