public void UnlockCPU() { int TlsLockCount = m_TlsIndex.Value; TlsLockCount--; System.Diagnostics.Debug.Assert(TlsLockCount >= 0); // ロック解除 if (TlsLockCount == 0) { m_SysSem.Release(); Interlocked.Decrement(ref m_Locked); } m_TlsIndex.Value = TlsLockCount; if (TlsLockCount == 0) { CPUContext LockCPUContext = CPUContext.GetCurrent(); if (LockCPUContext != null) { LockCPUContext.EndDelaySuspend(); } } }
public ushort GetSubUInt16(uint SubAddr) { ushort Result = 0; uint Addr; bool OK = false; CPUContext CPUContext = StartDelaySuspend(); try { foreach (TSystemIFItem Item in m_SystemIFList) { Addr = SubAddr - (uint)Item.Substitute + Item.Addr; if (((int)Addr >= Item.Addr) && ((int)Addr < Item.Addr + Item.Size)) { Result = Item.SystemIF.GetUInt16(SubAddr); OK = true; break; } } } finally { EndDelaySuspend(CPUContext); } if (!OK) { throw new Exception(); } return(Result); }
public void SetSubUInt32(uint SubAddr, uint Value) { uint Addr; bool OK = false; CPUContext CPUContext = StartDelaySuspend(); try { foreach (TSystemIFItem Item in m_SystemIFList) { Addr = SubAddr - (uint)Item.Substitute + Item.Addr; if (((int)Addr >= Item.Addr) && ((int)Addr < Item.Addr + Item.Size)) { Item.SystemIF.SetUInt32(SubAddr, Value); OK = true; break; } } } finally { EndDelaySuspend(CPUContext); } if (!OK) { throw new Exception(); } }
public void Output(int Kind, byte[] Data, int Size) { if (IsFinished()) { return; } CPUContext CPUContext = StartDelaySuspend(); try { TCallbackEvent callback = new TCallbackEvent(); callback.Func = 3; callback.Kind = Kind; callback.Data = new byte[Size]; Buffer.BlockCopy(Data, 0, callback.Data, 0, Size); if (!m_CallbackSem.WaitOne()) { throw new Exception(); } try { m_EventQueue.AddLast(callback); } finally { m_CallbackSem.Release(); } DoSetEvent(); } finally { EndDelaySuspend(CPUContext); } }
internal void SwitchKernelMode(CPUContext CPUContext) { int TlsLockCount; TlsLockCount = m_TlsIndex.Value; System.Diagnostics.Debug.Assert(TlsLockCount > 0); while ((m_Locked == 0) && !m_TaskMode) { Thread.Yield(); } // ロック解除 m_SysSem.Release(); Interlocked.Add(ref m_Locked, -TlsLockCount); m_TlsIndex.Value = 0; m_IntEvent.Set(); CPUContext.Suspend(); // 他のスレッドが動かないようロック Interlocked.Add(ref m_Locked, TlsLockCount); if (!m_SysSem.WaitOne()) { Terminate(); } m_TlsIndex.Value = TlsLockCount; }
CPUContext StartDelaySuspend() { CPUContext CPUContext = CPUContext.GetCurrent(); if (CPUContext != null) { CPUContext.StartDelaySuspend(); } return(CPUContext); }
void EndDelaySuspend(CPUContext CPUContext) { if (CPUContext == null) { return; } if (!CPUContext.EndDelaySuspend()) { CPUContext.Terminate(); } }
public void LockCPU() { int TlsLockCount = m_TlsIndex.Value; // 他のスレッドが動かないようロック if (TlsLockCount == 0) { Interlocked.Increment(ref m_Locked); for (;;) { if (!m_SysSem.WaitOne()) { Terminate(); break; } // 実行を意図したスレッドかチェック CPUContext Context = CPUContext.GetCurrent(); if ((Context == null) || (Context == m_Current) || (m_Current == null)) { break; } if (!InKernelMode()) { m_IntEvent.Set(); } // 実行したくないスレッドはもう一度待つ m_SysSem.Release(); Thread.Yield(); } } TlsLockCount++; m_TlsIndex.Value = TlsLockCount; if (TlsLockCount == 1) { CPUContext LockCPUContext = CPUContext.GetCurrent(); if (LockCPUContext != null) { LockCPUContext.StartDelaySuspend(); } } }
public void SetTaskName(ID tskid, string szName) { Kernel g_Kernel = this; CPUContext CPUContext = g_Kernel.StartDelaySuspend(); try { string Name = g_Kernel.m_UnitName + "." + szName; Task task = g_Kernel.Nucleus.GetTask(tskid); CPUContext tc = (CPUContext)(task.GetCPUContext()); tc.SetThreadName(Name); } finally { g_Kernel.EndDelaySuspend(CPUContext); } }
public int ReadFile(string Ext, int Pos, byte[] Data, int Size) { int result = 0; CPUContext CPUContext = StartDelaySuspend(); try { string FileName; FileName = GetFullPathName(Ext); using (System.IO.FileStream File = new System.IO.FileStream(FileName, System.IO.FileMode.Open)) { File.Seek(Pos, System.IO.SeekOrigin.Begin); result = File.Read(Data, 0, Size); } } finally { EndDelaySuspend(CPUContext); } return(result); }
bool IKernel.Dispatch() { bool Result; Task Task; ICPUContext CPUContext; Task = Nucleus.GetTask(ID.TSK_SELF); if ((Task != null) && (Task.rtsk.tskstat != TSKSTAT.TTS_DMT)) { CPUContext = Task.GetCPUContext(); Task.Ready(); Result = CPUContext.Dispatch(); } else { m_IntEvent.Set(); Result = !IsTerminated(); } return(Result); }
public void ClearAllTask() { Task Task; CPUContext CPUContext; MemoryPool MemPool; MemoryPoolFixedsize MemPoolFx; Mailbox Mailbox; Semaphore Semaphore; CyclicHandler CyclicHandler; UdpCep UdpCep; Udp6Cep Udp6Cep; TcpCep TcpCep; while (m_CyclicHandlerTable.Count != 0) { CyclicHandler = m_CyclicHandlerTable[0]; m_CyclicHandlerTable.RemoveAt(0); //if(CyclicHandler != null) // delete CyclicHandler; } do { LockTaskTable(); try { for (int i = 0; i < m_TaskTable.Count; i++) { Task = m_TaskTable[i]; CPUContext = (CPUContext)(Task.GetCPUContext()); if ((CPUContext == null) || CPUContext.IsFinished()) { m_TaskTable.RemoveAt(i); i--; continue; } try { CPUContext.Terminate(); } catch (System.Threading.ThreadAbortException) { } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } } } finally { UnlockTaskTable(); } System.Threading.Thread.Yield(); } while (m_TaskTable.Count != 0); while (m_MemPoolTable.Count != 0) { MemPool = m_MemPoolTable[0]; MemPool.EnumMemoryBlock(this, EnumBlockCallBack); m_MemPoolTable.RemoveAt(0); //delete MemPool; } while (m_MemPoolFxTable.Count != 0) { MemPoolFx = m_MemPoolFxTable[0]; m_MemPoolFxTable.RemoveAt(0); //delete MemPoolFx; } while (m_MailboxTable.Count != 0) { Mailbox = m_MailboxTable[0]; m_MailboxTable.RemoveAt(0); //delete Mailbox; } while (m_SemaphoreTable.Count != 0) { Semaphore = m_SemaphoreTable[0]; m_SemaphoreTable.RemoveAt(0); //delete Semaphore; } while (m_UdpCepTable.Count != 0) { UdpCep = m_UdpCepTable[0]; m_UdpCepTable.RemoveAt(0); //delete UdpCep; } while (m_Udp6CepTable.Count != 0) { Udp6Cep = m_Udp6CepTable[0]; m_Udp6CepTable.RemoveAt(0); //delete Udp6Cep; } while (m_TcpCepTable.Count != 0) { TcpCep = m_TcpCepTable[0]; m_TcpCepTable.RemoveAt(0); //delete TcpCep; } }
internal void SwitchKernelMode(CPUContext CPUContext) { int TlsLockCount; TlsLockCount = m_TlsIndex.Value; System.Diagnostics.Debug.Assert(TlsLockCount > 0); while ((m_Locked == 0) && !m_TaskMode) Thread.Yield(); // ロック解除 m_SysSem.Release(); Interlocked.Add(ref m_Locked, -TlsLockCount); m_TlsIndex.Value = 0; m_IntEvent.Set(); CPUContext.Suspend(); // 他のスレッドが動かないようロック Interlocked.Add(ref m_Locked, TlsLockCount); if (!m_SysSem.WaitOne()) { Terminate(); } m_TlsIndex.Value = TlsLockCount; }
void EndDelaySuspend(CPUContext CPUContext) { if (CPUContext == null) return; if (!CPUContext.EndDelaySuspend()) { CPUContext.Terminate(); } }
ICPUContext IKernel.GetCurrent() { return /*m_Current*/ (CPUContext.GetCurrent()); }
void MainLoop() { int intNo; ID tskid; Task task; bool noIntr; Start(); m_Nucleus.Start(); do { ProcInterrupt(); do { noIntr = true; for (intNo = 0; intNo < m_InProcIntr.Length; intNo++) { if (IsTerminated()) { return; } if (InterruptEnabled(intNo)) { noIntr = false; ClearInterrupt(intNo); if (intNo == Nucleus.SysTmrIntNo) { m_Nucleus.OnSysTime(); } tskid = CallIntHandler(intNo); if (tskid != ID.TSK_NULL) { Task wTask; wTask = Nucleus.GetTask(tskid); if (wTask != null) { wTask.Wakeup(); } } } } }while (!noIntr); m_Nucleus.Scheduling(); task = m_Nucleus.GetScheduledTask(); if (task == null) { Idle(); } else { m_Current = (CPUContext)task.GetCPUContext(); task.Run(); m_Current.PopContext(); UnlockCPU(); m_TaskMode = true; if (!m_IntEvent.WaitOne()) { Terminate(); } m_TaskMode = false; LockCPU(); CPUContext CPUContext = (CPUContext)m_Current; m_Current = null; if (CPUContext.IsReleased()) { CPUContext.Wait(); CPUContext.ClearTask(); } else { CPUContext.PushContext(); } } task = m_Nucleus.GetCurrentTask(); if (task != null) { task.Ready(); } } while (!IsTerminated()); }
bool IKernel.ExitAndDispatch() { ExitCPUContext(CPUContext.GetCurrent()); return(true); }