Saturday, June 29, 2013

Semaphore Vs Mutex: Deletion

① Delete Semaphore or Mutex
 
Similar to creation of mutex, deletion cannot be done from interrupt context. Deletion can be done from task contexts which has some priority value. All the waiting tasks in the queue are just waken up and E_DLT is set as return error code for them. However, deletion of mutex is assumed to be called after unlock of resource.
 
Deletion of semaphore is same as mutex.

② uITRON implementation to delete semaphore

ER del_sem(ID semid)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        If (semaphore resource count == 0) { /* some task may be waiting */
                For each task waiting on the semaphore's queue {
                        Set E_DLT at the error code register of task's context
                        If task is waiting for timeout {
                                Delete task from timer queue
                        }
                        Set task's status as Ready
                        If task is not in suspended state {
                                Change the task from semaphore's queue to ready queue
                                If executing task's priority is lower than the waiting,
                                    set to execute scheduler when quitting this function
                        }
                        else {
                                Delete the task from the semaphore queue
                        }
                }
        }
        Release resources allocated when creating semaphore(Release semaphore's queue)
        If scheduler has to be executed {
                Call scheduler
                Return the error code in the context's error code register
        }
        Exit critical section (Interrupt Restore)
        return E_OK;
}

③ uITRON implementation to delete mutex

ER del_mtx(ID mtxid)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        For each task waiting on the mutex's queue {
                Set E_DLT at the error code register of task's context
                If task is waiting for timeout {
                        Delete task from timer queue
                }
                Set task's status as Ready
                If task is not in suspended state {
                        Change the task from mutex's queue to ready queue
                        If executing task's priority is lower than the waiting,
                           set to execute scheduler when quitting this function
                }
                else {
                        Delete the task from the mutex queue
                }
        }
        If this mutex has been locked by any task {
                Remove the mutex from the task's mutex list
        }
        Release resources allocated when creating mutex(Release mutex's queue)
        If scheduler has to be executed {
                Call scheduler
                Return the error code in the context's error code register
        }
        Exit critical section (Interrupt Restore)
        return E_OK;
}

For other posts, please look under RTOS label.

No comments: