2016-04-19 13:03:26 -04:00
|
|
|
# test _thread lock object using a single thread
|
|
|
|
#
|
2020-06-03 18:40:05 -04:00
|
|
|
# SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
2016-04-19 13:03:26 -04:00
|
|
|
|
|
|
|
import _thread
|
|
|
|
|
|
|
|
# create lock
|
|
|
|
lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
print(type(lock) == _thread.LockType)
|
|
|
|
|
|
|
|
# should be unlocked
|
|
|
|
print(lock.locked())
|
|
|
|
|
|
|
|
# basic acquire and release
|
|
|
|
print(lock.acquire())
|
|
|
|
print(lock.locked())
|
|
|
|
lock.release()
|
|
|
|
print(lock.locked())
|
|
|
|
|
|
|
|
# try acquire twice (second should fail)
|
|
|
|
print(lock.acquire())
|
|
|
|
print(lock.locked())
|
|
|
|
print(lock.acquire(0))
|
|
|
|
print(lock.locked())
|
|
|
|
lock.release()
|
|
|
|
print(lock.locked())
|
|
|
|
|
|
|
|
# test with capabilities of lock
|
|
|
|
with lock:
|
|
|
|
print(lock.locked())
|
|
|
|
|
2023-03-18 11:17:02 -04:00
|
|
|
# test that lock is unlocked if an error is raised
|
2016-04-19 13:03:26 -04:00
|
|
|
try:
|
|
|
|
with lock:
|
|
|
|
print(lock.locked())
|
|
|
|
raise KeyError
|
|
|
|
except KeyError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("KeyError")
|
2016-04-19 13:03:26 -04:00
|
|
|
print(lock.locked())
|
2017-06-14 00:43:50 -04:00
|
|
|
|
|
|
|
# test that we can't release an unlocked lock
|
|
|
|
try:
|
|
|
|
lock.release()
|
|
|
|
except RuntimeError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("RuntimeError")
|