No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52threads2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:nrf52threads2 [2023/12/03 13:01] – ktokarz | en:edu:zmitac:s2:aih:lab:nrf52threads2 [2023/12/03 13:25] (current) – ktokarz | ||
|---|---|---|---|
| Line 173: | Line 173: | ||
| Function “Give” will increment the semaphore value up to the maximum limit, in which case the semaphore will not be modified. “Give” can be called from any thread or ISR because it does not stop program execution in any case. | Function “Give” will increment the semaphore value up to the maximum limit, in which case the semaphore will not be modified. “Give” can be called from any thread or ISR because it does not stop program execution in any case. | ||
| Function “Take” will decrement the semaphore count down to zero (the semaphore is unavailable). “Take” can be called only from threads but not from ISR, because if a thread is trying to " | Function “Take” will decrement the semaphore count down to zero (the semaphore is unavailable). “Take” can be called only from threads but not from ISR, because if a thread is trying to " | ||
| - | ===== Mutexes ===== | + | To create the semaphore |
| - | Mutexes are the sub-category of the semaphores which presents two values only: locked and unlocked. Despite this, they are implemented also as the counter in which zero value means that the resource is unlocked, and any other value means that the resource is locked. If the mutex is locked three times it also must be unlocked three times to make it unlocked. Another feature that differs between mutexes and semaphores is that the mutex can be unlocked only by the same thread which locks it. | + | |
| - | ===== Functions for semaphores and mutexes ===== | + | |
| - | To create the semaphore | + | |
| < | < | ||
| K_SEM_DEFINE(my_semaphore, | K_SEM_DEFINE(my_semaphore, | ||
| Line 196: | Line 193: | ||
| } | } | ||
| </ | </ | ||
| - | As you can see the k_sem_take() function has a timeout that can prevent the thread from hanging. This argument can also be K_NO_WAIT (returns immediately) and K_FOREVER (blocks the thread). | + | As you can see the k_sem_take() function has a timeout that can prevent the thread from hanging. This argument can also be K_NO_WAIT (returns immediately) and K_FOREVER (blocks the thread).\\ |
| + | ===== Mutexes ===== | ||
| + | Mutexes are similar to the semaphores but present two values only: locked and unlocked. Despite this, they are implemented also as the counter in which zero value means that the resource is unlocked, and any other value means that the resource is locked. If the mutex is locked three times it also must be unlocked three times to make it unlocked. Another feature that differs between mutexes and semaphores is that the mutex can be unlocked only by the same thread which locks it. Mutexes are mainly used to synchronize the critical shared code that can be executed from many threads. | ||
| + | To create the mutex you can use the macro | ||
| + | < | ||
| + | K_MUTEX_DEFINE(my_mutex); | ||
| + | </ | ||
| + | or the k_mutex structure definition and initialization as for the semaphores: | ||
| + | < | ||
| + | struct k_mutex my_mutex; | ||
| + | k_mutex_init(& | ||
| + | </ | ||
| + | Locking the mutex is done with the function: | ||
| + | < | ||
| + | if (k_mutex_lock(& | ||
| + | /* mutex successfully locked */ | ||
| + | } else { | ||
| + | printk(" | ||
| + | } | ||
| + | </ | ||
| + | Here the timeout can be also K_NO_WAIT and K_FOREVER.\\ | ||
| + | Unlocking the mutex can be done with the function: | ||
| + | < | ||
| + | k_mutex_unlock(& | ||
| + | </ | ||
| ===== Tasks for the laboratory ===== | ===== Tasks for the laboratory ===== | ||
| - | - Write the program that blinks the LED as many times as it is set in the semaphore (decrementing semaphore with every blink). The semaphore can be given (incremented) by pressing the button. | + | - Write the program that blinks the LED as many times as it is set in the semaphore (decrementing semaphore with every blink). The semaphore can be given (incremented) by pressing the button. Thus if you press the button three times the LED will blink three times. |
en/edu/zmitac/s2/aih/lab/nrf52threads2.1701604903.txt.gz · Last modified: by ktokarz
