1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace Mono.Debugger.Backend
{
internal abstract class DebuggerWaitHandle
{
public readonly string Name;
public DebugFlags DebugFlags = DebugFlags.Mutex;
[DllImport("monodebuggerserver")]
static extern int mono_debugger_server_get_current_pid ();
[DllImport("monodebuggerserver")]
static extern long mono_debugger_server_get_current_thread ();
protected DebuggerWaitHandle (string name)
{
this.Name = name;
}
public static string CurrentThread {
get {
int pid = mono_debugger_server_get_current_pid ();
long thread = mono_debugger_server_get_current_thread ();
return String.Format ("[{0}:{1}:0x{2:x}]",
Environment.MachineName, pid, thread);
}
}
public abstract bool TryLock ();
protected void Debug (string format, params object[] args)
{
Report.Debug (DebugFlags, format, args);
}
}
internal class DebuggerMutex : DebuggerWaitHandle
{
protected new IntPtr handle;
[DllImport("monodebuggerserver")]
static extern IntPtr mono_debugger_mutex_new ();
[DllImport("monodebuggerserver")]
static extern void mono_debugger_mutex_lock (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern void mono_debugger_mutex_unlock (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern bool mono_debugger_mutex_trylock (IntPtr handle);
public DebuggerMutex (string name)
: base (name)
{
handle = mono_debugger_mutex_new ();
}
public void Lock ()
{
Debug ("{0} locking {1}", CurrentThread, Name);
mono_debugger_mutex_lock (handle);
Debug ("{0} locked {1}", CurrentThread, Name);
}
public void Unlock ()
{
Debug ("{0} unlocking {1}", CurrentThread, Name);
mono_debugger_mutex_unlock (handle);
Debug ("{0} unlocked {1}", CurrentThread, Name);
}
public override bool TryLock ()
{
Debug ("{0} trying to lock {1}", CurrentThread, Name);
bool success = mono_debugger_mutex_trylock (handle);
if (success)
Debug ("{0} locked {1}", CurrentThread, Name);
else
Debug ("{0} could not lock {1}", CurrentThread, Name);
return success;
}
}
internal class DebuggerEventQueue : DebuggerMutex
{
IntPtr cond;
[DllImport("monodebuggerserver")]
static extern IntPtr mono_debugger_cond_new ();
[DllImport("monodebuggerserver")]
static extern void mono_debugger_cond_wait (IntPtr mutex, IntPtr cond);
[DllImport("monodebuggerserver")]
static extern void mono_debugger_cond_broadcast (IntPtr cond);
public DebuggerEventQueue (string name)
: base (name)
{
cond = mono_debugger_cond_new ();
}
public void Wait ()
{
Debug ("{0} waiting {1}", CurrentThread, Name);
mono_debugger_cond_wait (handle, cond);
Debug ("{0} done waiting {1}", CurrentThread, Name);
}
public void Signal ()
{
Debug ("{0} signal {1}", CurrentThread, Name);
mono_debugger_cond_broadcast (cond);
}
}
}
|