TNeo  v1.08
Data Structures | Functions
tn_sem.h File Reference

Detailed Description

A semaphore: an object to provide signaling mechanism.

There is a lot of confusion about differences between semaphores and mutexes, so, it's quite recommended to read small article by Michael Barr: Mutexes and Semaphores Demystified.

Very short:

While mutex is seemingly similar to a semaphore with maximum count of 1 (the so-called binary semaphore), their usage is very different: the purpose of mutex is to protect shared resource. A locked mutex is "owned" by the task that locked it, and only the same task may unlock it. This ownership allows to implement algorithms to prevent priority inversion. So, mutex is a locking mechanism.

Semaphore, on the other hand, is signaling mechanism. It's quite legal and encouraged for semaphore to be waited for in the task A, and then signaled from task B or even from ISR. It may be used in situations like "producer and consumer", etc.

In addition to the article mentioned above, you may want to look at the related question on stackoverflow.com.

Definition in file tn_sem.h.

Go to the source code of this file.

Data Structures

struct  TN_Sem
 Semaphore. More...
 

Functions

enum TN_RCode tn_sem_create (struct TN_Sem *sem, int start_count, int max_count)
 Construct the semaphore. More...
 
enum TN_RCode tn_sem_delete (struct TN_Sem *sem)
 Destruct the semaphore. More...
 
enum TN_RCode tn_sem_signal (struct TN_Sem *sem)
 Signal the semaphore. More...
 
enum TN_RCode tn_sem_isignal (struct TN_Sem *sem)
 The same as tn_sem_signal() but for using in the ISR. More...
 
enum TN_RCode tn_sem_wait (struct TN_Sem *sem, TN_TickCnt timeout)
 Wait for the semaphore. More...
 
enum TN_RCode tn_sem_wait_polling (struct TN_Sem *sem)
 The same as tn_sem_wait() with zero timeout. More...
 
enum TN_RCode tn_sem_iwait_polling (struct TN_Sem *sem)
 The same as tn_sem_wait() with zero timeout, but for using in the ISR. More...
 

Function Documentation

◆ tn_sem_create()

enum TN_RCode tn_sem_create ( struct TN_Sem sem,
int  start_count,
int  max_count 
)

Construct the semaphore.

id_sem field should not contain TN_ID_SEMAPHORE, otherwise, TN_RC_WPARAM is returned.

attr_call_task.png
attr_call_int.png
(refer to Legend for details)

Parameters
semPointer to already allocated struct TN_Sem
start_countInitial counter value, typically it is equal to max_count
max_countMaximum counter value.
Returns

◆ tn_sem_delete()

enum TN_RCode tn_sem_delete ( struct TN_Sem sem)

Destruct the semaphore.

All tasks that wait for the semaphore become runnable with TN_RC_DELETED code returned.

attr_call_task.png
attr_call_ct_sw.png
(refer to Legend for details)

Parameters
semsemaphore to destruct
Returns

◆ tn_sem_signal()

enum TN_RCode tn_sem_signal ( struct TN_Sem sem)

Signal the semaphore.

If current semaphore counter (count) is less than max_count, counter is incremented by one, and first task (if any) that waits for the semaphore becomes runnable with TN_RC_OK returned from tn_sem_wait().

if semaphore counter is already has its max value, no action performed and TN_RC_OVERFLOW is returned

attr_call_task.png
attr_call_ct_sw.png
(refer to Legend for details)

Parameters
semsemaphore to signal
Returns

◆ tn_sem_isignal()

enum TN_RCode tn_sem_isignal ( struct TN_Sem sem)

The same as tn_sem_signal() but for using in the ISR.

attr_call_int.png
attr_call_ct_sw.png
(refer to Legend for details)

◆ tn_sem_wait()

enum TN_RCode tn_sem_wait ( struct TN_Sem sem,
TN_TickCnt  timeout 
)

Wait for the semaphore.

If the current semaphore counter (count) is non-zero, it is decremented and TN_RC_OK is returned. Otherwise, behavior depends on timeout value: task might switch to WAIT state until someone signaled the semaphore or until the timeout expired. refer to TN_TickCnt.

attr_call_task.png
attr_call_ct_sw.png
attr_timeout.png
(refer to Legend for details)

Parameters
semsemaphore to wait for
timeoutrefer to TN_TickCnt
Returns

◆ tn_sem_wait_polling()

enum TN_RCode tn_sem_wait_polling ( struct TN_Sem sem)

The same as tn_sem_wait() with zero timeout.

attr_call_task.png
attr_call_ct_sw.png
(refer to Legend for details)

◆ tn_sem_iwait_polling()

enum TN_RCode tn_sem_iwait_polling ( struct TN_Sem sem)

The same as tn_sem_wait() with zero timeout, but for using in the ISR.

attr_call_int.png
attr_call_ct_sw.png
(refer to Legend for details)