Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ARM archictecture detection to avoid the native call error when RDTSC used #227

Merged
merged 1 commit into from
Feb 9, 2015
Merged
Changes from all commits
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
35 changes: 35 additions & 0 deletions src/NetMQ/zmq/Utils/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Opcode

private static IntPtr codeBuffer;
private static ulong size;
private static bool isArm;

public static void Open()
{
Expand All @@ -29,6 +30,12 @@ public static void Open()

if ((p == 4) || (p == 128))
{ // Unix
isArm = IsARMArchitecture();
if (isArm)
{
Rdtsc = RdtscOnArm;
return;
}
Assembly assembly =
Assembly.Load("Mono.Posix");

Expand Down Expand Up @@ -67,6 +74,29 @@ public static void Open()
codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
}

private static bool IsARMArchitecture()
{
bool result = false;
//force to load from mono gac
Assembly currentAssembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
Type syscall = currentAssembly.GetType("Mono.Unix.Native.Syscall");
Type utsname = currentAssembly.GetType("Mono.Unix.Native.Utsname");
MethodInfo uname = syscall.GetMethod("uname");
object[] parameters = new object[] { null };

int invokeResult = (int)uname.Invoke(null, parameters);
if (invokeResult == 0)
{
if (parameters != null)
{
object currentValues = parameters[0];
string machineValue = (string)utsname.GetField("machine").GetValue(currentValues);
result = machineValue.ToLower().Contains("arm");
}
}
return result;
}

public static void Close()
{
Rdtsc = null;
Expand All @@ -90,6 +120,11 @@ public static void Close()
}
}

private static ulong RdtscOnArm()
{
return (ulong)Environment.TickCount;
}

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate ulong RdtscDelegate();

Expand Down