Skip to content

Commit 4c3b303

Browse files
committed
CoreLib: honor TMPDIR for shared memory and skip robust mutexes on OpenHarmony
Split per review: the NUMA and platform-identity changes moved to a separate PR. This PR now only contains the shared-memory / named-mutex changes: - SharedMemoryManager: use the TMPDIR-based shared files path (read-only /tmp) - NamedMutex: do not use pthread robust mutexes on OpenHarmony - MutexTests: derive the shared memory directory the same way the runtime does
1 parent bd36a2d commit 4c3b303

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/libraries/System.Private.CoreLib/src/System/IO/SharedMemoryManager.Unix.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,15 @@ private static string InitalizeSharedFilesPath()
414414
}
415415
}
416416

417+
#if TARGET_OPENHARMONY
418+
// OpenHarmony app sandboxes mount /tmp read-only, so use the process temp
419+
// directory (which honors TMPDIR on Unix and falls back to /tmp) for the .NET shared
420+
// memory files (named mutexes, etc.).
421+
return Path.GetTempPath();
422+
#else
423+
// Other platforms: shared memory files live under the fixed /tmp path (historical behavior).
417424
return "/tmp/";
425+
#endif
418426
}
419427

420428
internal static SafeFileHandle CreateOrOpenFile(string sharedMemoryFilePath, SharedMemoryId id, bool createIfNotExist, out bool createdFile)

‎src/libraries/System.Private.CoreLib/src/System/Threading/NamedMutex.Unix.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ internal abstract class NamedMutexProcessDataBase(SharedMemoryProcessDataHeader<
8787
// On OpenBSD, cross process mutexes are not supported in the pthread implementation. See https://github.com/dotnet/runtime/pull/125089.
8888
// On Haiku, robust mutexes are WIP. See https://github.com/dotnet/runtime/pull/126701#issuecomment-4334338213.
8989
// On Linux arm and arm64, we do not use PThread mutex-backed named mutexes for compatibility with previous .NET versions.
90+
// On OpenHarmony, the musl sysroot does not provide the robust mutex APIs
91+
// (pthread_mutexattr_setrobust / pthread_mutex_consistent).
9092
private static bool UsePThreadMutexes =>
9193
#if (TARGET_ARM || TARGET_ARM64)
9294
!OperatingSystem.IsLinux() &&
9395
#endif
94-
!OperatingSystem.IsApplePlatform() && !OperatingSystem.IsFreeBSD() && !OperatingSystem.IsOpenBSD() && !OperatingSystem.IsHaiku();
96+
!OperatingSystem.IsApplePlatform() && !OperatingSystem.IsFreeBSD() && !OperatingSystem.IsOpenBSD() && !OperatingSystem.IsHaiku() && !OperatingSystem.IsOpenHarmony();
9597

9698
private readonly SharedMemoryProcessDataHeader<NamedMutexProcessDataBase> _processDataHeader = header;
9799
protected nuint _lockCount;

‎src/libraries/System.Threading/tests/MutexTests.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,14 @@ public void NamedMutex_OtherEvent_NotCompatible()
10841084
Assert.Throws<PlatformNotSupportedException>(() => WaitHandle.WaitAny(new WaitHandle[] { m, mre }, 0));
10851085
}
10861086

1087-
private const string GlobalSharedMemoryDirectory = $"/tmp/.dotnet/shm/global";
1087+
// The runtime places global shared memory files under {SharedFilesPath}/.dotnet/shm/global
1088+
// (see SharedMemoryManager.Unix.cs). SharedFilesPath is /tmp/ on non-Apple platforms except
1089+
// on OpenHarmony, where it honors TMPDIR. Mirror the runtime's derivation so these tests
1090+
// always target the same directory the runtime uses.
1091+
private static string GlobalSharedMemoryDirectory =>
1092+
OperatingSystem.IsOSPlatform("openharmony")
1093+
? Path.Combine(Path.GetTempPath(), ".dotnet", "shm", "global")
1094+
: "/tmp/.dotnet/shm/global";
10881095
private const UnixFileMode AllUsersRwx =
10891096
UnixFileMode.UserRead
10901097
| UnixFileMode.UserWrite

0 commit comments

Comments
 (0)