Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Workaround for invalid return value from clock_nanosleep #64679

1 change: 1 addition & 0 deletions src/mono/mono/eglib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(eglib_common_sources
gmarkup.c
gutf8.c
gunicode.c
gclock-nanosleep.c
simonrozsival marked this conversation as resolved.
Show resolved Hide resolved
unicode-data.h)

addprefix(eglib_sources ../eglib/ "${eglib_platform_sources};${eglib_common_sources}")
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/eglib/eglib-remap.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
#define g_unichar_xdigit_value monoeg_g_unichar_xdigit_value
#define g_unsetenv monoeg_g_unsetenv
#define g_usleep monoeg_g_usleep
#define g_clock_nanosleep monoeg_clock_nanosleep
#define g_utf16_to_ucs4 monoeg_g_utf16_to_ucs4
#define g_utf16_to_utf8 monoeg_g_utf16_to_utf8
#define g_utf16_to_utf8_custom_alloc monoeg_g_utf16_to_utf8_custom_alloc
Expand Down
31 changes: 31 additions & 0 deletions src/mono/mono/eglib/gclock-nanosleep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* gclock_nanosleep.c: Clock nanosleep on platforms that have clock_nanosleep().
*
* Copyright 2022 Microsoft
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>
#include <glib.h>
#include <errno.h>

gint
g_clock_nanosleep (clockid_t clockid, gint flags, const struct timespec *request, struct timespec *remain)
{
gint ret = 0;

#if defined(HAVE_CLOCK_NANOSLEEP) && !defined(__PASE__)
ret = clock_nanosleep (clockid, flags, request, remain);
#else
g_assert_not_reached ();
#endif

#ifdef HOST_ANDROID
// Workaround for incorrect implementation of clock_nanosleep return value on old Android (<=5.1)
// See https://github.com/xamarin/xamarin-android/issues/6600
if (ret == -1)
ret = errno;
#endif

return ret;
}
2 changes: 1 addition & 1 deletion src/mono/mono/eglib/gdate-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ g_usleep (gulong microseconds)
}

do {
ret = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
ret = g_clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
if (ret != 0 && ret != EINTR)
g_error ("%s: clock_nanosleep () returned %d", __func__, ret);
} while (ret == EINTR);
Expand Down
8 changes: 8 additions & 0 deletions src/mono/mono/eglib/glib.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <inttypes.h>
#include <eglib-config.h>
#include <minipal/utils.h>
#include <time.h>

// - Pointers should only be converted to or from pointer-sized integers.
// - Any size integer can be converted to any other size integer.
Expand Down Expand Up @@ -1506,4 +1507,11 @@ mono_qsort (void* base, size_t num, size_t size, int (*compare)(const void*, con
#define g_try_realloc(obj, size) (g_cast (monoeg_try_realloc ((obj), (size))))
#define g_memdup(mem, size) (g_cast (monoeg_g_memdup ((mem), (size))))

/*
* Clock Nanosleep
*/

gint
g_clock_nanosleep (clockid_t clockid, gint flags, const struct timespec *request, struct timespec *remain);
simonrozsival marked this conversation as resolved.
Show resolved Hide resolved
simonrozsival marked this conversation as resolved.
Show resolved Hide resolved

#endif // __GLIB_H
4 changes: 2 additions & 2 deletions src/mono/mono/mini/mini-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include <mono/component/debugger-agent.h>
#include "mini-runtime.h"
#include "jit-icalls.h"
#include <glib.h>

#ifdef HOST_DARWIN
#include <mach/mach.h>
Expand Down Expand Up @@ -484,7 +485,7 @@ clock_init_for_profiler (MonoProfilerSampleMode mode)
* CLOCK_PROCESS_CPUTIME_ID clock but don't actually support it. For
* those systems, we fall back to CLOCK_MONOTONIC if we get EINVAL.
*/
if (clock_nanosleep (CLOCK_PROCESS_CPUTIME_ID, TIMER_ABSTIME, &ts, NULL) != EINVAL) {
if (g_clock_nanosleep (CLOCK_PROCESS_CPUTIME_ID, TIMER_ABSTIME, &ts, NULL) != EINVAL) {
sampling_clock = CLOCK_PROCESS_CPUTIME_ID;
break;
}
Expand All @@ -509,7 +510,6 @@ clock_sleep_ns_abs (guint64 ns_abs)

do {
ret = clock_nanosleep (sampling_clock, TIMER_ABSTIME, &then, NULL);

if (ret != 0 && ret != EINTR)
g_error ("%s: clock_nanosleep () returned %d", __func__, ret);
} while (ret == EINTR && mono_atomic_load_i32 (&sampling_thread_running));
Expand Down
3 changes: 2 additions & 1 deletion src/mono/mono/utils/mono-threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <mono/utils/mono-threads-debug.h>
#include <mono/utils/os-event.h>
#include <mono/utils/w32api.h>
#include <glib.h>

#include <errno.h>
#include <mono/utils/mono-errno.h>
Expand Down Expand Up @@ -1748,7 +1749,7 @@ mono_thread_info_sleep (guint32 ms, gboolean *alerted)
}

do {
ret = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
ret = g_clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
} while (ret != 0);
#elif HOST_WIN32
Sleep (ms);
Expand Down