Skip to content

Commit eacaa81

Browse files
committed
[Android] Fix the issue that Environment.TickCount returns wrong value due to /proc/uptime is unaccessible on Android 8 and above.
1 parent f2f6a87 commit eacaa81

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

mono/utils/mono-time.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ mono_100ns_datetime (void)
9191

9292
#include <time.h>
9393

94+
/* a made up uptime of 300 seconds */
95+
#define MADEUP_BOOT_TIME (300 * MTICKS_PER_SEC)
96+
9497
static gint64
9598
get_boot_time (void)
9699
{
@@ -121,21 +124,28 @@ get_boot_time (void)
121124
fclose (uptime);
122125
}
123126
#endif
124-
/* a made up uptime of 300 seconds */
125-
return (gint64)300 * MTICKS_PER_SEC;
127+
return (gint64)MADEUP_BOOT_TIME;
126128
}
127129

128130
/* Returns the number of milliseconds from boot time: this should be monotonic */
129131
gint64
130132
mono_msec_boottime (void)
131133
{
134+
#if defined(ANDROID)
135+
struct timespec ts;
136+
if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) {
137+
return (gint64)MADEUP_BOOT_TIME;
138+
}
139+
return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
140+
#else
132141
static gint64 boot_time = 0;
133142
gint64 now;
134143
if (!boot_time)
135144
boot_time = get_boot_time ();
136145
now = mono_100ns_datetime ();
137146
/*printf ("now: %llu (boot: %llu) ticks: %llu\n", (gint64)now, (gint64)boot_time, (gint64)(now - boot_time));*/
138147
return (now - boot_time)/10000;
148+
#endif
139149
}
140150

141151
/* Returns the number of 100ns ticks from unspecified time: this should be monotonic */

0 commit comments

Comments
 (0)