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.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s2:aih:lab:nrf52threads2 [2023/12/03 13:03] – [Tasks for the laboratory] ktokarzen: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 "Take" an unavailable semaphore must wait until some other thread makes it available (ISRs can't be blocked anytime). Thus such a thread waits until another thread calls "Give" for the same semaphore. 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 "Take" an unavailable semaphore must wait until some other thread makes it available (ISRs can't be blocked anytime). Thus such a thread waits until another thread calls "Give" for the same semaphore.
-===== Mutexes ===== +To create the semaphore with the name "my_semaphore", initial value "0", and limit "1", you can use the following macro
-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 woth the name "my_semaphore", initial value "0", and the limit "1", you can use the floowing macro+
 <code> <code>
 K_SEM_DEFINE(my_semaphore, 0, 1); K_SEM_DEFINE(my_semaphore, 0, 1);
Line 196: Line 193:
 } }
 </code> </code>
-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 
 +<code> 
 +K_MUTEX_DEFINE(my_mutex); 
 +</code> 
 +or the k_mutex structure definition and initialization as for the semaphores: 
 +<code> 
 +struct k_mutex my_mutex; 
 +k_mutex_init(&my_mutex); 
 +</code> 
 +Locking the mutex is done with the function: 
 +<code> 
 +if (k_mutex_lock(&my_mutex, K_MSEC(100)) == 0) { 
 +    /* mutex successfully locked */ 
 +} else { 
 +    printk("Can't lock mutex\n"); 
 +} 
 +</code> 
 +Here the timeout can be also K_NO_WAIT and K_FOREVER.\\ 
 +Unlocking the mutex can be done with the function: 
 +<code> 
 +k_mutex_unlock(&my_mutex); 
 +</code>
 ===== 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. Thus if you press the button three times the LED will blink three times.   - 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.1701605034.txt.gz · Last modified: by ktokarz