Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb5a67c

Browse files
author
Steve Pfister
committed
Move thread block into CompareExchange
1 parent a9034d2 commit eb5a67c

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.Android.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@ public static DateTimeOffset Now
3232
return ToLocalTime(utcDateTime, true);
3333
}
3434

35-
if (Interlocked.CompareExchange(ref s_androidTZDataLoaded, 0, -1) != -1)
35+
// The cache isn't loaded yet.
36+
if (Interlocked.CompareExchange(ref s_androidTZDataLoaded, 0, -1) == -1)
3637
{
37-
return ToLocalTime(utcDateTime, true);
38-
}
39-
40-
new Thread(() =>
41-
{
42-
try
38+
new Thread(() =>
4339
{
44-
// Delay the background thread to avoid impacting startup, if it still coincides after 1s, startup is already perceived as slow
45-
Thread.Sleep(1000);
40+
try
41+
{
42+
// Delay the background thread to avoid impacting startup, if it still coincides after 1s, startup is already perceived as slow
43+
Thread.Sleep(1000);
4644

47-
_ = TimeZoneInfo.Local; // Load AndroidTZData
48-
}
49-
finally
50-
{
51-
Volatile.Write(ref s_androidTZDataLoaded, 1);
52-
}
53-
}) { IsBackground = true }.Start();
45+
_ = TimeZoneInfo.Local; // Load AndroidTZData
46+
}
47+
finally
48+
{
49+
Volatile.Write(ref s_androidTZDataLoaded, 1);
50+
}
51+
}) { IsBackground = true }.Start();
52+
}
5453

5554
object? localDateTimeOffset = AppContext.GetData("System.TimeZoneInfo.LocalDateTimeOffset");
5655
if (localDateTimeOffset == null) // If no offset property provided through monovm app context, default

src/tasks/AndroidAppBuilder/Templates/MonoRunner.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.OutputStream;
2525
import java.io.BufferedInputStream;
2626
import java.util.ArrayList;
27+
import java.util.Calendar;
2728
import java.util.zip.ZipEntry;
2829
import java.util.zip.ZipInputStream;
2930
import java.time.OffsetDateTime;
@@ -90,7 +91,7 @@ public static int initialize(String entryPointLibName, String[] args, Context co
9091
unzipAssets(context, filesDir, "assets.zip");
9192

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

@@ -152,6 +153,16 @@ static void unzipAssets(Context context, String toPath, String zipName) {
152153
}
153154
}
154155

156+
static int getLocalDateTimeOffset() {
157+
if (android.os.Build.VERSION.SDK_INT >= 26) {
158+
return OffsetDateTime.now().getOffset().getTotalSeconds();
159+
}
160+
else {
161+
int offsetInMillis = Calendar.getInstance().getTimeZone().getRawOffset();
162+
return offsetInMillis / 1000;
163+
}
164+
}
165+
155166
static native int initRuntime(String libsDir, String cacheDir, String testResultsDir, String entryPointLibName, String[] args, int local_date_time_offset);
156167

157168
static native int setEnv(String key, String value);

0 commit comments

Comments
 (0)