sched/semaphore.c
/*
 * sem_wait - decrement the semaphore
 *
 * @sem       - the semaphore address
 *
 * The call of this function can suspend the current execution if the semaphore
 * value is less than or equal to 0. Use this primitive for signalling purpose
 * or for locking implementation.
 *
 */
decrement the semaphore
@sem       - the semaphore address

The call of this function can suspend the current execution if the semaphore
value is less than or equal to 0. Use this primitive for signalling purpose
or for locking implementation.
decrement the semaphore @sem - the semaphore address The call of this function can suspend the current execution if the semaphore value is less than or equal to 0 . Use this primitive for signalling purpose or for locking implementation .
int sem_wait(sem_t *sem)
{
  irq_state_t irq_state = cpu_disableint();
  if (sem->count > 0)
  {
    sem->count--;
  }
  else
  {
    struct tcb_s *tcb     = sched_get_current_task();
    assert(!((tcb->t_state == WAITING_FOR_SEM) ||
           (tcb->t_state == HALTED)));
    if ((tcb == NULL) || (g_current_tcb->next == g_current_tcb->prev))
    {
      cpu_enableint(irq_state);
      return -EAGAIN;
    }
    tcb->t_state          = WAITING_FOR_SEM;
    tcb->waiting_tcb_sema = sem;
    SCHED_DEBUG_INFO("%s WAIT for sema\n", tcb->task_name);
    cpu_enableint(irq_state);
    sched_preempt_task(tcb);
    return 0;
  }
  cpu_enableint(irq_state);
  return 0;
}
int sem_wait ( sem_t * sem ) { irq_state_t irq_state = cpu_disableint ( ) ; if ( sem -> count > 0 ) { sem -> count -- ; } else { struct tcb_s * tcb = sched_get_current_task ( ) ; assert ( ! ( ( tcb -> t_state == WAITING_FOR_SEM ) || ( tcb -> t_state == HALTED ) ) ) ; if ( ( tcb == NULL ) || ( g_current_tcb -> next == g_current_tcb -> prev ) ) { cpu_enableint ( irq_state ) ; return - EAGAIN ; } tcb -> t_state = WAITING_FOR_SEM ; tcb -> waiting_tcb_sema = sem ; SCHED_DEBUG_INFO ( " \n " , tcb -> task_name ) ; cpu_enableint ( irq_state ) ; sched_preempt_task ( tcb ) ; return 0 ; } cpu_enableint ( irq_state ) ; return 0 ; }
sem_wait - decrement the semaphore
 @sem       - the semaphore address
sem_wait - decrement the semaphore @sem - the semaphore address
/* Disable context switch by disabling interrupts */ /* Verify the semaphore value and decrement it if it's > 0 */ /* There was an error or tasks were not initialized.
     * If there is only one task in the ready to run list don't change
     * the state to WAITING_FOR_SEM. Instead, we should return an error
     * like EAGAIN.
     */ /* Switch context to the next running task */ /* Place the current task in the waiting list and remove the task from the
     * running queue. Activate a new task that is prepared to be run.
     */ /* Re-enable interrupts */