void * join() { if ( thread.get() ) { void * p = 0; if ( pthread_join(*thread,&p) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_join() failed in PosixThread::join()"; se.finish(); throw se; } thread.reset(); return p; } else { ::libmaus::exception::LibMausException se; se.getStream() << "PosixThread::join() called but no thread initialised"; se.finish(); throw se; } }
void start() { if ( ! thread.get() ) { thread = UNIQUE_PTR_MOVE(thread_ptr_type(new pthread_t)); #if 0 std::cerr << "Creating thread without affinity." << std::endl; std::cerr << ::libmaus::util::StackTrace::getStackTrace() << std::endl; #endif if ( pthread_create(thread.get(),0,dispatch,this) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_create() failed in PosixThread::start()"; se.finish(); throw se; } } else { ::libmaus::exception::LibMausException se; se.getStream() << "PosixThread::start() called but object is already in use."; se.finish(); throw se; } }
void startStack(uint64_t const rstacksize = 64*1024) { if ( ! thread.get() ) { thread = UNIQUE_PTR_MOVE(thread_ptr_type(new pthread_t)); #if 0 std::cerr << "Creating thread without affinity." << std::endl; std::cerr << ::libmaus::util::StackTrace::getStackTrace() << std::endl; #endif pthread_attr_t attr; if ( pthread_attr_init(&attr) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_init failed:" << strerror(errno); se.finish(); throw se; } uint64_t const stacksize = std::max(rstacksize,static_cast<uint64_t>(PTHREAD_STACK_MIN)); if ( pthread_attr_setstacksize(&attr,stacksize) != 0 ) { pthread_attr_destroy(&attr); ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_setstacksize() failed in PosixThread::startStack(): " << strerror(errno) << std::endl; se.finish(); throw se; } if ( pthread_create(thread.get(),&attr,dispatch,this) ) { pthread_attr_destroy(&attr); ::libmaus::exception::LibMausException se; se.getStream() << "pthread_create() failed in PosixThread::start()"; se.finish(); throw se; } if ( pthread_attr_destroy(&attr) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_destroy failed:" << strerror(errno); se.finish(); throw se; } } else { ::libmaus::exception::LibMausException se; se.getStream() << "PosixThread::start() called but object is already in use."; se.finish(); throw se; } }
void * tryJoin() { if ( thread.get() ) { void * p = 0; if ( pthread_join(*thread,&p) ) { ::libmaus2::exception::LibMausException se; se.getStream() << "pthread_join() failed in PosixThread::join()"; se.finish(); throw se; } thread.reset(); return p; } else { return 0; } }
virtual ~PosixThread() { if ( thread.get() ) join(); }
void start(std::vector<uint64_t> const & procs) { if ( ! thread.get() ) { thread = UNIQUE_PTR_MOVE(thread_ptr_type(new pthread_t)); pthread_attr_t attr; if ( pthread_attr_init(&attr) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_init failed:" << strerror(errno); se.finish(); throw se; } cpu_set_t cpuset; CPU_ZERO(&cpuset); for ( uint64_t i = 0; i < procs.size(); ++i ) CPU_SET(procs[i],&cpuset); if ( pthread_attr_setaffinity_np(&attr,sizeof(cpu_set_t),&cpuset) ) { pthread_attr_destroy(&attr); ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_setaffinity_np failed:" << strerror(errno); se.finish(); throw se; } #if 0 std::cerr << "Creating thread with affinity." << std::endl; std::cerr << ::libmaus::util::StackTrace::getStackTrace() << std::endl; #endif if ( pthread_create(thread.get(),&attr,dispatch,this) ) { pthread_attr_destroy(&attr); ::libmaus::exception::LibMausException se; se.getStream() << "pthread_create() failed in PosixThread::start()"; se.finish(); throw se; } if ( pthread_attr_destroy(&attr) ) { ::libmaus::exception::LibMausException se; se.getStream() << "pthread_attr_destroy failed:" << strerror(errno); se.finish(); throw se; } } else { ::libmaus::exception::LibMausException se; se.getStream() << "PosixThread::start() called but object is already in use."; se.finish(); throw se; } }