Skip to content

py/_thread: Add support for lock.acquire timeout. #8932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

This working on micropython/micropython-lib#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.

  1. try to get lock (unblocking)
  2. if success, return True
  3. if fail, sleep 1ms
  4. if timeout expired, return False
  5. 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.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 20, 2022
@dpgeorge
Copy link
Member

Please see #3332 for discussion, and previous attempt in #5599.

IMO we should try and do this efficiently, because that's what the OS mutex's do well (having an efficient timeout).

@andrewleech
Copy link
Contributor Author

Thanks for the links, I tried to search for existing issues / PR's for this but didn't see these.

@andrewleech andrewleech force-pushed the thread_lock_acquire_timeout branch from e21d48b to d09a7c7 Compare January 6, 2023 00:12
@andrewleech andrewleech force-pushed the thread_lock_acquire_timeout branch from d09a7c7 to fcf907e Compare January 6, 2023 00:13
@github-actions
Copy link

github-actions bot commented Jan 6, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 

tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Feb 16, 2024
…essif

espressif: Set socket type on accepted socket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants