Exemple #1
0
//-------------------------------------------------------------------------------------
bool ThreadPool::removeHangThread(TPThread* tptd)
{
	THREAD_MUTEX_LOCK(threadStateList_mutex_);
	std::list<TPThread*>::iterator itr, itr1;
	itr = find(freeThreadList_.begin(), freeThreadList_.end(), tptd);
	itr1 = find(allThreadList_.begin(), allThreadList_.end(), tptd);
	
	if(itr != freeThreadList_.end() && itr1 != allThreadList_.end())
	{
		freeThreadList_.erase(itr);
		allThreadList_.erase(itr1);
		currentThreadCount_--;
		currentFreeThreadCount_--;

		INFO_MSG(boost::format("ThreadPool::removeHangThread: thread.%1% is destroy. "
			"currentFreeThreadCount:%2%, currentThreadCount:%3%\n") %
		(uint32)tptd->getID() % currentFreeThreadCount_ % currentThreadCount_);
		
		SAFE_RELEASE(tptd);
	}
	else
	{
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);		
		
		ERROR_MSG(boost::format("ThreadPool::removeHangThread: not found thread.%1%\n") % 
			(uint32)tptd->getID());
		
		return false;
	}
	
	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);		
	return true;		
}
Exemple #2
0
        void* Log::_logProc(void* arg)
#endif
{
    Log *pLog = (Log *)arg;
    while (!pLog->mbExit)
    {
        THREAD_MUTEX_LOCK(pLog->mMutex);
        while (pLog->mInlist->empty() && !pLog->mbExit)
        {
#if PLATFORM == PLATFORM_WIN32
            THREAD_MUTEX_UNLOCK(pLog->mMutex);
            ::WaitForSingleObject(pLog->mCond, INFINITE);
            THREAD_MUTEX_LOCK(pLog->mMutex);
#else
            pthread_cond_wait(&pLog->mCond, &pLog->mMutex);
#endif
        }

        Log::MsgList *tmpList = pLog->mOutlist;
        pLog->mOutlist = pLog->mInlist;
        pLog->mInlist = tmpList;
        THREAD_MUTEX_UNLOCK(pLog->mMutex);

        pLog->_flushOutlist();
    }

    return 0;
}
Exemple #3
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addBusyThread(TPThread* tptd)
{
    THREAD_MUTEX_LOCK(threadStateList_mutex_);
    std::list<TPThread*>::iterator itr;
    itr = find(freeThreadList_.begin(), freeThreadList_.end(), tptd);

    if(itr != freeThreadList_.end())
    {
        freeThreadList_.erase(itr);
    }
    else
    {
        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
        ERROR_MSG(fmt::format("ThreadPool::addBusyThread: freeThreadList_ not "
                              "found thread.{0}\n",
                              (uint32)tptd->id()));

        delete tptd;
        return false;
    }

    busyThreadList_.push_back(tptd);
    --currentFreeThreadCount_;
    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

    return true;
}
Exemple #4
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addFreeThread(TPThread* tptd)
{
	THREAD_MUTEX_LOCK(threadStateList_mutex_);
	std::list<TPThread*>::iterator itr;
	itr = find(busyThreadList_.begin(), busyThreadList_.end(), tptd);

	if(itr != busyThreadList_.end())
	{
		busyThreadList_.erase(itr);
	}
	else
	{
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		ERROR_MSG(boost::format("ThreadPool::addFreeThread: busyThreadList_ not found thread.%1%\n") %
		 (uint32)tptd->getID());
		
		delete tptd;
		return false;
	}
		
	freeThreadList_.push_back(tptd);
	currentFreeThreadCount_++;
	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
	return true;
}
Exemple #5
0
//-------------------------------------------------------------------------------------
bool ThreadPool::removeHangThread(TPThread* tptd)
{
    THREAD_MUTEX_LOCK(threadStateList_mutex_);
    std::list<TPThread*>::iterator itr, itr1;
    itr = find(freeThreadList_.begin(), freeThreadList_.end(), tptd);
    itr1 = find(allThreadList_.begin(), allThreadList_.end(), tptd);

    if(itr != freeThreadList_.end() && itr1 != allThreadList_.end())
    {
        freeThreadList_.erase(itr);
        allThreadList_.erase(itr1);
        --currentThreadCount_;
        --currentFreeThreadCount_;

        INFO_MSG(fmt::format("ThreadPool::removeHangThread: thread.{0} is destroy. "
                             "currentFreeThreadCount:{1}, currentThreadCount:{2}\n",
                             (uint32)tptd->id(), currentFreeThreadCount_, currentThreadCount_));

        SAFE_RELEASE(tptd);
    }
    else
    {
        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

        ERROR_MSG(fmt::format("ThreadPool::removeHangThread: not found thread.{0}\n",
                              (uint32)tptd->id()));

        return false;
    }

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
    return true;
}
Exemple #6
0
/* =============================================================================
 * thread_barrier
 * -- Simple logarithmic barrier
 * =============================================================================
 */
void
thread_barrier (thread_barrier_t* barrierPtr, long threadId)
{
    long i = 2;
    long base = 0;
    long index;
    long numThread = barrierPtr->numThread;

    if (numThread < 2) {
        return;
    }

    do {
        index = base + threadId / i;
        if ((threadId % i) == 0) {
            THREAD_MUTEX_LOCK(barrierPtr[index].countLock);
            barrierPtr[index].count++;
            while (barrierPtr[index].count < 2) {
                THREAD_COND_WAIT(barrierPtr[index].proceedCond,
                                 barrierPtr[index].countLock);
            }
            THREAD_MUTEX_UNLOCK(barrierPtr[index].countLock);
        } else {
            THREAD_MUTEX_LOCK(barrierPtr[index].countLock);
            barrierPtr[index].count++;
            if (barrierPtr[index].count == 2) {
                THREAD_COND_SIGNAL(barrierPtr[index].proceedCond);
            }
            while (THREAD_COND_WAIT(barrierPtr[index].proceedAllCond,
                                    barrierPtr[index].countLock) != 0)
            {
                /* wait */
            }
            THREAD_MUTEX_UNLOCK(barrierPtr[index].countLock);
            break;
        }
        base = base + numThread / i;
        i *= 2;
    } while (i <= numThread);

    for (i /= 2; i > 1; i /= 2) {
        base = base - numThread / i;
        index = base + threadId / i;
        THREAD_MUTEX_LOCK(barrierPtr[index].countLock);
        barrierPtr[index].count = 0;
        THREAD_COND_SIGNAL(barrierPtr[index].proceedAllCond);
        THREAD_MUTEX_UNLOCK(barrierPtr[index].countLock);
    }
}
Exemple #7
0
//-------------------------------------------------------------------------------------
void ThreadPool::onMainThreadTick()
{
	std::vector<TPTask*> finitasks;

	THREAD_MUTEX_LOCK(finiTaskList_mutex_);

	if(finiTaskList_.size() == 0)
	{
		THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);	
		return;
	}

	std::copy(finiTaskList_.begin(), finiTaskList_.end(), std::back_inserter(finitasks));   
	finiTaskList_.clear();
	THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);	

	std::vector<TPTask*>::iterator finiiter  = finitasks.begin();

	for(; finiiter != finitasks.end(); )
	{
		thread::TPTask::TPTaskState state = (*finiiter)->presentMainThread();

		switch(state)
		{
		case thread::TPTask::TPTASK_STATE_COMPLETED:
			delete (*finiiter);
			finiiter = finitasks.erase(finiiter);
			--finiTaskList_count_;
			break;
			
		case thread::TPTask::TPTASK_STATE_CONTINUE_CHILDTHREAD:
			this->addTask((*finiiter));
			finiiter = finitasks.erase(finiiter);
			--finiTaskList_count_;
			break;
			
		case thread::TPTask::TPTASK_STATE_CONTINUE_MAINTHREAD:
			THREAD_MUTEX_LOCK(finiTaskList_mutex_);
			finiTaskList_.push_back((*finiiter));
			THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);	
			++finiiter;
			break;
			
		default:
			KBE_ASSERT(false);
			break;
		};
	}
}
Exemple #8
0
void Log::printLog(ELogLevel level, const char *msg)
{
    if (NULL == msg)
        return;

    if ((mLevel & (int)level) == 0)
        return;

#if PLATFORM == PLATFORM_WIN32
# ifdef _DEBUG
    if (::IsDebuggerPresent())
    {
        ::OutputDebugString(msg);
    }
# endif
#endif

    THREAD_MUTEX_LOCK(mMutex);

    mInlist->push_back(_MSG());
    _MSG &msgnode = mInlist->back();
    msgnode.level = level;
    msgnode.time = mHasTime ? time(0) : 0;
    msgnode.msg = msg;

    THREAD_MUTEX_UNLOCK(mMutex);

    THREAD_SINGNAL_SET(mCond);
}
Exemple #9
0
SQLRETURN SQL_API SQLNativeSql(SQLHDBC ConnectionHandle,
			       SQLCHAR * InStatementText,
			       SQLINTEGER TextLength1,
			       SQLCHAR * OutStatementText,
			       SQLINTEGER BufferLength,
			       SQLINTEGER * TextLength2Ptr)
{
	hDbc_T *dbc = (hDbc_T *) ConnectionHandle;
	SQLRETURN status = SQL_SUCCESS;
	if (ENABLE_TRACE) {

		ood_log_message(dbc, __FILE__, __LINE__, TRACE_FUNCTION_ENTRY,
				(SQLHANDLE) dbc, 0, "");
	}
	ood_clear_diag((hgeneric *) dbc);
	THREAD_MUTEX_LOCK(dbc);

	THREAD_MUTEX_UNLOCK(dbc);
	if (ENABLE_TRACE) {
		ood_log_message(dbc, __FILE__, __LINE__, TRACE_FUNCTION_EXIT,
				(SQLHANDLE) NULL, status, "");
	}
	fprintf(stderr, "called stubbed function line %d file %s\n", __LINE__,
		__FILE__);
	assert(IS_VALID(dbc));
	return SQL_SUCCESS;
}
Exemple #10
0
//-------------------------------------------------------------------------------------
void ThreadPool::addFiniTask(TPTask* tptask)
{
    THREAD_MUTEX_LOCK(finiTaskList_mutex_);
    finiTaskList_.push_back(tptask);
    ++finiTaskList_count_;
    THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);
}
Exemple #11
0
//-------------------------------------------------------------------------------------
void ThreadPool::onMainThreadTick()
{
    THREAD_MUTEX_LOCK(finiTaskList_mutex_);

    std::list<TPTask*>::iterator finiiter  = finiTaskList_.begin();

    for(; finiiter != finiTaskList_.end(); )
    {
        thread::TPTask::TPTaskState state = (*finiiter)->presentMainThread();

        switch(state)
        {
        case thread::TPTask::TPTASK_STATE_COMPLETED:
            delete (*finiiter);
            finiTaskList_.erase(finiiter++);
            --finiTaskList_count_;
            break;
        case thread::TPTask::TPTASK_STATE_CONTINUE_CHILDTHREAD:
            this->addTask((*finiiter));
            finiTaskList_.erase(finiiter++);
            --finiTaskList_count_;
            break;
        case thread::TPTask::TPTASK_STATE_CONTINUE_MAINTHREAD:
            ++finiiter;
            break;
        default:
            KBE_ASSERT(false);
            break;
        };
    }

    THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);
}
Exemple #12
0
int Log::setLogLevel(int level)
{
    THREAD_MUTEX_LOCK(mMutex);
    int old = mLevel;
    mLevel = level;
    THREAD_MUTEX_UNLOCK(mMutex);

    return old;
}
Exemple #13
0
bool Log::hasTime(bool b)
{
    THREAD_MUTEX_LOCK(mMutex);
    bool old = mHasTime;
    mHasTime = b;
    THREAD_MUTEX_UNLOCK(mMutex);

    return old;
}
TaskBase* TaskDispatcher::TaskPop() {
	THREAD_MUTEX_LOCK(_lock);
	TaskBase* task = NULL;
	if (!_taskQueue.empty()) {
		task = _taskQueue.front();
		_taskQueue.pop();
	}
	THREAD_MUTEX_UNLOCK(_lock);
	return task;
}
Exemple #15
0
//-------------------------------------------------------------------------------------
void ThreadPool::bufferTask(TPTask* tptask)
{
	THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);
	bufferedTaskList_.push(tptask);

	WARNING_MSG(boost::format("ThreadPool::bufferTask: task buffered(%1%)!\n") % 
		(int)bufferedTaskList_.size());

	THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);
}
Exemple #16
0
//-------------------------------------------------------------------------------------
bool ThreadPool::hasThread(TPThread* pTPThread)
{
	bool ret = true;

	THREAD_MUTEX_LOCK(threadStateList_mutex_);

	std::list<TPThread*>::iterator itr1 = find(allThreadList_.begin(), allThreadList_.end(), pTPThread);
	if(itr1 == allThreadList_.end())
		ret = false;

	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

	return ret;
}
Exemple #17
0
//-------------------------------------------------------------------------------------
TPTask* ThreadPool::popbufferTask(void)
{
	TPTask* tptask = NULL;
	THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);

	if(bufferedTaskList_.size()> 0)
	{
		tptask = bufferedTaskList_.front();
		bufferedTaskList_.pop();
	}

	THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);	
	return tptask;
}
Exemple #18
0
//-------------------------------------------------------------------------------------
void ThreadPool::bufferTask(TPTask* tptask)
{
    THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);

    bufferedTaskList_.push(tptask);

    size_t size = bufferedTaskList_.size();
    if(size > THREAD_BUSY_SIZE)
    {
        WARNING_MSG(fmt::format("ThreadPool::bufferTask: task buffered({0})!\n",
                                size));
    }

    THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);
}
Exemple #19
0
SQLRETURN SQL_API SQLGetDescField(SQLHDESC DescriptorHandle,
				  SQLSMALLINT RecNumber,
				  SQLSMALLINT FieldIdentifier,
				  SQLPOINTER ValuePtr,
				  SQLINTEGER BufferLength,
				  SQLINTEGER * StringLengthPtr)
{
	hDesc_T *desc = (hDesc_T *) DescriptorHandle;
	SQLRETURN status = SQL_SUCCESS;

	ood_clear_diag((hgeneric *) desc);
	THREAD_MUTEX_LOCK(desc);

	THREAD_MUTEX_UNLOCK(desc);
	fprintf(stderr, "called stubbed function line %d file %s\n", __LINE__,
		__FILE__);
	return status;
}
Exemple #20
0
//-------------------------------------------------------------------------------------
std::string ThreadPool::printThreadWorks()
{
    std::string ret;

    THREAD_MUTEX_LOCK(threadStateList_mutex_);
    int i = 0;
    std::list<TPThread*>::iterator itr = busyThreadList_.begin();
    for(; itr != busyThreadList_.end(); itr++)
    {
        ret += (fmt::format("{0:p}:({1}), ", (void*)(*itr), (*itr)->printWorkState()));
        i++;

        if(i > 1024)
            break;
    }

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
    return ret;
}
Exemple #21
0
//-------------------------------------------------------------------------------------
TPTask* ThreadPool::popbufferTask(void)
{
    TPTask* tptask = NULL;
    THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);

    size_t size = bufferedTaskList_.size();
    if(size > 0)
    {
        tptask = bufferedTaskList_.front();
        bufferedTaskList_.pop();

        if(size > THREAD_BUSY_SIZE)
        {
            WARNING_MSG(fmt::format("ThreadPool::popbufferTask: task buffered({0})!\n",
                                    size));
        }
    }

    THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);

    return tptask;
}
void TaskDispatcher::TaskPush(TaskBase* task) {
	THREAD_MUTEX_LOCK(_lock);
	_taskQueue.push(task);
	THREAD_MUTEX_UNLOCK(_lock);
}
Exemple #23
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addTask(TPTask* tptask)
{
	THREAD_MUTEX_LOCK(threadStateList_mutex_);
	if(currentFreeThreadCount_ > 0)
	{
		std::list<TPThread*>::iterator itr = freeThreadList_.begin();
		TPThread* tptd = (TPThread*)(*itr);
		freeThreadList_.erase(itr);
		busyThreadList_.push_back(tptd);
		currentFreeThreadCount_--;
		
		//INFO_MSG("ThreadPool::currFree:%d, currThreadCount:%d, busy:[%d]\n",
		//		 currentFreeThreadCount_, currentThreadCount_, busyThreadList_.size());
		
		tptd->setTask(tptask);												// 给线程设置新任务	
		
#if KBE_PLATFORM == PLATFORM_WIN32
		if(tptd->sendCondSignal()== 0){
#else
		if(tptd->sendCondSignal()!= 0){
#endif
			ERROR_MSG("ThreadPool::addTask: pthread_cond_signal is error!\n");
			THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
			return false;
		}
		
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
		return true;
	}
	
	bufferTask(tptask);
	
	if(isThreadCountMax())
	{
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		WARNING_MSG(boost::format("ThreadPool::addTask: thread create is failed, count is full(%1%).\n") % 
			maxThreadCount_);

		return false;
	}

	for(uint32 i=0; i<extraNewAddThreadCount_; i++)
	{
		TPThread* tptd = createThread(300);									// 设定5分钟未使用则退出的线程
		if(!tptd)
		{
#if KBE_PLATFORM == PLATFORM_WIN32		
			ERROR_MSG("ThreadPool::addTask: ThreadPool create new Thread error! ... \n");
#else
			ERROR_MSG(boost::format("boost::format(ThreadPool::addTask: ThreadPool create new Thread error:%1% ... \n") % 
				kbe_strerror());
#endif				
		}

		allThreadList_.push_back(tptd);										// 所有的线程列表
		freeThreadList_.push_back(tptd);									// 闲置的线程列表
		currentThreadCount_++;
		currentFreeThreadCount_++;	
		
	}
	
	INFO_MSG(boost::format("ThreadPool::addTask: new Thread, currThreadCount: %1%\n") % 
		currentThreadCount_);

	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
	return true;
}

//-------------------------------------------------------------------------------------
#if KBE_PLATFORM == PLATFORM_WIN32
unsigned __stdcall TPThread::threadFunc(void *arg)
#else	
void* TPThread::threadFunc(void* arg)
#endif
{
	TPThread * tptd = static_cast<TPThread*>(arg);
	bool isRun = true;

#if KBE_PLATFORM == PLATFORM_WIN32
#else			
	pthread_detach(pthread_self());
#endif

	tptd->onStart();

	while(isRun)
	{
		if(tptd->getTask() != NULL)
		{
			isRun = true;
		}
		else
		{
			isRun = tptd->onWaitCondSignal();
		}

		if(!isRun || tptd->threadPool()->isDestroyed())
		{
			goto __THREAD_END__;
		}

		TPTask * task = tptd->getTask();
		if(task == NULL)
			continue;

		tptd->state_ = THREAD_STATE_BUSY;
		while(task && !tptd->threadPool()->isDestroyed())
		{
			tptd->onProcessTaskStart(task);
			tptd->processTask(task);							// 处理该任务								
			tptd->onProcessTaskEnd(task);

			TPTask * task1 = tptd->tryGetTask();				// 尝试继续从任务队列里取出一个繁忙的未处理的任务

			if(!task1)
			{
				tptd->onTaskComplete();
				break;
			}
			else
			{
				tptd->deleteFiniTask(task);
				task = task1;
				tptd->setTask(task1);
			}
		}
	}

__THREAD_END__:
	TPTask * task = tptd->getTask();
	if(task)
	{
		WARNING_MSG(boost::format("TPThread::threadFunc: task %1% not finish, thread.%2% will exit.\n") % 
			task % tptd);

		delete task;
	}

	if(tptd)
		tptd->onEnd();
	
	tptd->state_ = THREAD_STATE_END;

#if KBE_PLATFORM == PLATFORM_WIN32
	return 0;
#else	
	pthread_exit(NULL);
	return NULL;
#endif		
}
Exemple #24
0
//-------------------------------------------------------------------------------------
void ThreadPool::destroy()
{
	isDestroyed_ = true;

	THREAD_MUTEX_LOCK(threadStateList_mutex_);

	DEBUG_MSG(boost::format("ThreadPool::destroy(): starting size %1%.\n") % 
		allThreadList_.size());
	
	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

	int itry = 0;
	while(true)
	{
		KBEngine::sleep(300);
		itry++;

		THREAD_MUTEX_LOCK(threadStateList_mutex_);
		int count = allThreadList_.size();
		std::list<TPThread*>::iterator itr = allThreadList_.begin();
		for(; itr != allThreadList_.end(); itr++)
		{
			if((*itr))
			{
				if((*itr)->getState() != TPThread::THREAD_STATE_END)
					(*itr)->sendCondSignal();
				else
					count--;
			}
		}

		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		if(count <= 0)
		{
			break;
		}
		else
		{
			WARNING_MSG(boost::format("ThreadPool::destroy(): waiting for thread(%1%), try=%2%\n") % count % itry);
		}
	}

	THREAD_MUTEX_LOCK(threadStateList_mutex_);

	std::list<TPThread*>::iterator itr = allThreadList_.begin();
	for(; itr != allThreadList_.end(); itr++)
	{
		if((*itr))
		{
			delete (*itr);
			(*itr) = NULL;
		}
	}
	
	allThreadList_.clear();
	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

	THREAD_MUTEX_LOCK(finiTaskList_mutex_);
	
	if(finiTaskList_.size() > 0)
	{
		WARNING_MSG(boost::format("ThreadPool::~ThreadPool(): Discarding %1% finished tasks.\n") % 
			finiTaskList_.size());

		std::list<TPTask*>::iterator finiiter  = finiTaskList_.begin();
		for(; finiiter != finiTaskList_.end(); finiiter++)
		{
			delete (*finiiter);
		}
	
		finiTaskList_.clear();
	}

	THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);

	THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);

	if(bufferedTaskList_.size() > 0)
	{
		WARNING_MSG(boost::format("ThreadPool::~ThreadPool(): Discarding %1% buffered tasks.\n") % 
			bufferedTaskList_.size());

		while(bufferedTaskList_.size() > 0)
		{
			TPTask* tptask = bufferedTaskList_.front();
			bufferedTaskList_.pop();
			delete tptask;
		}
	}
	
	THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);

	THREAD_MUTEX_DELETE(threadStateList_mutex_);
	THREAD_MUTEX_DELETE(bufferedTaskList_mutex_);
	THREAD_MUTEX_DELETE(finiTaskList_mutex_);

	DEBUG_MSG("ThreadPool::destroy(): successfully!\n");
}
Exemple #25
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addTask(TPTask* tptask)
{
    THREAD_MUTEX_LOCK(threadStateList_mutex_);
    if(currentFreeThreadCount_ > 0)
    {
        std::list<TPThread*>::iterator itr = freeThreadList_.begin();
        TPThread* tptd = (TPThread*)(*itr);
        freeThreadList_.erase(itr);
        busyThreadList_.push_back(tptd);
        --currentFreeThreadCount_;

        //INFO_MSG("ThreadPool::currFree:%d, currThreadCount:%d, busy:[%d]\n",
        //		 currentFreeThreadCount_, currentThreadCount_, busyThreadList_count_);

        tptd->task(tptask);												// 给线程设置新任务

#if KBE_PLATFORM == PLATFORM_WIN32
        if(tptd->sendCondSignal()== 0) {
#else
        if(tptd->sendCondSignal()!= 0) {
#endif
            ERROR_MSG("ThreadPool::addTask: pthread_cond_signal is error!\n");
            THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
            return false;
        }

        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
        return true;
    }

    bufferTask(tptask);

    if(isThreadCountMax())
    {
        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

        //WARNING_MSG(fmt::format("ThreadPool::addTask: can't createthread, the poolsize is full({}).\n,
        //	maxThreadCount_));

        return false;
    }

    for(uint32 i=0; i<extraNewAddThreadCount_; i++)
    {
        TPThread* tptd = createThread(300);									// 设定5分钟未使用则退出的线程
        if(!tptd)
        {
#if KBE_PLATFORM == PLATFORM_WIN32
            ERROR_MSG("ThreadPool::addTask: the ThreadPool create thread is error! ... \n");
#else
            ERROR_MSG(fmt::format("ThreadPool::addTask: the ThreadPool create thread is error:{0}\n",
                                  kbe_strerror()));
#endif
        }

        allThreadList_.push_back(tptd);										// 所有的线程列表
        freeThreadList_.push_back(tptd);									// 闲置的线程列表
        ++currentThreadCount_;
        ++currentFreeThreadCount_;

    }

    INFO_MSG(fmt::format("ThreadPool::addTask: new Thread, currThreadCount: {0}\n",
                         currentThreadCount_));

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
    return true;
}

//-------------------------------------------------------------------------------------
bool ThreadPool::hasThread(TPThread* pTPThread)
{
    bool ret = true;

    THREAD_MUTEX_LOCK(threadStateList_mutex_);

    std::list<TPThread*>::iterator itr1 = find(allThreadList_.begin(), allThreadList_.end(), pTPThread);
    if(itr1 == allThreadList_.end())
        ret = false;

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

    return ret;
}
Exemple #26
0
//-------------------------------------------------------------------------------------
void ThreadPool::destroy()
{
    isDestroyed_ = true;

    THREAD_MUTEX_LOCK(threadStateList_mutex_);

    DEBUG_MSG(fmt::format("ThreadPool::destroy(): starting size {0}.\n",
                          allThreadList_.size()));

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

    int itry = 0;
    while(true)
    {
        KBEngine::sleep(300);
        itry++;

        std::string taskaddrs = "";
        THREAD_MUTEX_LOCK(threadStateList_mutex_);

        int count = allThreadList_.size();
        std::list<TPThread*>::iterator itr = allThreadList_.begin();
        for(; itr != allThreadList_.end(); itr++)
        {
            if((*itr))
            {
                if((*itr)->state() != TPThread::THREAD_STATE_END)
                {
                    (*itr)->sendCondSignal();
                    taskaddrs += (fmt::format("{0:p},", (void*)(*itr)));
                }
                else
                {
                    count--;
                }
            }
        }

        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

        if(count <= 0)
        {
            break;
        }
        else
        {
            WARNING_MSG(fmt::format("ThreadPool::destroy(): waiting for thread({0})[{1}], try={2}\n", count, taskaddrs, itry));
        }
    }

    THREAD_MUTEX_LOCK(threadStateList_mutex_);

    KBEngine::sleep(100);

    std::list<TPThread*>::iterator itr = allThreadList_.begin();
    for(; itr != allThreadList_.end(); itr++)
    {
        if((*itr))
        {
            delete (*itr);
            (*itr) = NULL;
        }
    }

    allThreadList_.clear();
    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

    THREAD_MUTEX_LOCK(finiTaskList_mutex_);

    if(finiTaskList_.size() > 0)
    {
        WARNING_MSG(fmt::format("ThreadPool::~ThreadPool(): Discarding {0} finished tasks.\n",
                                finiTaskList_.size()));

        std::list<TPTask*>::iterator finiiter  = finiTaskList_.begin();
        for(; finiiter != finiTaskList_.end(); finiiter++)
        {
            delete (*finiiter);
        }

        finiTaskList_.clear();
        finiTaskList_count_ = 0;
    }

    THREAD_MUTEX_UNLOCK(finiTaskList_mutex_);

    THREAD_MUTEX_LOCK(bufferedTaskList_mutex_);

    if(bufferedTaskList_.size() > 0)
    {
        WARNING_MSG(fmt::format("ThreadPool::~ThreadPool(): Discarding {0} buffered tasks.\n",
                                bufferedTaskList_.size()));

        while(bufferedTaskList_.size() > 0)
        {
            TPTask* tptask = bufferedTaskList_.front();
            bufferedTaskList_.pop();
            delete tptask;
        }
    }

    THREAD_MUTEX_UNLOCK(bufferedTaskList_mutex_);

    THREAD_MUTEX_DELETE(threadStateList_mutex_);
    THREAD_MUTEX_DELETE(bufferedTaskList_mutex_);
    THREAD_MUTEX_DELETE(finiTaskList_mutex_);

    DEBUG_MSG("ThreadPool::destroy(): successfully!\n");
}
Exemple #27
0
//-------------------------------------------------------------------------------------
bool ThreadPool::_addTask(TPTask* tptask)
{
	std::list<TPThread*>::iterator itr = freeThreadList_.begin();
	TPThread* tptd = (TPThread*)(*itr);
	freeThreadList_.erase(itr);
	busyThreadList_.push_back(tptd);
	--currentFreeThreadCount_;

	//INFO_MSG("ThreadPool::currFree:%d, currThreadCount:%d, busy:[%d]\n",
	//		 currentFreeThreadCount_, currentThreadCount_, busyThreadList_count_);

	tptd->task(tptask);

#if KBE_PLATFORM == PLATFORM_WIN32
	if (tptd->sendCondSignal() == 0) {
#else
	if (tptd->sendCondSignal() != 0) {
#endif
		ERROR_MSG("ThreadPool::addTask: pthread_cond_signal error!\n");
		return false;
	}

	return true;
}

//-------------------------------------------------------------------------------------
bool ThreadPool::addTask(TPTask* tptask)
{
	THREAD_MUTEX_LOCK(threadStateList_mutex_);
	if(currentFreeThreadCount_ > 0)
	{
		bool ret = _addTask(tptask);
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		return ret;
	}
	
	bufferTask(tptask);
	
	if(isThreadCountMax())
	{
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		//WARNING_MSG(fmt::format("ThreadPool::addTask: can't createthread, the poolsize is full({}).\n,
		//	maxThreadCount_));

		return false;
	}

	for(uint32 i=0; i<extraNewAddThreadCount_; ++i)
	{
		bool threadStartsImmediately = i > 0;

		// 设定5分钟未使用则退出的线程
		TPThread* tptd = createThread(ThreadPool::timeout, threadStartsImmediately);
		if(!tptd)
		{
#if KBE_PLATFORM == PLATFORM_WIN32		
			ERROR_MSG("ThreadPool::addTask: the ThreadPool create thread error! ... \n");
#else
			ERROR_MSG(fmt::format("ThreadPool::addTask: the ThreadPool create thread error:{0}\n", 
				kbe_strerror()));
#endif				
		}
		
		// 所有的线程列表
		allThreadList_.push_back(tptd);	
		
		if (threadStartsImmediately)
		{
			// 闲置的线程列表
			freeThreadList_.push_back(tptd);
			++currentFreeThreadCount_;
		}
		else
		{
			TPTask * pTask = tptd->tryGetTask();
			if (pTask)
			{
				busyThreadList_.push_back(tptd);
				tptd->task(pTask);
			}
			else
			{
				freeThreadList_.push_back(tptd);
				++currentFreeThreadCount_;
			}

			tptd->createThread();
		}

		++currentThreadCount_;
		
		
	}
	
	INFO_MSG(fmt::format("ThreadPool::addTask: new Thread, currThreadCount: {0}\n", 
		currentThreadCount_));

	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
	return true;
}
Exemple #28
0
	virtual void unlock(void)
	{
		THREAD_MUTEX_UNLOCK(mutex_); 
	}	
Exemple #29
0
 void unlockMutex(void)
 {
     THREAD_MUTEX_UNLOCK(mutex_);
 }