/* Notice that lwIP implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function.  */
u32_t ESP_IRAM_ATTR sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
/* Notice that lwIP implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function.  */
u32_t ESP_IRAM_ATTR sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{
  portTickType StartTime, EndTime, Elapsed;
  unsigned long ulReturn;
  StartTime = xTaskGetTickCount();
  if (timeout != 0) {
    if (xSemaphoreTake(*sem, timeout / portTICK_PERIOD_MS) == pdTRUE) {
      EndTime = xTaskGetTickCount();
      Elapsed = (EndTime - StartTime) * portTICK_PERIOD_MS;
      if (Elapsed == 0) {
        Elapsed = 1;
      }
      ulReturn = Elapsed;
    } else {
      ulReturn = SYS_ARCH_TIMEOUT;
    }
  } else {
    while (xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE);
    EndTime = xTaskGetTickCount();
    Elapsed = (EndTime - StartTime) * portTICK_PERIOD_MS;
    if (Elapsed == 0) {
      Elapsed = 1;
    }
    ulReturn = Elapsed;
  }
  return ulReturn ;
}