Thursday, June 13, 2013

Semaphore Vs Mutex: Creation

See some implementation differences between semaphore annd mutex:

① Arguments to create a semaphore and mutex:
----------------------------------------------------------
Semaphore needs a) count = Total number of resoures to be maintained by this semaphore. For binary semaphore, this will be one. b) initial count = Number of resources acquired at initialization c) Order = Order in which the tasks should be granted with resources. FIFO or Priority Based
 
Mutex needs a) protocol = Protocol to be used to avoid priority inversion. It can be "Priority inheritence" or "Priority Ceiling". b) ceiling priority = Value of ceiling priority. It is used only when using Priority Ceiling protocol. c) Order = Order in which the tasks should be granted with resources. FIFO or Priority Based

② ITRON implementation to create semaphore
-------------------------------------------
ER cre_sem(ID semid, T_CSEM *pk_csem)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        Initialize internal structure with the arguments.
        Internal structure has one queue to order waiting tasks
        If (tasks are queued in FIFO order)
                Initialize queue as follows:
                ┏━━━━━┯━━━━━━┓
                ┃Queue   │Queue     ┃
                ┃[0]     │Tail       ┃
                ┗━━━━━┷━━━━━━┛
        else (tasks are queued as priority based)
                Initialize as follows:
                ┏━━━━━━━━┯━━┯━━━━┯━━━━━━┓
                ┃Priority0      │1   │Max   │Queue    ┃
                ┃            │    │Pri    │Tail       ┃
                ┗━━━━━━━━┷━━┷━━━━┷━━━━━━┛
        Exit critical section (Interrupt Restore)
        return E_OK;
}

③ ITRON implementation to create mutex
---------------------------------------
ER cre_mtx(ID mtxid, T_CMTX *pk_cmtx)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        Initialize internal structure with the arguments.
        Decide on priority of tasks that lock the mutex
        If (Ceiling) {
                the priority will be the one specified in argument
        } else if (Priority inheritence) {
                temporarily keep 0.
                Later it will be changed to the highest priority of waiting tasks.
        } else (nothing is specified) {
                It will be Highest priority that can be assigned in the system
        }
        Internal structure has one queue to order waiting tasks
        If (tasks are queued in FIFO order)
                Initialize queue as follows:
                ┏━━━━━┯━━━━━━┓
                ┃Queue   │Queue    ┃
                ┃[0]     │Tail       ┃
                ┗━━━━━┷━━━━━━┛
        else (tasks are queued as priority based)
                Initialize as follows:
                ┏━━━━━━━━┯━━┯━━━━┯━━━━━━┓
                ┃Priority0      │1   │Max   │Queue    ┃
                ┃            │    │Pri    │Tail       ┃
                ┗━━━━━━━━┷━━┷━━━━┷━━━━━━┛
        Exit critical section (Interrupt Restore)
        return E_OK;
}
■ For more details on uITRON APIs and structures
--------------------------------------------------------------
Please refer http://www.ertl.jp/ITRON/SPEC/FILE/mitron-400e.pdf

Check Other posts under RTOS label. 

2 comments:

Anonymous said...

clarify TA_FIFO, TA_PRI will affect the priority inversion protocol

Student Fredrick said...

If TA_FIFO or TA_PRI flag is specified, priority inversion protocol would not selected/executed.

TA_FIFO -> Just first waiting task would be granted with mutex
TA_PRI -> Highest priority waiting task would be granted with mutex (But, there would not be any change in task priorities when the some high priority tasks go waiting for the Mutex which does not comply with priority inversion protocol)