diff --git a/src/platform/platform.c b/src/platform/platform.c index 3788352..191c41f 100644 --- a/src/platform/platform.c +++ b/src/platform/platform.c @@ -287,19 +287,33 @@ static void set_current_error(int err) errno = err; } +#ifdef __clang__ +#pragma clang diagnostic push +#ifdef __arm__ +// warning: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0 bytes) +#pragma clang diagnostic ignored "-Watomic-alignment" +#endif // __arm__ +#endif // __clang__ + static void enter_lock(dnne_lock_handle* lock) { - while (__sync_val_compare_and_swap(lock, DNNE_LOCK_OPEN, -1) != DNNE_LOCK_OPEN) + long tmp = DNNE_LOCK_OPEN; + while (!__atomic_compare_exchange_n(lock, &tmp, -1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { (void)sched_yield(); // Yield instead of sleeping. + tmp = DNNE_LOCK_OPEN; } } static void exit_lock(dnne_lock_handle* lock) { - __atomic_exchange_n(lock, DNNE_LOCK_OPEN, __ATOMIC_ACQ_REL); + __atomic_exchange_n(lock, DNNE_LOCK_OPEN, __ATOMIC_SEQ_CST); } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif // __clang__ + #endif // !DNNE_WINDOWS static failure_fn failure_fptr; diff --git a/test/DNNE.UnitTests/Consumption.cs b/test/DNNE.UnitTests/Consumption.cs index 0de9639..b01ba36 100644 --- a/test/DNNE.UnitTests/Consumption.cs +++ b/test/DNNE.UnitTests/Consumption.cs @@ -92,7 +92,7 @@ public void InstanceExports() Assert.Equal(num, ExportingAssembly.InstanceExports.MyClass_getNumber(inst)); - ExportingAssembly.InstanceExports.MyClass_printNumber(inst); + Assert.Equal(num * 2, ExportingAssembly.InstanceExports.MyClass_doubleNumber(inst)); ExportingAssembly.InstanceExports.MyClass_dtor(inst); } diff --git a/test/DNNE.UnitTests/ExportingAssembly.cs b/test/DNNE.UnitTests/ExportingAssembly.cs index a2072fb..e3040ee 100644 --- a/test/DNNE.UnitTests/ExportingAssembly.cs +++ b/test/DNNE.UnitTests/ExportingAssembly.cs @@ -56,7 +56,7 @@ public static class InstanceExports public static extern void MyClass_setNumber(IntPtr inst, int number); [DllImport(nameof(ExportingAssemblyNE))] - public static extern void MyClass_printNumber(IntPtr inst); + public static extern int MyClass_doubleNumber(IntPtr inst); } public static class IntExports diff --git a/test/ExportingAssembly/ExportingAssembly.csproj b/test/ExportingAssembly/ExportingAssembly.csproj index 63fc4c5..986be29 100644 --- a/test/ExportingAssembly/ExportingAssembly.csproj +++ b/test/ExportingAssembly/ExportingAssembly.csproj @@ -7,7 +7,8 @@ - net6.0;net472 + net6.0 + $(TargetFrameworks);net472 true Major true @@ -22,7 +23,7 @@ clang++ - $(DnneCompilerUserFlags)$(MSBuildThisFileDirectory)override.c + $(DnneCompilerUserFlags) $(MSBuildThisFileDirectory)override.c false diff --git a/test/ExportingAssembly/InstanceExports.cs b/test/ExportingAssembly/InstanceExports.cs index 4d7f464..456e637 100644 --- a/test/ExportingAssembly/InstanceExports.cs +++ b/test/ExportingAssembly/InstanceExports.cs @@ -38,7 +38,7 @@ namespace ExportingAssembly /// class MyClass /// { /// intptr_t _inst; - /// + /// /// public: /// MyClass() /// { @@ -56,9 +56,9 @@ namespace ExportingAssembly /// { /// MyClass_setNumber(_inst, num); /// } - /// void printNumber() + /// int32_t doubleNumber() /// { - /// MyClass_printNumber(_inst); + /// return MyClass_doubleNumber(_inst); /// } /// // Delete copy functions since a copy export isn't defined. /// MyClass(const MyClass&) = delete; @@ -94,10 +94,10 @@ public static void MyClassSetNumber(IntPtr inst, int number) As(inst).Number = number; } - [UnmanagedCallersOnly(EntryPoint = "MyClass_printNumber")] - public static void MyClassPrintNumber(IntPtr inst) + [UnmanagedCallersOnly(EntryPoint = "MyClass_doubleNumber")] + public static int MyClassDoubleNumber(IntPtr inst) { - As(inst).PrintNumber(); + return As(inst).DoubleNumber(); } private static T As(IntPtr ptr) where T : class @@ -111,15 +111,12 @@ private static T As(IntPtr ptr) where T : class /// public class MyClass { - private int number; - public MyClass() { - this.number = 0; } public int Number { get; set; } - public void PrintNumber() => Console.WriteLine(this.number); + public int DoubleNumber() => 2 * this.Number; } }