void Thread::start() { if(getuid() == 0) // Check to see if we are root { sched_param sched_p; if(pthread_attr_setschedpolicy(&attr_, SCHED_RR) != 0) throw ThreadError(); // Set RR scheduling (RT, timesliced) if(pthread_attr_setinheritsched(&attr_, PTHREAD_EXPLICIT_SCHED) != 0) throw ThreadError(); // Create thread with explicit (non-inherited) scheduling - setting priority will not work otherwise! sched_p.sched_priority = static_cast<int>(priority_); // Set priority if(pthread_attr_setschedparam(&attr_, &sched_p) != 0) throw ThreadError(); // Use the priority if(pthread_create(&threadId_, &attr_, threadMapper, this) != 0) throw ThreadError(); } else { if(pthread_create(&threadId_, NULL, threadMapper, this) != 0) throw ThreadError(); } sleep(1); }
void WorkerThread::start(::CrashAndSqueeze::Parallel::ITaskExecutor * executor, unsigned max_wait_ms, Logger *logger) { _ASSERT(NULL == handle); this->stopped = false; this->max_wait_ms = max_wait_ms; this->task_executors.push_back(executor); this->executors_count = 1; this->logger = logger; handle = CreateThread(NULL, 0, WorkerThread::routine, this, 0, NULL); if (NULL == handle) throw ThreadError(); }
void Thread::join() { if(WaitForSingleObject(handle_, INFINITE) != 0) throw ThreadError(); }
void Thread::setPriority(ThreadPriority priority) { if(!SetThreadPriority(handle_, priority_)) throw ThreadError(); priority_ = priority; }
SimpleTask::~SimpleTask() { int res = LowLevel::DestroyTask(task); if (res != 0) throw ThreadError(); }
SimpleTask::SimpleTask(int priority, LowLevel::TaskCallback callback) throw(ThreadError) { task = LowLevel::CreateTask(priority, callback); if (task == 0) throw ThreadError(); }
///////////////////////////////////////////////////////////////////////////// // DlgURL message handlers void DlgURL::OnOK() { UpdateData(); if (m_url.IsEmpty()) return; m_ok.EnableWindow(0); m_bSafeToClose = FALSE; SetStatus(_T("Connecting to site...")); CString* m_strBuffer = &m_data; CString m_sServer=_T(""); CString m_sObject; INTERNET_PORT m_nPort = INTERNET_DEFAULT_HTTP_PORT; DWORD m_dwServiceType = INTERNET_SERVICE_HTTP; AfxParseURL(m_url, m_dwServiceType, m_sServer, m_sObject, m_nPort); //Create the Internet session handle m_hInternetSession = ::InternetOpen(AfxGetAppName(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (m_hInternetSession == NULL){ ThreadError(_T("cannot open internet session")); return; } if (m_bAbort) { OnThreadFinished(1); return; } //Make the connection to the HTTP server m_hHttpConnection = ::InternetConnect(m_hInternetSession, m_sServer, m_nPort , NULL, NULL, m_dwServiceType , 0, (DWORD) this); if (m_hHttpConnection == NULL){ ThreadError(_T("cannot connect to remote server")); return; } if (m_bAbort) { OnThreadFinished(1); return; } //Issue the request to read the file LPCTSTR ppszAcceptTypes[2]; ppszAcceptTypes[0] = _T("*/*"); //We support accepting any mime file type since this is a simple download of a file ppszAcceptTypes[1] = NULL; m_hHttpFile = HttpOpenRequest(m_hHttpConnection, NULL, m_sObject, NULL, NULL, ppszAcceptTypes, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION, (DWORD) this); if (m_hHttpFile == NULL){ ThreadError(_T("Failed in call to HttpOpenRequest")); return; } if (m_bAbort) { OnThreadFinished(1); return; } //Issue the request BOOL bSend = ::HttpSendRequest(m_hHttpFile, NULL, 0, NULL, 0); // Get the length of the file. TCHAR szContentLength[32]; DWORD dwInfoSize = 32; DWORD dwFileSize = 0; if (::HttpQueryInfo(m_hHttpFile, HTTP_QUERY_CONTENT_LENGTH, szContentLength, &dwInfoSize, NULL)){ dwFileSize = (DWORD) _ttol(szContentLength); } //Now do the actual read of the file DWORD dwBytesRead = 0; char szReadBuf[1025]; DWORD dwBytesToRead = 1024; DWORD dwTotalBytesRead = 0; do { if (::InternetReadFile(m_hHttpFile, szReadBuf, dwBytesToRead, &dwBytesRead)){ if (dwBytesRead && !m_bAbort) { //Write the data to file szReadBuf[dwBytesRead]=0; LPTSTR ptr = m_strBuffer->GetBufferSetLength(dwTotalBytesRead + dwBytesRead + 1); memcpy(ptr+dwTotalBytesRead, szReadBuf, dwBytesRead); //Increment the total number of bytes read dwTotalBytesRead += dwBytesRead; m_strBuffer->ReleaseBuffer(dwTotalBytesRead+1); CString s; s.Format(_T("%d/%d"),dwTotalBytesRead,dwFileSize); SetStatus(s); } } else { ThreadError(_T("An error occurred while downloading the file")); return; } } while (dwBytesRead && !m_bAbort); m_size = dwTotalBytesRead; //We're finished OnThreadFinished(0); }