Exemple #1
0
 public void RegisterTimeout(uint *timeout, Action wakeUpCallback)
 {
     if (timeout != null)
     {
         RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(*timeout), wakeUpCallback);
     }
 }
Exemple #2
0
        /// <summary>
        /// Wait for an event flag for a given bit pattern with callback.
        /// </summary>
        /// <param name="EventId">The event id returned by sceKernelCreateEventFlag.</param>
        /// <param name="Bits">The bit pattern to poll for.</param>
        /// <param name="Wait">Wait type, one or more of ::PspEventFlagWaitTypes or'ed together</param>
        /// <param name="OutBits">The bit pattern that was matched.</param>
        /// <param name="Timeout">Timeout in microseconds</param>
        /// <param name="HandleCallbacks"></param>
        /// <returns>
        ///		ERROR_KERNEL_NOT_FOUND_EVENT_FLAG - If can't find the eventFlag
        ///		ERROR_KERNEL_WAIT_TIMEOUT         - If there was a timeout
        ///		0                                 - On success
        /// </returns>
        public int _sceKernelWaitEventFlagCB(EventFlagId EventId, uint Bits, EventFlagWaitTypeSet Wait, uint *OutBits, uint *Timeout, bool HandleCallbacks)
        {
            var  EventFlag = HleState.EventFlagManager.EventFlags.Get(EventId);
            bool TimedOut  = false;

            HleState.ThreadManager.Current.SetWaitAndPrepareWakeUp(HleThread.WaitType.Semaphore, String.Format("_sceKernelWaitEventFlagCB(EventId={0}, Bits={1:X}, Wait={2})", EventId, Bits, Wait), WakeUpCallback =>
            {
                if (Timeout != null)
                {
                    HleState.PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(*Timeout), () =>
                    {
                        TimedOut = true;
                        WakeUpCallback();
                    });
                }

                EventFlag.AddWaitingThread(new HleEventFlag.WaitThread()
                {
                    HleThread      = HleState.ThreadManager.Current,
                    BitsToMatch    = Bits,
                    WaitType       = Wait,
                    WakeUpCallback = () => { WakeUpCallback(); },
                    OutBits        = OutBits,
                });
            }, HandleCallbacks: HandleCallbacks);

            if (TimedOut)
            {
                throw(new SceKernelException(SceKernelErrors.ERROR_KERNEL_WAIT_TIMEOUT));
            }

            //throw(new NotImplementedException());
            return(0);
        }
Exemple #3
0
 protected void UpdateHandlerTime()
 {
     PspRtc.Update();
     Console.Error.WriteLine("UpdateHandlerTime: {0}", this.HandlerTime - ElapsedAccumulatedTime);
     this.Timer.DateTime = PspRtc.CurrentDateTime +
                           TimeSpanUtils.FromMicroseconds(this.HandlerTime - ElapsedAccumulatedTime);
 }
        public int sceKernelReceiveMbx(MessageBox MessageBox, PspPointer *PointerToMessage, uint *Timeout)
        {
            var  CurrentThread = ThreadManager.Current;
            bool TimedOut      = false;

            CurrentThread.SetWaitAndPrepareWakeUp(HleThread.WaitType.None, "sceKernelReceiveMbx", MessageBox,
                                                  WakeUpCallback =>
            {
                if (Timeout != null)
                {
                    PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(*Timeout), () =>
                    {
                        TimedOut = true;
                        WakeUpCallback();
                    });
                }
                MessageBox.Receive(PointerToMessage, WakeUpCallback);
            }, HandleCallbacks: false);

            if (TimedOut)
            {
                return((int)SceKernelErrors.ERROR_KERNEL_WAIT_TIMEOUT);
            }
            else
            {
                //if (Timeout)
                //return MessageBox.Receive(Message);
                return(0);
            }
        }
        /// <summary>
        /// Wait for an event flag for a given bit pattern with callback.
        /// </summary>
        /// <param name="EventId">The event ID returned by <see cref="sceKernelCreateEventFlag"/>.</param>
        /// <param name="Bits">The bit pattern to poll for.</param>
        /// <param name="Wait">Wait type, one or more of PspEventFlagWaitTypes or'ed together</param>
        /// <param name="OutBits">The bit pattern that was matched.</param>
        /// <param name="Timeout">Timeout in microseconds</param>
        /// <param name="HandleCallbacks"></param>
        /// <returns>
        ///		ERROR_KERNEL_NOT_FOUND_EVENT_FLAG - If can't find the eventFlag
        ///		ERROR_KERNEL_WAIT_TIMEOUT         - If there was a timeout
        ///		0                                 - On success
        /// </returns>
        public int _sceKernelWaitEventFlagCB(HleEventFlag EventFlag, uint Bits, EventFlagWaitTypeSet Wait,
                                             uint *OutBits, uint *Timeout, bool HandleCallbacks)
        {
            if ((Wait & ~EventFlagWaitTypeSet.MaskValidBits) != 0)
            {
                throw new SceKernelException(SceKernelErrors.ERROR_KERNEL_ILLEGAL_MODE);
            }
            if (Bits == 0)
            {
                throw new SceKernelException(SceKernelErrors.ERROR_KERNEL_EVENT_FLAG_ILLEGAL_WAIT_PATTERN);
            }
            bool TimedOut = false;

            var PreviousPattern = EventFlag.Info.CurrentPattern;

            ThreadManager.Current.SetWaitAndPrepareWakeUp(
                HleThread.WaitType.Semaphore,
                $"_sceKernelWaitEventFlagCB(EventId={EventFlag.GetUidIndex(InjectContext)}, Bits={Bits:X}, Wait={Wait})",
                EventFlag,
                WakeUpCallback =>
            {
                if (Timeout != null)
                {
                    PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(*Timeout), () =>
                    {
                        TimedOut = true;
                        *Timeout = 0;
                        WakeUpCallback();
                    });
                }

                EventFlag.AddWaitingThread(new HleEventFlag.WaitThread()
                {
                    HleThread      = ThreadManager.Current,
                    BitsToMatch    = Bits,
                    WaitType       = Wait,
                    WakeUpCallback = () => { WakeUpCallback(); },
                    OutBits        = OutBits,
                });
            }, HandleCallbacks: HandleCallbacks);

            if (OutBits != null)
            {
                *OutBits = PreviousPattern;
            }

            if (TimedOut)
            {
                throw new SceKernelException(SceKernelErrors.ERROR_KERNEL_WAIT_TIMEOUT);
            }

            //throw(new NotImplementedException());
            return(0);
        }
        private int _sceKernelDelayThreadCB(uint DelayInMicroseconds, bool HandleCallbacks)
        {
            HleState.ThreadManager.Current.SetWaitAndPrepareWakeUp(HleThread.WaitType.Timer, "sceKernelDelayThread", WakeUpCallback =>
            {
                HleState.PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(DelayInMicroseconds), () =>
                {
                    WakeUpCallback();
                });
            }, HandleCallbacks: HandleCallbacks);

            return(0);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ThreadId"></param>
        /// <param name="Timeout"></param>
        /// <param name="HandleCallbacks"></param>
        /// <returns></returns>
        private int _sceKernelWaitThreadEndCB(int ThreadId, uint *Timeout, bool HandleCallbacks)
        {
            var ThreadToWaitEnd = GetThreadById(ThreadId);

            if (ThreadToWaitEnd.HasAnyStatus(HleThread.Status.Stopped))
            {
                return(0);
            }

            if (ThreadToWaitEnd.HasAnyStatus(HleThread.Status.Killed))
            {
                return(0);
            }

            bool TimedOut = false;

            ThreadManager.Current.SetWaitAndPrepareWakeUp(HleThread.WaitType.None,
                                                          $"sceKernelWaitThreadEnd('{ThreadToWaitEnd.Name}')", ThreadToWaitEnd, WakeUpCallback =>
            {
                if (Timeout != null)
                {
                    PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(*Timeout), () =>
                    {
                        TimedOut = true;
                        WakeUpCallback();
                    });
                }

                Console.WriteLine("Wait End!");
                Action OnTerminate = null;

                OnTerminate = () =>
                {
                    ThreadToWaitEnd.OnTerminate -= OnTerminate;
                    Console.WriteLine("Ended!");
                    //throw(new Exception("aaaaaaaaaaaa"));
                    WakeUpCallback();
                };

                ThreadToWaitEnd.OnTerminate += OnTerminate;
            }, HandleCallbacks: HandleCallbacks);

            if (TimedOut)
            {
                return((int)SceKernelErrors.ERROR_KERNEL_WAIT_TIMEOUT);
            }
            else
            {
                return(0);
            }
        }
        private int _sceKernelDelayThreadCB(uint DelayInMicroseconds, bool HandleCallbacks)
        {
            var CurrentThread = ThreadManager.Current;

            CurrentThread.SetWaitAndPrepareWakeUp(HleThread.WaitType.Timer,
                                                  $"sceKernelDelayThread({DelayInMicroseconds}, {HandleCallbacks})", null,
                                                  WakeUpCallback =>
            {
                PspRtc.RegisterTimerInOnce(TimeSpanUtils.FromMicroseconds(DelayInMicroseconds),
                                           () => { WakeUpCallback(); });
            }, HandleCallbacks: HandleCallbacks);

            return(0);
        }