-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Timed lock aquisition #5599
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
Closed
Closed
Timed lock aquisition #5599
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2cbc0ab
Support for timed lock acquire. This enables threading functionality.
c1d9f42
Merge branch 'master' into threads
e264864
Merge branch 'master' into threads
48a80c5
Testing and final touches.
fb726ed
Testing and final touches.
bcb3f61
Clean up
9e5b4d4
Clean up
bb14c0a
Incorporating feedback:
fd623fd
Changed from ms to us in timeout
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# test timed _thread lock object using a single thread | ||
# | ||
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd | ||
|
||
import _thread | ||
|
||
# create lock | ||
lock = _thread.allocate_lock() | ||
|
||
print(type(lock) == _thread.LockType) | ||
|
||
# should be unlocked | ||
print(lock.locked()) | ||
|
||
# try acquire | ||
print(lock.acquire()) | ||
print(lock.locked()) | ||
|
||
# this fail with timeout | ||
print(lock.acquire(1, 0.1)) | ||
print(lock.locked()) | ||
|
||
# this will fail with negative timeout | ||
print(lock.acquire(0, -1)) | ||
print(lock.locked()) | ||
|
||
# these will raise value errors | ||
try: | ||
print(lock.acquire(0, 1)) | ||
except ValueError: | ||
print("can't specify a timeout for a non-blocking call") | ||
|
||
try: | ||
print(lock.acquire(0, 0)) | ||
except ValueError: | ||
print("can't specify a timeout for a non-blocking call") | ||
|
||
print(lock.locked()) | ||
lock.release() | ||
print(lock.locked()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# test timed _thread lock object using multiple threads | ||
# | ||
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd | ||
try: | ||
import utime as time | ||
except ImportError: | ||
import time | ||
import _thread | ||
|
||
# create lock | ||
lock = _thread.allocate_lock() | ||
|
||
def thread_entry(): | ||
lock.acquire() | ||
time.sleep(1) | ||
print('have it') | ||
lock.release() | ||
|
||
# spawn the threads | ||
for i in range(4): | ||
_thread.start_new_thread(thread_entry, ()) | ||
|
||
# wait for threads to start | ||
time.sleep(.1) | ||
|
||
# will timeout and roughly measure time | ||
start = time.time() | ||
lock.acquire(1, 2.5) | ||
stop = time.time() | ||
print((stop - start) > 2) | ||
print((stop - start) < 3) | ||
|
||
# wait for threads to finish | ||
lock.acquire() | ||
print('done') |
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.
This comment was marked as resolved.
Sorry, something went wrong.