Exemple #1
0
	void TimerThread::Run()
	{
		while(IsContinue())
		{
			TIMER_REQUEST_T tTimerReq = {0};
			bool bHasReq = false;
			bool bReqExpired = false;
			int nDiffTime = 0;

			// 取一个Timer请求
			{
				Synchronized syn(m_Mutex);
				m_TimerRequestList.sort();

				TimerRequestList::iterator it = m_TimerRequestList.begin();
				if(it != m_TimerRequestList.end())
				{
					tTimerReq = *it;
					bHasReq = true;

					struct timeval tv = {0};
					gettimeofday(&tv, NULL);

					if(tv.tv_sec >= tTimerReq.nTimestamp)
					{
						m_TimerRequestList.pop_front();
						bReqExpired = true;
					}
					else
					{
						nDiffTime = tTimerReq.nTimestamp - tv.tv_sec;
					}
				}
			}

			// 对请求执行定时
			if(bHasReq)
			{
				if(bReqExpired)
				{
					DoTimerRequest(tTimerReq.pRequest);
				}
				else
				{
					Wait(nDiffTime);
				}
			}
			else
			{
				Wait();
			}
		}
	}
void WaitNotifyThread::Run()
{
	DEBUG_FUNCTION();
	assert(-1 != m_nUDPFd);

	while(IsContinue())
	{
		try
		{
			fd_set fds;
			struct timeval tv = {0};
			FD_ZERO(&fds);

			FD_SET(m_nUDPFd, &fds);
			tv.tv_sec = GateConfig::GetInstance()->TUXONE_GATE_WSH_HOUSEKEEPING_INTERVAL();
			int ret = select(m_nUDPFd + 1, &fds, NULL, NULL, &tv);
			// 收到UDP消息
			if(ret > 0)
			{
				logger->debug(LTRACE, "select OK, receive UDP data");
				// 根据消息的类型具体工作
				HousekeepingThread::GetInstance()->Wakeup(false);
				LWPR::Socket::ClearUDPSocket(m_nUDPFd);
			}
			// 超时
			else if(ret == 0)
			{
				logger->debug(LTRACE, "select timeout");
				HousekeepingThread::GetInstance()->Wakeup(false);
			}
			// 发生错误(可能是信号中断)
			else if(ret == -1)
			{
				logger->error(LTRACE, "select error");
			}
		}
		catch(LWPR::Exception& e)
		{
			logger->error(LTRACE, "%s", e.what());
		}
		catch(std::exception& e)
		{
			logger->error(LTRACE, "%s", e.what());
		}
		catch(...)
		{
			logger->error(LTRACE, "Unknow exception");
		}
	}
}
	void EventNetDispatch::Run()
	{
		while(IsContinue())
		{
			// 以后会替换成wait
			Thread::Sleep(3);

			// 清理死去的线程
			ClearDeadThread();

			// 动态调整线程池
			if(IsServiceBusy())
			{
				if(m_nTotalThread < m_EventNetOption.nThreadPoolMax)
				{
					EventNetWorkThread* pThread = new EventNetWorkThread(*this);
					pThread->Start();
					m_listThread.push_back(pThread);
				}
			}
			else
			{
				if(m_nIdleThread > m_EventNetOption.nThreadPoolMaxIdle)
				{
					EventNetWorkThread* pThread = m_listThread.front();
					if(pThread != NULL)
					{
						pThread->StopRunning();
					}
				}
			}
		}

		// 令线程池中线程退出
		std::list< EventNetWorkThread* >::iterator it = m_listThread.begin();
		for(; it != m_listThread.end(); it++)
		{
			(*it)->StopRunning();
		}

		// 清理线程资源
		for(int i = 0; i < 5 && m_listThread.size() > 0; i++)
		{
			ClearDeadThread();
			Thread::Sleep(1);
		}

		Socket::CloseSocket(m_fdConnSelfServer);
		Socket::CloseSocket(m_fdConnSelfClient);
	}
Exemple #4
0
	void ThreadPool::ThreadManage::Run()
	{
		while(IsContinue())
		{
			Wait(10);

			// 清理死去的线程
			m_ThreadPool.ClearDeadThread();

			// 创建线程
			if(m_ThreadPool.IsNeededToCreateThread())
			{
				ThreadPool::ThreadWorker* pWorker = new ThreadPool::ThreadWorker(m_ThreadPool);
				m_ThreadPool.m_ThreadWorkerList.push_back(pWorker);
				pWorker->Start();
			}

			// 销毁线程
			if(m_ThreadPool.IsNeededToDestroyThread())
			{
				ThreadWorkerList::iterator it = m_ThreadPool.m_ThreadWorkerList.begin();
				if(it != m_ThreadPool.m_ThreadWorkerList.end())
				{
					(*it)->StopRunning();
				}
			}
		}

		// 令线程池中线程退出
		ThreadWorkerList::iterator it = m_ThreadPool.m_ThreadWorkerList.begin();
		for(; it != m_ThreadPool.m_ThreadWorkerList.end(); it++)
		{
			(*it)->StopRunning();
		}

		// 清理线程资源
		for(int i = 0; i < 5 && m_ThreadPool.m_ThreadWorkerList.size() > 0; i++)
		{
			m_ThreadPool.ClearDeadThread();
			Thread::Sleep(1);
		}
	}
Exemple #5
0
	void ThreadPool::ThreadWorker::Run()
	{
		// 所有线程数量
		AutomaticCount autoTotalThread(m_ThreadPool.m_nTotalThread);

		while(IsContinue())
		{
			WorkRequest* pReq = NULL;

			{
				// 空闲线程计数
				AutomaticCount autoIdleThread(m_ThreadPool.m_nIdleThread);
				m_ThreadPool.Wait();

				// 取请求
				Synchronized syn(m_ThreadPool.m_Mutex);
				ThreadPool::WorkRequestList::iterator it = m_ThreadPool.m_WorkRequestList.begin();
				if(it != m_ThreadPool.m_WorkRequestList.end())
				{
					pReq = *it;
					m_ThreadPool.m_WorkRequestList.pop_front();
				}
			}

			// 执行请求
			if(pReq != NULL)
			{
				pReq->DoWork();
			}

			// 调整线程池
			if(m_ThreadPool.IsNeededToCreateThread() || m_ThreadPool.IsNeededToDestroyThread())
			{
				m_ThreadPool.m_pThreadManage->Notify();
			}
		}
	}
	void EventNetWorkThread::Run()
	{
		AutomaticCount autoTotalThread(m_EventNetDispatch.m_nTotalThread);

		while(IsContinue())
		{
			try
			{
				// 可读socket
				SOCKET_FD_T nReadySocket = -1;

				{
					// 空闲线程计数
					AutomaticCount autoIdleThread(m_EventNetDispatch.m_nIdleThread);

					// 加锁,获取可读的Socket(进入临界区)
					Synchronized syn(m_EventNetDispatch.m_MutexEvent);

					// 找一个可读socket,开始工作
					if(m_EventNetDispatch.m_listReadableSocket.size() > 0)
					{
						nReadySocket = m_EventNetDispatch.m_listReadableSocket.back();
						m_EventNetDispatch.m_listReadableSocket.pop_back();
					}

					// 如果没有可读socket,则select
					while(-1 == nReadySocket)
					{
						// 判断当前线程是否可以继续执行
						if(!m_EventNetDispatch.IsContinue())
						{
							return;
						}

						fd_set setReadableFd;
						memcpy(&setReadableFd, &m_EventNetDispatch.m_setActiveFd, sizeof(m_EventNetDispatch.m_setActiveFd));
						struct timeval tv = {0};
						tv.tv_sec = m_EventNetDispatch.m_EventNetOption.nHousekeepInterval;
						tv.tv_usec = 0;
						struct timeval *ptv = &tv;

						if(!m_EventNetDispatch.m_EventNetOption.bAllowDoHousekeep)
						{
							ptv = NULL;
						}

						int nCode = select(m_EventNetDispatch.m_fdMax + 1, &setReadableFd, NULL, NULL, ptv);
						// 有socket可读
						if(nCode > 0)
						{
							for(int i = 0; i <= m_EventNetDispatch.m_fdMax; i++)
							{
								if(FD_ISSET(i, &setReadableFd))
								{
									// 更新socket最后活跃时间
									m_EventNetDispatch.m_mapActiveFd[i] = time(NULL);

									// 处理新来连接
									if(i == m_EventNetDispatch.m_fdListen)
									{
										SOCKET_FD_T fd = LWPR::Socket::AcceptSocket(i);
										if(fd != -1)
										{
											// 如果新来的连接超过select支持的最大数,则CloseSocket
											if(fd >= FD_SETSIZE)
											{
												Socket::CloseSocket(fd);
											}
											else
											{
												FD_SET(fd, &m_EventNetDispatch.m_setActiveFd);
												m_EventNetDispatch.m_mapActiveFd[fd] = time(NULL);
												m_EventNetDispatch.m_fdMax = (fd > m_EventNetDispatch.m_fdMax) ? fd : m_EventNetDispatch.m_fdMax;
												if(m_EventNetDispatch.m_EventNetOption.bAllowDoRcvConnHandler)
												{
													try
													{
														m_EventNetDispatch.m_EventNetOption.pHandler->DoReceiveConnection(fd);
													}
													catch(...)
													{
													}
												}
											}
										}
									}
									// 处理自身连接,用来内部控制信息的传递
									else if(i == m_EventNetDispatch.m_fdConnSelfServer)
									{
										LWPR::Buffer buf;
										SOCKET_RET_TYPE_E res = Socket::ReadSocket(i, buf, sizeof(SOCKET_SELF_MSG_T));
										if(res == SOCKET_RET_OK)
										{
											SOCKET_SELF_MSG_T* pMsg = (SOCKET_SELF_MSG_T*)buf.Inout();

											switch(pMsg->nMsgType)
											{
											case SOCKET_SELF_MSG_ADD_FD:
												FD_SET(pMsg->nSocketFd, &m_EventNetDispatch.m_setActiveFd);
												m_EventNetDispatch.m_mapActiveFd[pMsg->nSocketFd] = time(NULL);
												break;
											case SOCKET_SELF_MSG_EXIT_THREAD:
												break;
											default:
												assert(0);
											}
										}
									}
									// 处理第一个可读socket
									else if(-1 == nReadySocket)
									{
										nReadySocket = i;
										FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
									}
									// 处理其余可读socket
									else
									{
										m_EventNetDispatch.m_listReadableSocket.push_back(i);
										FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
									}
								}
							}
						}
						// select 超时
						else if(nCode == 0)
						{
							// 检查不活跃的socket
							if(m_EventNetDispatch.m_EventNetOption.bAllowDoHousekeep)
							{
								for(int i = 0; i <= m_EventNetDispatch.m_fdMax; i++)
								{
									if(FD_ISSET(i, &m_EventNetDispatch.m_setActiveFd))
									{
										if(i == m_EventNetDispatch.m_fdListen)
										{
										}
										else if(i == m_EventNetDispatch.m_fdConnSelfServer)
										{
										}
										else
										{
											if((time(NULL) - m_EventNetDispatch.m_mapActiveFd[i]) >= m_EventNetDispatch.m_EventNetOption.nSocketExpiredTime)
											{
												if(m_EventNetDispatch.m_EventNetOption.bAllowDoCloseHandler)
												{
													try
													{
														m_EventNetDispatch.m_EventNetOption.pHandler->DoCloseExpiredSocket(i);
													}
													catch(...)
													{
													}
												}

												Socket::CloseSocket(i);
												FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
											}
										}
									}
								}
							}
						}
					} // end of while(-1 == nReadableSocket)
				} // end of Lock

				// 准备处理找到的连接
				SOCKET_RET_TYPE_E nRet = SOCKET_RET_OK;
				try
				{
					nRet = m_EventNetDispatch.m_EventNetOption.pHandler->DoReceiveNormalData(nReadySocket);
				}
				catch(...)
				{
					nRet = SOCKET_RET_FAILED;
				}

				switch(nRet)
				{
				case SOCKET_RET_FAILED:
				case SOCKET_RET_TIMEOUT:
					Socket::CloseSocket(nReadySocket);
					break;
				case SOCKET_RET_OK:
					m_EventNetDispatch.MakeSocketDispatching(nReadySocket);
					break;
				case SOCKET_RET_FREE:
					break;
				default:
					assert(0);
				}
			}
			catch(const LWPR::Exception& e)
			{
				fprintf(stderr, "%s\n", e.what());
			}
		}
	}