Skip to content

Commit

Permalink
- Implementing lock(object) functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Jun 6, 2018
1 parent 8980bb0 commit efc47bd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Source/Mosa.Compiler.Linker/LinkerSection.cs
Expand Up @@ -3,6 +3,7 @@
using Mosa.Compiler.Common;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Mosa.Compiler.Linker
{
Expand Down Expand Up @@ -65,11 +66,20 @@ internal void SetFirst(LinkerSymbol symbol)
}
}

private static string SortName(string a)
{
int pos = a.IndexOf(' ');
string s = (pos < 0) ? a : a.Substring(pos + 1);
return s;
}

internal void ResolveLayout(uint fileOffset, ulong virtualAddress)
{
VirtualAddress = virtualAddress;
FileOffset = fileOffset;

//var sortedSymbols = Symbols.OrderBy(x => SortName(x.Name)).ToList();

foreach (var symbol in Symbols)
{
if (symbol.IsResolved)
Expand Down
15 changes: 14 additions & 1 deletion Source/Mosa.Runtime.x86/Korlib/System.Threading/Interlocked.cs
@@ -1,17 +1,30 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using System.Runtime.CompilerServices;
using Mosa.Runtime.Plug;

namespace Mosa.Runtime.x86.Korlib.System.Threading
{
public static class Interlocked
{
[Method("System.Threading.Interlocked::CompareExchange")]
internal static int Exchange(ref int location1, int value, int comparand)
internal static int CompareExchange(ref int location1, int value, int comparand)
{
return Native.CmpXChgLoad32(ref location1, value, comparand);
}

//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//[Method("System.Threading.Interlocked::CompareExchange")]
//public static IntPtr CompareExchange(ref IntPtr location1, IntPtr value, IntPtr comparand)
//{
// int location = location1.ToInt32(); // FIXME? might need to load instead
//
// var result = Native.CmpXChgLoad32(location, value.ToInt32(), comparand.ToInt32());
//
// return new IntPtr(result);
//}

[Method("System.Threading.Interlocked::Exchange")]
internal static int Exchange(ref int location1, int value)
{
Expand Down
11 changes: 10 additions & 1 deletion Source/Mosa.Runtime.x86/Korlib/System.Threading/Monitor.cs
Expand Up @@ -2,20 +2,29 @@

using Mosa.Runtime.Plug;
using System;
using System.Runtime.CompilerServices;

namespace Mosa.Runtime.x86.Korlib.System.Threading
{
public static class Monitor
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Method("System.Threading.Monitor::Enter")]
internal static void Enter(Object obj)
{
var sync = Intrinsic.GetObjectAddress(obj) + IntPtr.Size;

while (Native.CmpXChgLoad32(sync.ToInt32(), 1, 0) == 0)
{ }
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Method("System.Threading.Monitor::Exit")]
internal static void Exit(Object obj)
{
}
var sync = Intrinsic.GetObjectAddress(obj) + IntPtr.Size;

Native.XAddLoad32(sync.ToInt32(), 0);
}
}
}
9 changes: 9 additions & 0 deletions Source/Mosa.Runtime.x86/Native.cs
Expand Up @@ -126,12 +126,21 @@ public static unsafe class Native
[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.CmpXChgLoad32, Mosa.Platform.x86")]
public extern static int CmpXChgLoad32(ref int location, int value, int comparand);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.CmpXChgLoad32, Mosa.Platform.x86")]
public extern static int CmpXChgLoad32(int location, int value, int comparand);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.XAddLoad32, Mosa.Platform.x86")]
public extern static int XAddLoad32(ref int location, int value);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.XAddLoad32, Mosa.Platform.x86")]
public extern static int XAddLoad32(int location, int value);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.XChgLoad32, Mosa.Platform.x86")]
public extern static int XChgLoad32(ref int location, int value);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.XChgLoad32, Mosa.Platform.x86")]
public extern static int XChgLoad32(int location, int value);

[DllImportAttribute(@"Mosa.Platform.x86.Intrinsic.Pause, Mosa.Platform.x86")]
public extern static void Pause();

Expand Down

0 comments on commit efc47bd

Please sign in to comment.