UINT __stdcall CThreadPool::_ThreadProc(LPVOID pParam) #endif { //BUGTRAP_THREAD_IN(_ThreadProc) DWORD dwWait; CThreadPool* pool; HANDLE hThread = GetCurrentThread(); LPTHREAD_START_ROUTINE proc; LPVOID data; DWORD dwThreadId = GetCurrentThreadId(); HANDLE hWaits[2]; IRunObject* runObject; bool bAutoDelete; ASSERT(pParam != NULL); if(NULL == pParam) { //BUGTRAP_THREAD_OUT(_ThreadProc) return -1; } pool = static_cast<CThreadPool*>(pParam); hWaits[0] = pool->GetWaitHandle(dwThreadId); hWaits[1] = pool->GetShutdownHandle(); loop_here: dwWait = WaitForMultipleObjects(2, hWaits, FALSE, INFINITE); if(dwWait - WAIT_OBJECT_0 == 0) { if(pool->CheckThreadStop()) { //BUGTRAP_THREAD_OUT(_ThreadProc) return 0; } if(pool->GetThreadProc(dwThreadId, proc, &data, &runObject)) { pool->BusyNotify(dwThreadId); if(proc == NULL) { runObject->Run(); runObject->Release(); } else { // note: the user's data is wrapped inside UserPoolData object proc(data); // Note: data is created by the pool and is deleted by the pool. // The internal user data is not deleted by the pool. UserPoolData* pPoolData = static_cast<UserPoolData*>(data); delete pPoolData; } } else { pool->FinishNotify(dwThreadId); // tell the pool, i am now free } goto loop_here; } //BUGTRAP_THREAD_OUT(_ThreadProc) return 0; }
UINT __stdcall CThreadPool::_ThreadProc(LPVOID pParam) #endif { DWORD dwWait; CThreadPool* pool; HANDLE hThread = GetCurrentThread(); LPTHREAD_START_ROUTINE proc; LPVOID data; DWORD dwThreadId = GetCurrentThreadId(); HANDLE hWaits[2]; IRunObject* runObject; bool bAutoDelete; if(NULL == pParam) { return -1; } pool = static_cast<CThreadPool*>(pParam); hWaits[0] = pool->GetWaitHandle(dwThreadId); hWaits[1] = pool->GetShutdownHandle(); loop_here: dwWait = WaitForMultipleObjects(2, hWaits, FALSE, INFINITE); if(dwWait - WAIT_OBJECT_0 == 0) { if(pool->CheckThreadStop()) { return 0; } // a new function was added, go and get it if(pool->GetThreadProc(dwThreadId, proc, &data, &runObject)) { pool->BusyNotify(dwThreadId); if(proc == NULL) { // a function object is being used instead of // a function pointer. bAutoDelete = runObject->AutoDelete(); runObject->Run(); // see if we need to free this object if(bAutoDelete) { delete runObject; } } else { // note: the user's data is wrapped inside UserPoolData object proc(data); // Note: data is created by the pool and is deleted by the pool. // The internal user data is not deleted by the pool. UserPoolData* pPoolData = static_cast<UserPoolData*>(data); delete pPoolData; } pool->FinishNotify(dwThreadId); // tell the pool, i am now free } goto loop_here; } return 0; }
void CRecBinViewer::Empty () { gPool.StartThread(RECYCLEBIN_THREAD, this, &CRecBinViewer::EmptyHelper); }
int main(int argc, char* argv[]) {//test for(int i=0; i < argc; ++i) { if(strncmp(argv[i], "-d", 2) == 0) { if(daemon(1, 0, 1) < 0) { cout<<"daemon error"<<endl; return -1; } break; } } log("MsgServer max files can open: %d", getdtablesize()); char* listen_ip = config_file.GetConfigName("ListenIP"); char* str_listen_port = config_file.GetConfigName("ListenPort"); char* base_dir = config_file.GetConfigName("BaseDir"); char* str_file_cnt = config_file.GetConfigName("FileCnt"); char* str_files_per_dir = config_file.GetConfigName("FilesPerDir"); char* str_post_thread_count = config_file.GetConfigName("PostThreadCount"); char* str_get_thread_count = config_file.GetConfigName("GetThreadCount"); if (!listen_ip || !str_listen_port || !base_dir || !str_file_cnt || !str_files_per_dir || !str_post_thread_count || !str_get_thread_count) { log("config file miss, exit..."); return -1; } log("%s,%s",listen_ip, str_listen_port); uint16_t listen_port = atoi(str_listen_port); long long int fileCnt = atoll(str_file_cnt); int filesPerDir = atoi(str_files_per_dir); int nPostThreadCount = atoi(str_post_thread_count); int nGetThreadCount = atoi(str_get_thread_count); if(nPostThreadCount <= 0 || nGetThreadCount <= 0) { log("thread count is invalied"); return -1; } g_PostThreadPool.Init(nPostThreadCount); g_GetThreadPool.Init(nGetThreadCount); g_fileManager = FileManager::getInstance(listen_ip, base_dir, fileCnt, filesPerDir); int ret = g_fileManager->initDir(); if (ret) { printf("The BaseDir is set incorrectly :%s\n",base_dir); return ret; } ret = netlib_init(); if (ret == NETLIB_ERROR) return ret; CStrExplode listen_ip_list(listen_ip, ';'); for (uint32_t i = 0; i < listen_ip_list.GetItemCnt(); i++) { ret = netlib_listen(listen_ip_list.GetItem(i), listen_port, http_callback, NULL); if (ret == NETLIB_ERROR) return ret; } signal(SIGINT, Stop); signal (SIGTERM, Stop); signal (SIGQUIT, Stop); signal(SIGPIPE, SIG_IGN); signal (SIGHUP, SIG_IGN); printf("server start listen on: %s:%d\n", listen_ip, listen_port); init_http_conn(); printf("now enter the event loop...\n"); netlib_eventloop(); return 0; }