Refactoring time-related default port implementations (#4513)

In the non-Windows code paths:
- New approach to compute TZA without the need for GNU-specific
  `struct tm.tm_gmtoff`.
- Always using `usleep` to sleep. (No real need for `nanosleep` as
  port API has sleep granularity of milliseconds.)
- Not checking for "time.h" at build configuration time as that
  header is mandated by the C standard.
- Not checking for "unistd.h" at build configuration time as that
  header is mandated by the POSIX standard (the default port is
  targeting POSIX systems -- and Windows).
- Fixing some macro guards.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2021-01-29 10:45:46 +01:00
committed by GitHub
parent ba06d492a3
commit 87d30b8088
3 changed files with 44 additions and 66 deletions
+5 -15
View File
@@ -21,9 +21,7 @@
#ifdef _WIN32
#include <windows.h>
#elif defined (HAVE_TIME_H)
#include <time.h>
#elif defined (HAVE_UNISTD_H)
#else /* !_WIN32 */
#include <unistd.h>
#endif /* _WIN32 */
@@ -31,22 +29,14 @@
#include "jerryscript-port-default.h"
/**
* Default implementation of jerry_port_sleep. Uses 'nanosleep' or 'usleep' if
* available on the system, does nothing otherwise.
* Default implementation of jerry_port_sleep. Uses 'usleep' if available on the
* system, does nothing otherwise.
*/
void jerry_port_sleep (uint32_t sleep_time) /**< milliseconds to sleep */
{
#ifdef _WIN32
Sleep (sleep_time);
#elif defined (HAVE_TIME_H)
struct timespec sleep_timespec;
sleep_timespec.tv_sec = (time_t) sleep_time / 1000;
sleep_timespec.tv_nsec = ((long int) sleep_time % 1000) * 1000000L;
nanosleep (&sleep_timespec, NULL);
#elif defined (HAVE_UNISTD_H)
#else /* !_WIN32 */
usleep ((useconds_t) sleep_time * 1000);
#else
(void) sleep_time;
#endif /* HAVE_TIME_H */
#endif /* _WIN32 */
} /* jerry_port_sleep */