void zThread::destroy() { stop(); join(); zMutexLock m2(&zThread::ms_zThread); zMutexLock m1(&m_zThread); #if defined(_WIN32) || defined(_WIN64) if(hThread != ((HANDLE) NULL)) { TerminateThread(hThread, 0); hThread=(HANDLE) NULL; #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) if(thread != pthread_t()) { // pthread_cancel(thread); thread=pthread_t(); #endif #endif // __GNUG__ pid=0; stopFlag=false; suspendFlag=true; detachFlag=false; vector<zThread*>::iterator k=std::find(zThread::ms_list.begin(),zThread::ms_list.end(),this); if(k != zThread::ms_list.end()) { zThread::ms_list.erase(k); } } } string zThread::getName() const { zMutexLock m1(&m_zThread); return name; }
Reader::Reader(int comPortNumber) { _comPortNumber = comPortNumber; _print = false; _fileName = "log.txt"; string comPortNumberString = ""; stringstream stringStream; stringStream << comPortNumber; stringStream>>comPortNumberString; string comPortString = "\\\\.\\COM" + comPortNumberString; char* comPortPointer = new char[comPortString.size() + 1]; strcpy(comPortPointer, comPortString.c_str()); //create serial port connection _serial = new Serial(comPortPointer); if (_serial->IsConnected()) cout << "Connection established with device on Com Port "<<comPortNumber<<endl; //delete previous file content ofstream file; file.open(_fileName); file<<""; file.close(); _thread = pthread_t(); SpinThread(); }
thread::native_handle_type thread::native_handle() { detail::thread_data_ptr const local_thread_info=(get_thread_info)(); if(local_thread_info) { lock_guard<mutex> lk(local_thread_info->data_mutex); return local_thread_info->thread_handle; } else { return pthread_t(); } }
DVBStreamHandler::DVBStreamHandler(const QString &dvb_device) : _dvb_dev(dvb_device), _dvr_dev_path(CardUtil::GetDeviceName(DVB_DEV_DVR, _dvb_dev)), _allow_section_reader(false), _needs_buffering(false), _allow_retune(false), _start_stop_lock(QMutex::Recursive), _running(false), _reader_thread(pthread_t()), _using_section_reader(false), _device_read_buffer(NULL), _sigmon(NULL), _dvbchannel(NULL), _pid_lock(QMutex::Recursive), _open_pid_filters(0), _listener_lock(QMutex::Recursive) { }
void zThread::suspend() { { zMutexLock m1(&m_zThread); #if defined(_WIN32) || defined(_WIN64) if(!suspendFlag && alive && (hThread != ((HANDLE) NULL))) #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) if(!suspendFlag && alive && (thread != pthread_t())) #endif #endif suspendFlag=true; } #if defined(_WIN32) || defined(_WIN64) SuspendThread(hThread); #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) pthread_kill(thread, SIGSTOP); #endif #endif }
void *Routine(void *arg) { Baton *baton(reinterpret_cast<Baton *>(arg)); const mach_header_xx *dyld(Library(baton, "/usr/lib/system/libdyld.dylib")); Dynamic dynamic; cyset(dynamic.dlerror, "_dlerror", dyld); cyset(dynamic.dlsym, "_dlsym", dyld); int (*pthread_detach)(pthread_t); dlset(&dynamic, pthread_detach, "pthread_detach"); pthread_t (*pthread_self)(); dlset(&dynamic, pthread_self, "pthread_self"); pthread_detach(pthread_self()); void *(*dlopen)(const char *, int); dlset(&dynamic, dlopen, "dlopen"); void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL)); if (handle == NULL) { dynamic.dlerror(); return NULL; } void (*CYHandleServer)(pid_t); dlset(&dynamic, CYHandleServer, "CYHandleServer", handle); if (CYHandleServer == NULL) { dynamic.dlerror(); return NULL; } CYHandleServer(baton->pid); return NULL; }
~AutoAttachJavaThread() { mozilla_AndroidBridge_SetMainThread(pthread_t()); attached = false; }
void zThread::resume() { zMutexLock m1(&m_zThread); #if defined(_WIN32) || defined(_WIN64) if(suspendFlag && !alive && (hThread != ((HANDLE) NULL))) { suspendFlag=false; ResumeThread(hThread); #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) if(suspendFlag && !alive && (thread != pthread_t())) { suspendFlag=false; pthread_kill(thread, SIGCONT); #endif #endif } } zThread* zThread::currentThread() { size_t p=zThread::getTid(); zMutexLock m2(&zThread::ms_zThread); for(size_t i=0; i < zThread::ms_list.size(); i++) { if(p == zThread::ms_list.at(i)->Pid()) return zThread::ms_list.at(i); } return NULL; } size_t zThread::getPid() { #if defined(_WIN32) || defined(_WIN64) return ((size_t) GetCurrentProcessId()); #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) return ((size_t) getpid()); #endif #endif }; size_t zThread::getTid() { #if defined(_WIN32) || defined(_WIN64) return ((size_t) GetCurrentThreadId()); #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) return ((size_t) pthread_self()); #endif #endif } void zThread::sleep(time_t timeout) { if(timeout > 0) { #if defined(_WIN32) || defined(_WIN64) Sleep(timeout); #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) struct timespec ts; ts.tv_sec = (timeout/1000); ts.tv_nsec = ((timeout%1000)*1000000); nanosleep(&ts, NULL); #endif #endif } } void zThread::stopAll() { zMutexLock m2(&zThread::ms_zThread); for(size_t i=0; i < zThread::ms_list.size(); i++) { if(zThread::ms_list.at(i) != NULL) zThread::ms_list.at(i)->stop(); } }
zThread::~zThread() { stop(); join(); zMutexLock m2(&zThread::ms_zThread); zMutexLock m1(&m_zThread); #if defined(_WIN32) || defined(_WIN64) if(hThread != ((HANDLE) NULL)) { // TerminateThread(hThread,0); CloseHandle(hThread); hThread=(HANDLE) 0; #else #if defined(__GNUG__) || defined(__linux__) || defined(__CYGWIN__) if(thread != pthread_t()) { // pthread_cancel(thread); thread=pthread_t(); #endif #endif pid=0; stopFlag=false; suspendFlag=false; detachFlag=false; vector<zThread*>::iterator k=std::find(zThread::ms_list.begin(),zThread::ms_list.end(),this); if(k != zThread::ms_list.end()) { zThread::ms_list.erase(k); } } } #if defined(_WIN32) || defined(_WIN64) DWORD zThread::run_thread_item(LPDWORD attr) { zThread *tr = reinterpret_cast<zThread*>(attr); if(tr == NULL) return 0; { zMutexLock m1(&tr->m_zThread); tr->pid=zThread::getTid(); tr->suspendFlag = false; tr->alive=true; } tr->run(); { zMutexLock m1(&tr->m_zThread); tr->pid=0; } if(tr->detachFlag) { { zMutexLock m1(&tr->m_zThread); tr->alive=false; } delete tr; ExitThread(0); return 0; } else { zMutexLock m1(&tr->m_zThread); tr->alive=false; } ExitThread(0); return 0; }