Refactor clock functions to use WASI types (#2666)

Refactoring the clock functions to use WASI types so we can simplify the
code and remove some unnecessary boilerplate. See
https://github.com/bytecodealliance/wasm-micro-runtime/pull/2637#discussion_r1362202879
for details.
This commit is contained in:
zoraaver
2023-10-25 11:06:04 +01:00
committed by GitHub
parent 75208073c0
commit e7a62d2099
14 changed files with 188 additions and 228 deletions

View File

@ -6,7 +6,10 @@ set (PLATFORM_COMMON_POSIX_DIR ${CMAKE_CURRENT_LIST_DIR})
file (GLOB_RECURSE source_all ${PLATFORM_COMMON_POSIX_DIR}/*.c)
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
list(REMOVE_ITEM source_all ${PLATFORM_COMMON_POSIX_DIR}/posix_file.c)
list(REMOVE_ITEM source_all
${PLATFORM_COMMON_POSIX_DIR}/posix_file.c
${PLATFORM_COMMON_POSIX_DIR}/posix_clock.c
)
else()
include (${CMAKE_CURRENT_LIST_DIR}/../libc-util/platform_common_libc_util.cmake)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})

View File

@ -3,70 +3,84 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "platform_api_vmcore.h"
#include "libc_errno.h"
#include "platform_api_extension.h"
#define NANOSECONDS_PER_SECOND 1000000000ULL
static bool
bh_clockid_to_clockid(bh_clock_id_t in, clockid_t *out)
static __wasi_errno_t
wasi_clockid_to_clockid(__wasi_clockid_t in, clockid_t *out)
{
switch (in) {
case BH_CLOCK_ID_MONOTONIC:
case __WASI_CLOCK_MONOTONIC:
*out = CLOCK_MONOTONIC;
return true;
#if defined(CLOCK_PROCESS_CPUTIME_ID)
case BH_CLOCK_ID_PROCESS_CPUTIME_ID:
*out = CLOCK_PROCESS_CPUTIME_ID;
return true;
#endif
case BH_CLOCK_ID_REALTIME:
return __WASI_ESUCCESS;
case __WASI_CLOCK_REALTIME:
*out = CLOCK_REALTIME;
return true;
return __WASI_ESUCCESS;
case __WASI_CLOCK_PROCESS_CPUTIME_ID:
#if defined(CLOCK_PROCESS_CPUTIME_ID)
*out = CLOCK_PROCESS_CPUTIME_ID;
return __WASI_ESUCCESS;
#else
return __WASI_ENOTSUP;
#endif
case __WASI_CLOCK_THREAD_CPUTIME_ID:
#if defined(CLOCK_THREAD_CPUTIME_ID)
case BH_CLOCK_ID_THREAD_CPUTIME_ID:
*out = CLOCK_THREAD_CPUTIME_ID;
return true;
return __WASI_ESUCCESS;
#else
return __WASI_ENOTSUP;
#endif
default:
errno = EINVAL;
return false;
return __WASI_EINVAL;
}
}
static uint64
static __wasi_timestamp_t
timespec_to_nanoseconds(const struct timespec *ts)
{
if (ts->tv_sec < 0)
return 0;
if ((uint64)ts->tv_sec >= UINT64_MAX / NANOSECONDS_PER_SECOND)
if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / NANOSECONDS_PER_SECOND)
return UINT64_MAX;
return (uint64)ts->tv_sec * NANOSECONDS_PER_SECOND + (uint64)ts->tv_nsec;
return (__wasi_timestamp_t)ts->tv_sec * NANOSECONDS_PER_SECOND
+ (__wasi_timestamp_t)ts->tv_nsec;
}
int
os_clock_res_get(bh_clock_id_t clock_id, uint64 *resolution)
__wasi_errno_t
os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution)
{
clockid_t nclock_id;
if (!bh_clockid_to_clockid(clock_id, &nclock_id))
return BHT_ERROR;
__wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id);
if (error != __WASI_ESUCCESS)
return error;
struct timespec ts;
if (clock_getres(nclock_id, &ts) < 0)
return BHT_ERROR;
return convert_errno(errno);
*resolution = timespec_to_nanoseconds(&ts);
return BHT_OK;
return error;
}
int
os_clock_time_get(bh_clock_id_t clock_id, uint64 precision, uint64 *time)
__wasi_errno_t
os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
__wasi_timestamp_t *time)
{
clockid_t nclock_id;
if (!bh_clockid_to_clockid(clock_id, &nclock_id))
return BHT_ERROR;
__wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id);
if (error != __WASI_ESUCCESS)
return error;
struct timespec ts;
if (clock_gettime(nclock_id, &ts) < 0)
return BHT_ERROR;
return convert_errno(errno);
*time = timespec_to_nanoseconds(&ts);
return 0;
return error;
}