Skip to content

Commit

Permalink
Move thread block into CompareExchange
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Pfister committed Sep 9, 2022
1 parent a9034d2 commit eb5a67c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,24 @@ public static DateTimeOffset Now
return ToLocalTime(utcDateTime, true);
}

if (Interlocked.CompareExchange(ref s_androidTZDataLoaded, 0, -1) != -1)
// The cache isn't loaded yet.
if (Interlocked.CompareExchange(ref s_androidTZDataLoaded, 0, -1) == -1)
{
return ToLocalTime(utcDateTime, true);
}

new Thread(() =>
{
try
new Thread(() =>
{
// Delay the background thread to avoid impacting startup, if it still coincides after 1s, startup is already perceived as slow
Thread.Sleep(1000);
try
{
// Delay the background thread to avoid impacting startup, if it still coincides after 1s, startup is already perceived as slow
Thread.Sleep(1000);
_ = TimeZoneInfo.Local; // Load AndroidTZData
}
finally
{
Volatile.Write(ref s_androidTZDataLoaded, 1);
}
}) { IsBackground = true }.Start();
_ = TimeZoneInfo.Local; // Load AndroidTZData
}
finally
{
Volatile.Write(ref s_androidTZDataLoaded, 1);
}
}) { IsBackground = true }.Start();
}

object? localDateTimeOffset = AppContext.GetData("System.TimeZoneInfo.LocalDateTimeOffset");
if (localDateTimeOffset == null) // If no offset property provided through monovm app context, default
Expand Down
13 changes: 12 additions & 1 deletion src/tasks/AndroidAppBuilder/Templates/MonoRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -90,7 +91,7 @@ public static int initialize(String entryPointLibName, String[] args, Context co
unzipAssets(context, filesDir, "assets.zip");

Log.i("DOTNET", "MonoRunner initialize,, entryPointLibName=" + entryPointLibName);
int localDateTimeOffset = OffsetDateTime.now().getOffset().getTotalSeconds();
int localDateTimeOffset = getLocalDateTimeOffset();
return initRuntime(filesDir, cacheDir, testResultsDir, entryPointLibName, args, localDateTimeOffset);
}

Expand Down Expand Up @@ -152,6 +153,16 @@ static void unzipAssets(Context context, String toPath, String zipName) {
}
}

static int getLocalDateTimeOffset() {
if (android.os.Build.VERSION.SDK_INT >= 26) {
return OffsetDateTime.now().getOffset().getTotalSeconds();
}
else {
int offsetInMillis = Calendar.getInstance().getTimeZone().getRawOffset();
return offsetInMillis / 1000;
}
}

static native int initRuntime(String libsDir, String cacheDir, String testResultsDir, String entryPointLibName, String[] args, int local_date_time_offset);

static native int setEnv(String key, String value);
Expand Down

0 comments on commit eb5a67c

Please sign in to comment.