/****************************************************************************
* Public Function Prototypes
****************************************************************************/
-/**
- * @brief This function attempts to lock the semaphore referenced by 'sem'
- * which will be posted from interrupt handler.
- * This function should have nothing to do with the priority inheritance.
- * This function should not be called from interrupt handler.
- * @since Tizen RT v1.0
- */
-int sem_wait_for_isr(FAR sem_t *sem);
-
-/**
- * @brief Interrupt handler releases the semaphore referenced by 'sem'.
- * This function should have nothing to do with the priority inheritance.
- * This function should be called from interrupt handler.
- * @since Tizen RT v1.0
- */
-int sem_post_from_isr(FAR sem_t *sem);
/****************************************************************************
* Name: sem_reset
/****************************************************************************
*
- * Copyright 2016 Samsung Electronics All Rights Reserved.
+ * Copyright 2016-2017 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
return ret;
}
-
-/****************************************************************************
- * Name: sem_post_from_isr
- *
- * Description:
- * When an interrupt handler has finished with a semaphore, it will call
- * sem_post_from_isr(). This function unlocks the semaphore referenced by sem
- * by performing the semaphore unlock operation on that semaphore.
- * This function should have nothing to do with the priority inheritance.
- *
- * If the semaphore value resulting from this operation is positive, then
- * no tasks were blocked waiting for the semaphore to become unlocked; the
- * semaphore is simply incremented.
- *
- * If the value of the semaphore resulting from this operation is zero,
- * then one of the tasks blocked waiting for the semaphore shall be
- * allowed to return successfully from its call to sem_wait_for_isr().
- *
- * Parameters:
- * sem - Semaphore descriptor
- *
- * Return Value:
- * 0 (OK) or -1 (ERROR) if unsuccessful
- *
- * Assumptions:
- * This function may be called from an interrupt handler.
- *
- ****************************************************************************/
-
-int sem_post_from_isr(FAR sem_t *sem)
-{
- FAR struct tcb_s *stcb = NULL;
- irqstate_t saved_state;
- int ret = ERROR;
-
- /* Make sure we were supplied with a valid semaphore. */
-
- if (sem) {
- /* The following operations must be performed with interrupts
- * disabled because sem_post() may be called from an interrupt
- * handler.
- */
-
- saved_state = irqsave();
-
- /* Perform the semaphore unlock operation. */
-
- ASSERT(sem->semcount < SEM_VALUE_MAX);
- sem->semcount++;
-
- /* If the result of of semaphore unlock is non-positive, then
- * there must be some task waiting for the semaphore.
- */
-
- if (sem->semcount <= 0) {
- /* Check if there are any tasks in the waiting for semaphore
- * task list that are waiting for this semaphore. This is a
- * prioritized list so the first one we encounter is the one
- * that we want.
- */
-
- for (stcb = (FAR struct tcb_s *)g_waitingforsemaphore.head; (stcb && stcb->waitsem != sem); stcb = stcb->flink) ;
-
- if (stcb) {
- /* It is, let the task take the semaphore */
-
- stcb->waitsem = NULL;
-
- /* Restart the waiting task. */
-
- up_unblock_task(stcb);
- }
- }
-
- ret = OK;
-
- /* Interrupts may now be enabled. */
-
- irqrestore(saved_state);
- } else {
- set_errno(EINVAL);
- }
-
- return ret;
-}
/****************************************************************************
*
- * Copyright 2016 Samsung Electronics All Rights Reserved.
+ * Copyright 2016-2017 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
irqrestore(saved_state);
return ret;
}
-
-/****************************************************************************
- * Name: sem_wait_for_isr
- *
- * Description:
- * This function attempts to lock the semaphore referenced by 'sem'
- * which will be posted from interrupt handler.
- * This function should have nothing to do with the priority inheritance.
- * If the semaphore value is (<=) zero, then the calling task will not return
- * until it successfully acquires the lock.
- *
- * Parameters:
- * sem - Semaphore descriptor.
- *
- * Return Value:
- * 0 (OK), or -1 (ERROR) is unsuccessful
- * If this function returns -1 (ERROR), then the cause of the failure will
- * be reported in 'errno' as:
- * - EINVAL: Invalid attempt to get the semaphore
- * - EINTR: The wait was interrupted by the receipt of a signal.
- *
- * Assumptions:
- *
- ****************************************************************************/
-
-int sem_wait_for_isr(FAR sem_t *sem)
-{
- FAR struct tcb_s *rtcb = (FAR struct tcb_s *)g_readytorun.head;
- irqstate_t saved_state;
- int ret = ERROR;
-
- /* This API should not be called from interrupt handlers */
-
- DEBUGASSERT(up_interrupt_context() == false);
-
- /* Assume any errors reported are due to invalid arguments. */
- set_errno(EINVAL);
-
-
- /* The following operations must be performed with interrupts
- * disabled because sem_post() may be called from an interrupt
- * handler.
- */
- saved_state = irqsave();
-
- /* sem_wait() is a cancellation point */
- if (enter_cancellation_point()) {
- /* If there is a pending cancellation, then do not perform
- * the wait. Exit now with ECANCELED.
- */
- set_errno(ECANCELED);
- leave_cancellation_point();
- irqrestore(saved_state);
- return ERROR;
- }
-
- if (sem) {
-
- /* Check if the lock is available */
-
- if (sem->semcount > 0) {
- /* It is, let the task take the semaphore. */
-
- sem->semcount--;
- rtcb->waitsem = NULL;
-
- ret = OK;
- }
-
- /* The semaphore is NOT available, We will have to block the
- * current thread of execution.
- */
-
- else {
- /* First, verify that the task is not already waiting on a
- * semaphore
- */
-
- ASSERT(rtcb->waitsem == NULL);
-
- /* Handle the POSIX semaphore (but don't set the owner yet) */
-
- sem->semcount--;
-
- /* Save the waited on semaphore in the TCB */
-
- rtcb->waitsem = sem;
-
-
- /* Add the TCB to the prioritized semaphore wait queue */
-
- set_errno(0);
-
- up_block_task(rtcb, TSTATE_WAIT_SEM);
-
- /* When we resume at this point, either (1) the semaphore has been
- * assigned to this thread of execution, or (2) the semaphore wait
- * has been interrupted by a signal or a timeout. We can detect these
- * latter cases be examining the errno value.
- *
- * In the event that the semaphore wait was interrupted by a signal or
- * a timeout, certain semaphore clean-up operations have already been
- * performed (see sem_waitirq.c). Specifically:
- *
- * - sem_canceled() was called to restore the priority of all threads
- * that hold a reference to the semaphore,
- * - The semaphore count was decremented, and
- * - tcb->waitsem was nullifed.
- *
- * It is necesaary to do these things in sem_waitirq.c because a long
- * time may elapse between the time that the signal was issued and
- * this thread is awakened and this leaves a door open to several
- * race conditions.
- */
-
- if (get_errno() != EINTR && get_errno() != ETIMEDOUT) {
- ret = OK;
- }
- }
-
- /* Interrupts may now be enabled. */
- }
-
- leave_cancellation_point();
- irqrestore(saved_state);
-
- return ret;
-}