void MyDelay(int delTime) /* Delays a specified number of miliseconds or until key pressed on the keyboard */ { long lDone; div_t div_result; div_result = div(delTime,10); /* convert to 1/100 th secs */ delTime = div_result.quot; lDone = SystemTime() + delTime; /* 'Now' plus delay */ while ((SystemTime() < lDone) && (_kbhit() == 0)); while (_kbhit() != 0) _getch(); }
bigtime_t Thread::GetQuantumUsed() const { if (fState == kThreadRunning) return SystemTime() - fLastEvent; return 0; }
void AddressSpace::TrimWorkingSet() { int mappedMemory = fPhysicalMap->CountMappedPages() * PAGE_SIZE; bigtime_t now = SystemTime(); // Adjust the working set size based on fault rate. if (now - fLastWorkingSetAdjust > kWorkingSetAdjustInterval) { cpu_flags fl = DisableInterrupts(); int faultsPerSecond = static_cast<int64>(fFaultCount) * 1000000 / (now - fLastWorkingSetAdjust); if (faultsPerSecond > kMaxFaultsPerSecond && mappedMemory >= fWorkingSetSize && fWorkingSetSize < fMaxWorkingSet) { fWorkingSetSize = MIN(fWorkingSetSize + kWorkingSetIncrement, fMaxWorkingSet); } else if (faultsPerSecond < kMinFaultsPerSecond && mappedMemory <= fWorkingSetSize && fWorkingSetSize > fMaxWorkingSet && Page::CountFreePages() < kMinFreePages) { fWorkingSetSize = MAX(fWorkingSetSize - kWorkingSetIncrement, fMaxWorkingSet); } fLastWorkingSetAdjust = now; fFaultCount = 0; RestoreInterrupts(fl); } // Trim some pages if needed while (mappedMemory > fWorkingSetSize) { break; } }
// Update the file name, returns true if the file name changed bool updateFileName(const CommonTime& t=SystemTime()) { bool openedNewFile = false; const std::string newFilename=printTime(t,filespec); if (currentFilename.size() == 0 && newFilename.size() > 0) { currentFilename = newFilename; currentTime = t; BaseStream::open(currentFilename.c_str(), omode); if (debugLevel) std::cout << "Opened " << currentFilename << std::endl; openedNewFile=true; } else if (newFilename == currentFilename) { currentTime = t; openedNewFile=false; } else { if (debugLevel) std::cout << "Closing " << currentFilename << std::endl; BaseStream::close(); currentFilename = newFilename; currentTime = t; BaseStream::open(currentFilename.c_str(), omode); if (debugLevel) std::cout << "Opened " << currentFilename << std::endl; openedNewFile=true; } return openedNewFile; }
bigtime_t Thread::GetSleepTime() const { if (fState == kThreadWaiting) return SystemTime() - fLastEvent; return 0; }
CodeBuffer::CodeBuffer( const int SVPRNID ) { PRNID = SVPRNID; POrYCode = P_CODE; currentTime = SystemTime(); buffer = new unsigned long[NUM_6SEC_WORDS]; }
void CSimplexDlg::endSystem() { SetLedOff(1); CmenuDlg *ppDlg = (CmenuDlg*)AfxGetApp()->m_pMainWnd; SystemTime(); ppDlg->m_Hist.SetSel(ppDlg->m_Hist.GetWindowTextLength(), -1); //获取当前编辑框字符 ppDlg->m_Hist.ReplaceSel(_T("End the Thread.\r\n")); AfxEndThread(0, TRUE); }
Maybe<SystemTime> file_modification_time(StringRef path) { struct stat s; COPY_STRING_REF_TO_CSTR_BUFFER(path_cstr, path); int r = stat(path_cstr.data(), &s); if (r == 0 && !S_ISDIR(s.st_mode)) { auto mtime = s.st_mtimespec; return SystemTime(0) + SystemTime::seconds((int64)mtime.tv_sec) + SystemTime::nanoseconds((int64)mtime.tv_nsec); } return Nothing; }
// This is the constructor for bootstrap thread. There is less state to // setup since it is already running. Thread::Thread(const char name[]) : Resource(OBJ_THREAD, name), fBasePriority(16), fCurrentPriority(16), fFaultHandler(0), fLastEvent(SystemTime()), fCurrentDir(0), fState(kThreadRunning), fKernelStack(0), fUserStack(0) { }
Thread::Thread(const char name[], Team *team, thread_start_t startAddress, void *param, int priority) : Resource(OBJ_THREAD, name), fThreadContext(team->GetAddressSpace()->GetPhysicalMap()), fBasePriority(priority), fCurrentPriority(priority), fFaultHandler(0), fLastEvent(SystemTime()), fState(kThreadCreated), fTeam(team), fKernelStack(0), fUserStack(0) { char stackName[OS_NAME_LENGTH]; snprintf(stackName, OS_NAME_LENGTH, "%.12s stack", name); fKernelStack = AddressSpace::GetKernelAddressSpace()->CreateArea(stackName, kKernelStackSize, AREA_WIRED, SYSTEM_READ | SYSTEM_WRITE, new PageCache, 0, INVALID_PAGE, SEARCH_FROM_TOP); if (fKernelStack == 0) { printf("team = %p\n", fTeam); panic("Can't create kernel stack for thread: out of virtual space\n"); } unsigned int kernelStack = fKernelStack->GetBaseAddress() + kKernelStackSize - 4; unsigned int userStack = 0; if (team->GetAddressSpace() != AddressSpace::GetKernelAddressSpace()) { // Create the user stack fUserStack = fTeam->GetAddressSpace()->CreateArea(stackName, kUserStackSize, AREA_NOT_WIRED, USER_READ | USER_WRITE | SYSTEM_READ | SYSTEM_WRITE, new PageCache, 0, INVALID_PAGE, SEARCH_FROM_TOP); if (fUserStack == 0) { printf("team = %p\n", fTeam); panic("Can't create user stack for thread: out of virtual space\n"); } userStack = fUserStack->GetBaseAddress() + kUserStackSize - 4; } fThreadContext.Setup(startAddress, param, userStack, kernelStack); // Inherit the current directory from the thread that created this. fCurrentDir = GetRunningThread()->fCurrentDir; if (fCurrentDir) fCurrentDir->AcquireRef(); AcquireRef(); // This reference is effectively owned by the actual thread // of execution. It will be released by the Grim Reaper team->ThreadCreated(this); gScheduler.EnqueueReadyThread(this); gScheduler.Reschedule(); }
void Thread::SwitchTo() { cpu_flags cs = DisableInterrupts(); fState = kThreadRunning; if (fRunningThread != this) { bigtime_t now = SystemTime(); fRunningThread->fLastEvent = now; fLastEvent = now; fRunningThread = this; fThreadContext.SwitchTo(); } RestoreInterrupts(cs); }
AddressSpace::AddressSpace() : fPhysicalMap(new PhysicalMap), fChangeCount(0), fWorkingSetSize(kDefaultWorkingSet), fMinWorkingSet(kDefaultMinWorkingSet), fMaxWorkingSet(kDefaultMaxWorkingSet), fFaultCount(0), fLastWorkingSetAdjust(SystemTime()), fNextTrimAddress(0) { // This is a new user address space. Add some dummy areas for // the lower 4k (which is reserved to detect null pointer references), // and for kernel space, so they can't be allocated. fAreas.Add(new Area("(null area)"), 0, kUserBase - 1); fAreas.Add(new Area("(kernel)"), kKernelBase, kKernelTop); }
// Constructor for kernel address space AddressSpace::AddressSpace(PhysicalMap *map) : fPhysicalMap(map), fWorkingSetSize(kDefaultWorkingSet), fMinWorkingSet(kDefaultMinWorkingSet), fMaxWorkingSet(kDefaultMaxWorkingSet), fFaultCount(0), fLastWorkingSetAdjust(SystemTime()), fNextTrimAddress(0) { fAreas.Add(new Area("(user space)"), 0, kUserTop); // dummy area-> fAreas.Add(new Area("Kernel Text", SYSTEM_READ | SYSTEM_EXEC), kKernelBase, kKernelDataBase - 1); fAreas.Add(new Area("Kernel Data", SYSTEM_READ | SYSTEM_WRITE), kKernelDataBase, kKernelDataTop); fAreas.Add(new Area("Kernel Heap", SYSTEM_READ | SYSTEM_WRITE), kHeapBase, kHeapTop); fAreas.Add(new Area("Hyperspace", SYSTEM_READ | SYSTEM_WRITE), kIOAreaBase, kIOAreaTop); Area *kstack = new Area("Init Stack", SYSTEM_READ | SYSTEM_WRITE); fAreas.Add(kstack, kBootStackBase, kBootStackTop); Thread::GetRunningThread()->SetKernelStack(kstack); }
int CSimplexDlg::StartRunSimplex(int a) { state_inactiveR = 0; state_waithailR = 0; state_hailacquisitionR = 0; state_haildirectivesR = 0; state_hailtailR = 0; state_hailresponseR = 0; state_carrieronlyR = 0; state_acquisitionR = 0; state_simplexronR = 0; state_lnmdendR = 0; state_rnmdendR = 0; state_simplexrendR = 0; state_terminatingtailR = 0; int StopFlagS = 0; SetLedOff(1); Sleep(1000); state_inactiveR = 1; //初始状态 m_led1.SetBitmap(m_red); //hThread = CreateThread(NULL, // 0, // (LPTHREAD_START_ROUTINE)ThreadFuncS, // this, // 0, // &ThreadID); //开辟一个线程 CWinThread * m_pThread; m_pThread = AfxBeginThread(ThreadFuncS, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL); if (NULL == m_pThread) { CmenuDlg *ppDlg = (CmenuDlg*)AfxGetApp()->m_pMainWnd; SystemTime(); ppDlg->m_Hist.SetSel(ppDlg->m_Hist.GetWindowTextLength(), -1); //获取当前编辑框字符 ppDlg->m_Hist.ReplaceSel(_T("Error in Begin a Thread.\r\n")); } return 0; //定义的是有返回值的函数,所以需要return 0 }
status_t Dispatcher::WaitInternal(int dispatcherCount, Dispatcher *dispatchers[], WaitFlags flags, bigtime_t timeout, WaitTag tags[]) { status_t result = E_NO_ERROR; ThreadWaitEvent waitEvent(Thread::GetRunningThread(), flags); for (int dispatcherIndex = 0; dispatcherIndex < dispatcherCount; dispatcherIndex++) { tags[dispatcherIndex].fEvent = &waitEvent; dispatchers[dispatcherIndex]->fTags.Enqueue(&tags[dispatcherIndex]); tags[dispatcherIndex].fEventNext = waitEvent.fTags; tags[dispatcherIndex].fDispatcher = dispatchers[dispatcherIndex]; waitEvent.fTags = &tags[dispatcherIndex]; } if (timeout != INFINITE_TIMEOUT) waitEvent.SetTimeout(SystemTime() + timeout, kOneShotTimer); waitEvent.fThread->SetState(kThreadWaiting); gScheduler.Reschedule(); result = waitEvent.fWakeError; return result; }
int main(int argc, char ** argv) { CommServer commServer; int ret = commServer.setup(); assert(ret == 0); commServer.poll(true); CommFakeSocket * cfs = new CommFakeSocket(commServer); assert(ret == 0); ret = commServer.addSocket(cfs); if (ret != 0) { commServer.removeSocket(cfs); } commServer.poll(true); commServer.idle(SystemTime(), false); }
void SleepUntil( const Time& Tm ) { _Sleep ( Tm - SystemTime() ); }