int DbDumperWriter::write_record(bool flush) { int ret = OB_SUCCESS; RecordInfo * rec = NULL; struct timespec timeout; UNUSED(flush); timeout.tv_sec = time(NULL) + kSemWaitTimeout; timeout.tv_nsec = 0; if (!records_.empty()) { CThreadGuard gard(&records_lock_); //no problem doing this, because only one thread is waiting on the queue rec = records_.front(); } else if (running_) { sem_timedwait(&sem_empty_, &timeout); } if (rec != NULL) { CThreadGuard gard(&records_lock_); if (file_ == NULL || (ret = file_->Append(rec->buf, rec->length)) != OB_SUCCESS) { TBSYS_LOG(ERROR, "Write record failed, ret:%d, path:%s, len:%d", ret, path_.c_str(), rec->length); } else { writen_lines_++; records_.pop_front(); free_record(rec); } } return ret; }
int ThreadPool::kill() { MutexLockGuard gard(this->ListLock_); while(!threads_.empty()) { cout << ".l........." << endl; pthread_kill(threads_.front(), SIGRTMIN); threads_.pop_front(); } return 0; }
int ThreadPool::start() { state_ = 1; for(int i = 0; i < count; ++i){ pthread_t tid = 0; pthread_create(&tid, 0, ThreadPool::thread, this); MutexLockGuard gard(ListLock_); this->threads_.push_back(tid); } return 0; }
int DbDumperWriter::push_record(const char *data, int lenth) { int ret = OB_SUCCESS; RecordInfo *rec = alloc_record(data, lenth); if (rec == NULL) { TBSYS_LOG(ERROR, "Can't allocate records, len:%d", lenth); ret = OB_ERROR; } else { CThreadGuard gard(&records_lock_); pushed_lines_++; records_.push_back(rec); sem_post(&sem_empty_); } return ret; }
void* ThreadPool::thread(void* p) { signal(SIGRTMIN, sighandle); ThreadPool* pp = static_cast<ThreadPool*>(p); task* t = NULL; while(0 != pp->state_.value()){ if(0 == pp->TaskQueue_.dequeue(t,3) && NULL != t){ pp->free_ ++; t->work(); pp->free_ --; if(t->autorelease) { delete t; t = NULL; } }else{ t = NULL; } } MutexLockGuard gard(pp->ListLock_); pp->threads_.remove(pthread_self()); return NULL; }
int ThreadPool::getcount() { MutexLockGuard gard(ListLock_); return this->threads_.size(); }