Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move thread block into CompareExchange
  • Loading branch information
Steve Pfister committed Sep 9, 2022
commit eb5a67ce65d521389e7665f119f7bd6abda72c3b
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