示例#1
0
 public static void StartHelper(Original::Thread t, object o, bool parameterized)
 {
     Helper.SimpleDel <bool> common = delegate()
     {
         if (parameterized)
         {
             t.Start(o);
         }
         else
         {
             t.Start();
         }
         return(false);
     };
     Helper.SimpleWrap <bool>(
         delegate(ClrSyncManager manager)
     {
         ChessTask child             = manager.TaskFork();
         Original.Semaphore childSem = new Original.Semaphore(0, 1);
         manager.RegisterTaskSemaphore(child, childSem, true);
         manager.AddChildHandle(child, t);
         manager.TaskResume(child);
         common();
         return(false);
     },
         common);
 }
示例#2
0
 public SimplePartitioner(ClrSyncManager manager,
                          long start, long end, long split, Func <long, T> get, bool keysNormalized) :
     base(false, false, keysNormalized)
 {
     this.manager     = manager;
     this.min         = start;
     this.max         = end - 1;
     this.split       = split;
     this.get         = get;
     this.forwardpos  = min;
     this.backwardpos = max;
     barrier          = new OThreading.Barrier(2);
     semaphore        = new OThreading.Semaphore(0, 1);
     mainthread       = OThreading.Thread.CurrentThread;
     //global::System.Console.WriteLine("SimplePartitioner constructed by " + OThreading.Thread.CurrentThread.ManagedThreadId);
 }
示例#3
0
        public static global::System.IntPtr CreateThread(
            global::System.IntPtr lpThreadAttributes,
            global::System.IntPtr dwStackSize,
            global::System.IntPtr lpStartAddress,
            global::System.IntPtr lpParameter,
            int dwCreationFlags,
            global::System.IntPtr lpThreadId
            )
        {
            return(Helper.SimpleWrap <global::System.IntPtr>(
                       delegate(ClrSyncManager manager)
            {
                Original.Semaphore semaphore = new Original.Semaphore(0, 1);
                int childThread = manager.TaskFork();

                Win32ThreadStartDelegate wrapper =
                    delegate(global::System.IntPtr argPtr)
                {
                    try
                    {
                        manager.ThreadBegin(semaphore);
                        int returnValue = 0;
                        Exception exception = null;
                        Microsoft.ManagedChess.MChessChess.LeaveChess();
                        try
                        {
                            returnValue = CallThreadFunction(lpStartAddress, lpParameter);
                        }
                        catch (Exception e)         // catch recoverable exception in monitored code
                        {
                            exception = e;
                        }
                        Microsoft.ManagedChess.MChessChess.EnterChess();
                        if (manager.BreakDeadlockMode)
                        {
                            Microsoft.ManagedChess.MChessChess.WakeNextDeadlockedThread(false, true);
                        }
                        else if (exception == null)
                        {
                            manager.ThreadEnd(childThread);
                        }
                        else
                        {
                            manager.Shutdown(exception);
                        }
                        return returnValue;
                    }
                    catch (Exception e)         // catch fatal exception in our code
                    {
                        manager.Shutdown(e);
                        return -1;
                    }
                };

                //make sure wrapper does not get GCed
                manager.PinObject(wrapper);

                global::System.IntPtr wrapperPointer = Marshal.GetFunctionPointerForDelegate(wrapper);

                global::System.IntPtr returnVal =
                    OrigNativeMethods.CreateThread(lpThreadAttributes, dwStackSize, wrapperPointer,
                                                   global::System.IntPtr.Zero, dwCreationFlags, lpThreadId);
                if ((dwCreationFlags & OrigNativeMethods.CREATE_SUSPENDED) == 0)
                {
                    manager.TaskResume(childThread);
                }
                manager.RegisterTaskSemaphore(childThread, semaphore, true);
                global::System.IntPtr childHandleCp;
                bool dupret = OrigNativeMethods.DuplicateHandle(
                    OrigNativeMethods.GetCurrentProcess(), returnVal,
                    OrigNativeMethods.GetCurrentProcess(), out childHandleCp,
                    0, false, OrigNativeMethods.DUPLICATE_SAME_ACCESS);

                global::System.Diagnostics.Debug.Assert(dupret);

                manager.AddIJoinable(childThread, new NativeThreadJoinable(childHandleCp));
                manager.AddNativeHandleForSyncVar(returnVal, childThread);
                return returnVal;
            },
                       delegate()
            {
                // default to direct call
                return OrigNativeMethods.CreateThread(lpThreadAttributes, dwStackSize,
                                                      lpStartAddress, lpParameter, dwCreationFlags, lpThreadId);
            }
                       ));
        }