Repository: dotnet/runtime
File: src/native/minipal/time.c (C)

Code before:
    __asm__ __volatile__(".word 0x0100000f");
#else
    return;
#endif
}

#define tccSecondsToNanoSeconds 1000000000      // 10^9
#define tccSecondsToMilliSeconds 1000           // 10^3
#define tccMilliSecondsToNanoSeconds 1000000    // 10^6
#define tccSecondsTo100NS 10000000              // 10^7
int64_t minipal_hires_tick_frequency(void)
{
    return tccSecondsToNanoSeconds;
}

int64_t minipal_hires_ticks(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
    return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
    struct timespec ts;
    int result = clock_gettime(CLOCK_MONOTONIC, &ts);
    if (result != 0)
    {
        assert(!"clock_gettime(CLOCK_MONOTONIC) failed");
    }

    return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec);
#endif
}

int64_t minipal_lowres_ticks(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
    return  (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / (int64_t)(tccMilliSecondsToNanoSeconds);
#else
    struct timespec ts;

    // emscripten exposes CLOCK_MONOTONIC_COARSE but doesn't implement it
#if HAVE_CLOCK_MONOTONIC_COARSE && !defined(__EMSCRIPTEN__)
    // CLOCK_MONOTONIC_COARSE has enough precision for GetTickCount but
    // doesn't have the same overhead as CLOCK_MONOTONIC. This allows
    // overall higher throughput. See dotnet/coreclr#2257 for more details.

    const clockid_t clockType = CLOCK_MONOTONIC_COARSE;
#else
    const clockid_t clockType = CLOCK_MONOTONIC;
#endif

    int result = clock_gettime(clockType, &ts);
    if (result != 0)

Review comment (bug):
```suggestion
    assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC) failed");
```

Code after:
    __asm__ __volatile__(".word 0x0100000f");
#else
    return;
#endif
}

#define tccSecondsToNanoSeconds 1000000000      // 10^9
#define tccSecondsToMilliSeconds 1000           // 10^3
#define tccMilliSecondsToNanoSeconds 1000000    // 10^6
#define tccSecondsTo100NS 10000000              // 10^7
int64_t minipal_hires_tick_frequency(void)
{
    return tccSecondsToNanoSeconds;
}

int64_t minipal_hires_ticks(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
    return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
    struct timespec ts;
    int result;
    result = clock_gettime(CLOCK_MONOTONIC, &ts);
    assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC) failed");

    return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec);
#endif
}

int64_t minipal_lowres_ticks(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
    return  (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / (int64_t)(tccMilliSecondsToNanoSeconds);
#else
    struct timespec ts;

    // emscripten exposes CLOCK_MONOTONIC_COARSE but doesn't implement it
#if HAVE_CLOCK_MONOTONIC_COARSE && !defined(__EMSCRIPTEN__)
    // CLOCK_MONOTONIC_COARSE has enough precision for GetTickCount but
    // doesn't have the same overhead as CLOCK_MONOTONIC. This allows
    // overall higher throughput. See dotnet/coreclr#2257 for more details.

    const clockid_t clockType = CLOCK_MONOTONIC_COARSE;
#else
    const clockid_t clockType = CLOCK_MONOTONIC;
#endif

    int result;
    result = clock_gettime(clockType, &ts);
#if HAVE_CLOCK_MONOTONIC_COARSE && !defined(__EMSCRIPTEN__)
    assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC_COARSE) failed");