示例#1
0
        /// <summary>
        /// Yields the calling thread.
        /// </summary>
        /// <param name="args">The arguments to return from Resume.</param>
        /// <returns>The objects passed to Resume.</returns>
        /// <exception cref="System.InvalidOperationException">If the thread
        /// is not already running -or- if this is not a Lua thread.</exception>
        public override ILuaMultiValue Yield(ILuaMultiValue args)
        {
            lock (handle_)
            {
                if (!IsLua)
                {
                    throw new InvalidOperationException(
                              "Cannot resume a thread that has been created outside Lua.");
                }
                if (Status != LuaThreadStatus.Running)
                {
                    throw new InvalidOperationException("Thread must be running to yield.");
                }

                // Fire the OnYield event.
                var e = new YieldEventArgs(args);
                CallOnYield(e);

                // If the yield is rejected, simply return the arguments.
                if (e.RejectYield)
                {
                    return(e.ReturnArguments);
                }

                args   = args ?? E_.Runtime.CreateMultiValue();
                args_  = args;
                Status = LuaThreadStatus.Suspended;

                Monitor.Pulse(handle_);
                while (Status != LuaThreadStatus.Running)
                {
                    Monitor.Wait(handle_);
                }

                ILuaMultiValue ret = Interlocked.Exchange(ref args_, null);
                return(ret ?? E_.Runtime.CreateMultiValue());
            }
        }
示例#2
0
        /// <summary>
        /// Yields the calling thread.
        /// </summary>
        /// <param name="args">The arguments to return from Resume.</param>
        /// <returns>The objects passed to Resume.</returns>
        /// <exception cref="System.InvalidOperationException">If the thread
        /// is not already running -or- if this is not a Lua thread.</exception>
        public override ILuaMultiValue Yield(ILuaMultiValue args)
        {
            lock (handle_)
            {
                if (!IsLua)
                {
                    throw new InvalidOperationException(
                        "Cannot resume a thread that has been created outside Lua.");
                }
                if (Status != LuaThreadStatus.Running)
                    throw new InvalidOperationException("Thread must be running to yield.");

                // Fire the OnYield event.
                var e = new YieldEventArgs(args);
                CallOnYield(e);

                // If the yield is rejected, simply return the arguments.
                if (e.RejectYield)
                    return e.ReturnArguments;

                args = args ?? E_.Runtime.CreateMultiValue();
                args_ = args;
                Status = LuaThreadStatus.Suspended;

                Monitor.Pulse(handle_);
                while (Status != LuaThreadStatus.Running)
                {
                    Monitor.Wait(handle_);
                }

                ILuaMultiValue ret = Interlocked.Exchange(ref args_, null);
                return ret ?? E_.Runtime.CreateMultiValue();
            }
        }