TNeoKernel  v1.01
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_Timeout 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

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
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
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; otherwise, TN_RC_OVERFLOW is returned.

If wait queue is not empty, the first task from the queue becomes runnable with TN_RC_OK returned from tn_sem_wait().

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

Parameters
semsemaphore to signal
Returns
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)

enum TN_RCode tn_sem_wait ( struct TN_Sem sem,
TN_Timeout  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: refer to TN_Timeout.

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_Timeout
Returns
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)

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)