micropython-samples/mutex/README.md

51 wiersze
1.9 KiB
Markdown

2015-11-30 07:14:40 +00:00
# A very simple mutex class.
2015-11-29 17:37:41 +00:00
2015-11-30 07:14:40 +00:00
Files mutex.py mutex_test.py
2015-11-29 17:37:41 +00:00
2015-11-30 07:14:40 +00:00
A mutex is an object enabling threads of execution to protect critical sections of code from reentrancy
or to temporarily protect critical data sets from being updated by other threads. The term "mutex"
derives from the notion of mutual exclusion. A mutex usually provides three methods:
2015-11-29 17:37:41 +00:00
2015-11-30 07:14:40 +00:00
lock() (POSIX pthread_mutex_lock): wait until the mutex is free, then lock it
unlock() (POSIX pthread_mutex_unlock): Immediate return. unlock the mutex.
test() (POSIX pthread_mutex_trylock): Immediate return. test if it is locked.
2015-11-29 17:37:41 +00:00
2015-11-30 07:14:40 +00:00
In this implementation lock and unlock is controlled by a context manager.
In the context of MicroPython a mutex provides a mechanism where an interrupt service routine (ISR) can
test whether the main loop was using critical variables at the time the interrupt occurred, and if so
avoid modifying those variables. Typical use is as follows:
```python
import pyb, mutex
mutex = mutex.Mutex()
data_ready = False
def callback(): # Timer or interrupt callback
2017-02-07 07:48:32 +00:00
global data_ready
2015-11-30 07:14:40 +00:00
if mutex.test():
data_ready = True
# Update critical variables
2017-02-07 07:48:32 +00:00
mutex.release()
2015-11-30 07:14:40 +00:00
else:
# defer any update
# Associate callback with device (pin or timer)
while True:
# code
if data_ready:
with mutex:
data_ready = False
# Access critical variables
# more code
```
Note that the ``with`` statement will execute immediately because no other process runs a ``with`` block
on the same mutex instance.
[Linux man page](http://linux.die.net/man/3/pthread_mutex_lock)
2015-11-30 07:16:44 +00:00
References describing mutex and semaphore objects
[geeksforgeeks](http://www.geeksforgeeks.org/mutex-vs-semaphore/)
[stackoverflow](http://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex)
2015-11-30 07:14:40 +00:00
[differencebetween](http://www.differencebetween.net/language/difference-between-mutex-and-semaphore/)