|
|
Semaphores. More...
Data Structures | |
struct | os_sema_s |
Semaphore structure. More... | |
Defines | |
#define | os_sema_init(sem, init_value) DOC_HIDDEN |
Initialization of the semaphore. | |
#define | os_sema_post(sem) os_sema_post_common( sem, 0 ) |
Macro releases the semaphore. | |
#define | os_sema_post_intr(sem) os_sema_post_common( sem , 1 ) |
Macro releases the semaphore. | |
#define | os_sema_wait(sem) os_sema_wait_timeouted( sem, 0 ) |
Waits on the semaphore. | |
#define | OS_WAIT_INFINITE 0 |
symbol for infinite wait on semaphore | |
Typedefs | |
typedef struct os_sema_s | os_sema_t |
Semaphore. | |
Functions | |
os_result_t | os_sema_post_common (os_sema_t *sem, unsigned char in_interrupts) |
Releases the semaphore. | |
os_result_t | os_sema_wait_timeouted (os_sema_t *sem, unsigned short timeout) |
Waits on the semaphore with timeout. | |
os_result_t | os_sema_tryget (os_sema_t *sem) |
Try to obtain semaphore. | |
os_result_t | os_sleep (unsigned short timeout) |
Makes running thread sleeping for specified time. |
Semaphores.
A semaphore is one of synchronization object, it provides well-known mechanism for signalling between threads. The semaphore contains an internal counter initialized with a number at creation. This value affects its further behaviour. Two operations on the semaphore are defined: post and wait.
Wait operation has two options:
1. if counter>0 then it is decremented and thread execution is continued,
2. if counter<=0 then the thread is waiting until [1].
Post operation increments the counter and if it is positive wakes up thread that is waiting on the semaphore.
The DioneOS provides the semaphores consistent with above theoretical model. The post operation has its special version (os_sema_post_intr()) designed for calls from ISR. The blocking wait operation has its variant (os_sema_tryget()) that checks if the wait would happen and if not it performs operation on the semaphore. Otherwise it return OS_WOULD_WAIT code and does not change the semaphore.
Additionaly, os_sema_wait() is equipped with timeout option, so waiting can be limited in time.
This module contain also a function (os_sleep()) for holding thread execution in specified time.
#define os_sema_init | ( | sem, | |
init_value | |||
) | DOC_HIDDEN |
Initialization of the semaphore.
This should be done before semaphore is used.
[in] | sem | pointer to semaphore structure |
[in] | init_value | initial value of semaphore counter (0-means that semaphore is blocked, n>0 that n threads can go through os_sema_wait(). |
#define os_sema_post | ( | sem | ) | os_sema_post_common( sem, 0 ) |
Macro releases the semaphore.
[in] | sem | the pointer to the semaphore structure |
#define os_sema_post_intr | ( | sem | ) | os_sema_post_common( sem , 1 ) |
Macro releases the semaphore.
[in] | sem | the pointer to the semaphore structure |
#define os_sema_wait | ( | sem | ) | os_sema_wait_timeouted( sem, 0 ) |
Waits on the semaphore.
Calling this may cause context switch.
[in] | sem | the pointer to the semaphore structure |
os_result_t os_sema_post_common | ( | os_sema_t * | sem, |
unsigned char | in_interrupts | ||
) |
Releases the semaphore.
[in] | sem | the pointer to the semaphore structure |
[in] | in_interrupts | flag signals if call is from ISR |
os_result_t os_sema_tryget | ( | os_sema_t * | sem | ) |
Try to obtain semaphore.
The function tries to obtain semaphore but never blocks (waits) on it. The result of operation is returned by return code.
[in] | sem | pointer to the semaphore structure |
os_result_t os_sema_wait_timeouted | ( | os_sema_t * | sem, |
unsigned short | timeout | ||
) |
Waits on the semaphore with timeout.
Calling this may cause context switch.
[in] | sem | the pointer to the semaphore structure |
[in] | timeout | waiting timeout in os_tick units |
os_result_t os_sleep | ( | unsigned short | timeout | ) |
Makes running thread sleeping for specified time.
The function may be used when some waiting time is required in the thread. The thread is going sleep for specified time and after that is woken up.
[in] | timeout | the time for which the thread will be sleeping. |