bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
{
   bool_t ret;

   //Wait until the semaphore is available or the timeout interval elapses
   if(timeout == 0)
   {
      //Non-blocking call
      ret = OS_CSemaRequest(semaphore);
   }
   else if(timeout == INFINITE_DELAY)
   {
      //Infinite timeout period
      OS_WaitCSema(semaphore);
      ret = TRUE;
   }
   else
   {
      //Wait until the specified semaphore becomes available
      ret = OS_WaitCSemaTimed(semaphore, OS_MS_TO_SYSTICKS(timeout));
   }

   //The return value specifies whether the semaphore is available
   return ret;
}
/*--------------------------------------------
| Name:        kernel_sem_timedwait
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
int kernel_sem_timedwait(kernel_sem_t* kernel_sem, int flag, const struct timespec * abs_timeout){

   int timeout=0;

   //
   if(!kernel_sem)
      return -1;
   //
   if(flag==TIMER_ABSTIME && abs_timeout) { //warning: on 16bit architecture use ldiv instead '/' for division with long type.
      OS_DI();
      timeout = kernel_clock_timeout(CLOCK_REALTIME,abs_timeout);
   } if(!flag && abs_timeout) {
      timeout = __time_s_to_ms(abs_timeout->tv_sec)+__time_ns_to_ms(abs_timeout->tv_nsec);
   }
   //
#ifdef __KERNEL_UCORE_EMBOS
   if(abs_timeout && timeout) {
      if(!OS_WaitCSemaTimed(&kernel_sem->sem, timeout)) {
         return -1;
      }
   }else if(abs_timeout && !timeout) {
      if(!OS_WaitCSemaTimed(&kernel_sem->sem, 0))   //try to get sem
         return -EBUSY;
   }else{
      OS_WaitCSema(&kernel_sem->sem);
   }

#endif
   return 0;
}
/*--------------------------------------------
| Name:        kernel_sem_wait
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
int kernel_sem_wait(kernel_sem_t* kernel_sem){
   if(!kernel_sem)
      return -1;
#ifdef __KERNEL_UCORE_EMBOS
   OS_WaitCSema(&kernel_sem->sem);
#endif
   return 0;
}
/*
 * Wait on a semaphore for at most timeout millisecs
 * Return -1 if timed out otherwise time spent waiting.
 */
u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
{
   int end_time = 0, start_time = 0;

   /*if (timeout) {
           start_time = OS_GetTime();
           if(!OS_WaitCSemaTimed(sem, timeout))
    return SYS_ARCH_TIMEOUT;
           end_time = OS_GetTime();

   } else {
*/
   OS_WaitCSema(sem);
   //}

   return (end_time - start_time);
}
Exemple #5
0
/*-----------------------------------------------------------------------------------
 * Blocks the thread while waiting for the semaphore to be
 * signaled. If the "timeout" argument is non-zero, the thread should
 * only be blocked for the specified time (measured in
 * milliseconds).
 *
 * If the timeout argument is non-zero, the return value is the number of
 * milliseconds spent waiting for the semaphore to be signaled. If the
 * semaphore wasn't signaled within the specified time, the return value is
 * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
 * (i.e., it was already signaled), the function may return zero.
 *
 * Notice that lwIP implements a function with a similar name,
 * sys_sem_wait(), that uses the sys_arch_sem_wait() function.
 */
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{
    portTickType start_time, end_time, elapsed_time;

    start_time = OS_GetTime32();

    if ( timeout != 0 )
    {
        if (0 == OS_WaitCSemaTimed(*sem, timeout))
        {
            end_time = OS_GetTime32();
            elapsed_time = end_time - start_time;
            if ( elapsed_time == 0 )
            {
                elapsed_time = (portTickType) 1;
            }
            return ( elapsed_time ); /* return time blocked TBD test */
        }
        else
        {
            return SYS_ARCH_TIMEOUT;
        }
    }
    else /* must block without a timeout */
    {
        OS_WaitCSema(*sem);
    }
    end_time = OS_GetTime32();
        elapsed_time = end_time - start_time;
        if ( elapsed_time == 0 )
        {
            elapsed_time = (portTickType) 1;
        }

        return ( elapsed_time ); /* return time blocked */

}
void
sys_sem_wait(sys_sem_t sem)
{
   OS_WaitCSema(sem);

}