void SplitCmd(char *buf) { g_cmd_lock.Lock(); if (!buf) { g_cmd_lock.Unlock(); return; } int buf_len = strlen(buf); if (buf_len <= 0) { g_cmd_lock.Unlock(); return; } g_cmd_string_vec.clear(); string cmd; for (int i = 0; i < buf_len; ++i) { if (buf[i] != ' ' && buf[i] != '\t') { cmd.push_back(buf[i]); } else { if (!cmd.empty()) { g_cmd_string_vec.push_back(cmd); cmd.clear(); } } } // push the last cmd if (!cmd.empty()) { g_cmd_string_vec.push_back(cmd); } g_cmd_lock.Unlock(); }
void ChatDialog::InitWindow() { m_pInput = static_cast<CRichEditUI*>(GetPaintMgr()->FindControl(kInputRichEditControlName)); m_pMsgView = static_cast<CRichEditUI*>(GetPaintMgr()->FindControl(kViewRichEditControlName)); if (theApp->HasMsgForUserInQueue(friend_.m_loadName)) { theLockMsgQue.Lock(); typedef std::map<CString,std::vector<CMsg*> >::iterator iter; iter it; for (it=theMsgQueue.begin();it!=theMsgQueue.end();++it) { if(it->first==friend_.m_loadName) { std::vector<CMsg*> msg = it->second; for (int i=0;i<msg.size();++i) { ShowMsg(msg[i]); } theMsgQueue.erase(it); break; } } theLockMsgQue.UnLock(); } }
TCGvoid CPlaybackAdapter::PlayFrame() { /* For multithread rendering, we will parse event always. */ while (TCG_TRUE) { #ifdef ANDROID _Lock.Lock(); TCGbool bWindowMax = g_bWindowMax; _Lock.UnLock(); if (m_parser.IsEglSwapEvent() || !bWindowMax) { m_parser.ClearEglSwapFlag(); } if (!bWindowMax) { continue; } #endif m_parser.ParseSingleEvent(); m_player.SyncLog(); #if defined(USE_FILE) || TCG_AUTO_TEST if (g_bParserFinish && IsRenderFinish()) { break; } #endif } m_player.ResetFrame(); m_player.SyncLog(); }
void ExecCmd(ClientConn *client_conn) { g_cmd_lock.Lock(); if (g_cmd_string_vec.empty()) { g_cmd_lock.Unlock(); return; } if (g_cmd_string_vec[0] == "login") { if (g_cmd_string_vec.size() == 3) { SendLoginRequest(client_conn, g_cmd_string_vec[1], g_cmd_string_vec[2]); } else { PrintHelp(); } } else if (g_cmd_string_vec[0] == "logout") { SendLogoutRequest(client_conn); } else if (g_cmd_string_vec[0] == "send") { if (g_cmd_string_vec.size() == 3) { SendMessageRequest(client_conn, g_cmd_string_vec[1], g_cmd_string_vec[2]); } else { PrintHelp(); } } else { PrintHelp(); } g_cmd_string_vec.clear(); g_cmd_lock.Unlock(); }
static TCGvoid EngineHandleCmd(struct android_app* app, TCGint cmd) { struct Engine* engine = (struct Engine*)app->userData; switch (cmd) { case APP_CMD_SAVE_STATE: // The system has asked us to save our current state. Do so. engine->app->savedState = TCG_MALLOC(sizeof(struct SavedState), eTCG_MAIN); *((struct SavedState*)engine->app->savedState) = engine->state; engine->app->savedStateSize = sizeof(struct SavedState); break; case APP_CMD_INIT_WINDOW: //窗口初始化 _Lock.Lock(); g_bWindowMax = TCG_TRUE; _Lock.UnLock(); if (engine->app->window != NULL) { g_WindowType = engine->app->window; } break; case APP_CMD_TERM_WINDOW: //窗口销毁 _Lock.Lock(); g_bWindowMax = TCG_FALSE; _Lock.UnLock(); break; case APP_CMD_GAINED_FOCUS: // When our app gains focus, we start monitoring the accelerometer. if (engine->accelerometerSensor != NULL) { ASensorEventQueue_enableSensor(engine->sensorEventQueue, engine->accelerometerSensor); // We'd like to get 60 events per second (in us). ASensorEventQueue_setEventRate(engine->sensorEventQueue, engine->accelerometerSensor, (1000L/60)*1000); } break; case APP_CMD_LOST_FOCUS: // When our app loses focus, we stop monitoring the accelerometer. // This is to avoid consuming battery while not being used. if (engine->accelerometerSensor != NULL) { ASensorEventQueue_disableSensor(engine->sensorEventQueue, engine->accelerometerSensor); } // Also stop animating. engine->animating = 0; break; default: break; } }
HttpManager* HttpManager::getInstance() { if (httpManager == NULL) { cLock.Lock(); if (httpManager == NULL) { httpManager = new HttpManager(); } cLock.Unlock(); } return httpManager; }
CAccessWriteHandle * CAccessWriteHandle::getInstance() { if(_instance == NULL ) { writeInstanceLock.lock(); if( _instance == NULL ) { _instance= new CAccessWriteHandle(); } writeInstanceLock.unlock(); } return _instance; }
void ChatDialog::OnExit(TNotifyUI& msg) { thelockChatDlgList.Lock(); POSITION pos,temp; for (pos=theChatDialogList.GetHeadPosition();pos!=NULL;) { temp = pos; ChatDialog* pDlg = (ChatDialog*)theChatDialogList.GetNext(pos); if (pDlg&&pDlg==this) { theChatDialogList.RemoveAt(temp); } } thelockChatDlgList.UnLock(); Close(); }
void COSError::LogMessage(const TCHAR* msg) { static CLock lock; TCHAR txt[512]; size_t len = CTimeWrap::GetNow(txt, _countof(txt)); _stprintf_s(txt + len, _countof(txt) - len, _T(" %s%s"), msg, _T("\r\n")); lock.Lock(); try { if(m_proc!=NULL) m_proc(txt); else if(m_show_out) _tprintf_s(_T("%s"), txt); if(m_log != NULL) _fputts(txt, m_log); } catch(...) { } lock.UnLock(); }
CStaticsTask * CStaticsTask::getStaticsTask(uint32_t sourceId) { std::map<uint32_t,CStaticsTask *>::iterator it=gMapCStaticsTask.find(sourceId); if( it!=gMapCStaticsTask.end()) { return (*it).second; } instanceLock.lock(); it=gMapCStaticsTask.find(sourceId); if(it==gMapCStaticsTask.end()) { CStaticsTask * p = new CStaticsTask(sourceId); gMapCStaticsTask.insert(std::pair<uint32_t,CStaticsTask *>(sourceId,p)); //每个dataid(view)建了一个线程池 if(gMapCStaticsTask.size()==1) CStaticsTask::gThreadTaskPool = new Common::CThreadPool(16,20,100000); instanceLock.unlock(); return p; } else { instanceLock.unlock(); return (*it).second; } }
unsigned __stdcall ThreadDownLoad(void* pParam) { struct threadInfo *pthreadInfo = (struct threadInfo *)pParam; if(pthreadInfo->bState) return 0; //BOOL bError = FALSE; // 计算临时文件中已成功下载部分大小. DWORD occupy_size = 0; DWORD dwFileSize = myfile.GetDownloadedFileSizeByName(pthreadInfo->csDesFileName, &occupy_size); DWORD dwSectSize = pthreadInfo->dwEnd - pthreadInfo->dwStart; // 此段已下载完毕. if( dwFileSize >= dwSectSize ) { //InterlockedIncrement(&g_finishedThreadNumber); pthreadInfo->bState = true; return 0; } BYTE * pStore = NULL; while (!pStore) { pStore = (BYTE*)::VirtualAlloc(NULL, PAGE_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } HANDLE hFile = myfile.GetFilePointer(pthreadInfo->csDesFileName, occupy_size); if(hFile == INVALID_HANDLE_VALUE) { //SetErrorDuringDownload(); ::VirtualFree(pStore, 0, MEM_RELEASE); return 0; } SOCKET hSocket; if(!g_csProxy.IsEmpty()) { hSocket = CHttpGet::ConnectHttpProxy(g_csProxy,g_ProxyPort); } else { hSocket = CHttpGet::ConnectHttpNonProxy(pthreadInfo->csHost,80); } if(hSocket == INVALID_SOCKET) { SetErrorDuringDownload(); CloseHandle(hFile); ::VirtualFree(pStore, 0, MEM_RELEASE); return 0; } // 设置下载范围. DWORD dwFileLength = 0; if (!CHttpGet::SendHttpHeader(hSocket,pthreadInfo->csHost,pthreadInfo->csPath, pthreadInfo->csFileName,dwFileLength,pthreadInfo->dwStart+dwFileSize)) { g_bErrorDuringDownload = true; closesocket(hSocket); CloseHandle(hFile); ::VirtualFree(pStore, 0, MEM_RELEASE); return 0; } int nLen; DWORD nSumLen=0; char szBuffer[1024]; BYTE* p = pStore; while(!g_bStopDownload && !g_bErrorDuringDownload) { if(nSumLen>=dwSectSize-dwFileSize) break; nLen = recv(hSocket,szBuffer,sizeof(szBuffer),0); if (nLen == SOCKET_ERROR) { int err_code = WSAGetLastError(); if (WSAEWOULDBLOCK != err_code) { SetErrorDuringDownload(); break; } else { Sleep(300); continue; } } else if(nLen==0) break; nSumLen +=nLen; if (nSumLen>=dwSectSize-dwFileSize) { g_lock.Lock(); g_downloaded += nLen - (nSumLen - (dwSectSize-dwFileSize)); g_lock.Unlock(); } else { g_lock.Lock(); g_downloaded += nLen; g_lock.Unlock(); } if ( (p + nLen) >= (pStore + PAGE_SIZE) ) { memcpy(p, szBuffer, pStore + PAGE_SIZE - p); // CRC value //unsigned long crc_value = crc32.Calculate(pStore, WRITE_SIZE); //memcpy(pStore + WRITE_SIZE, &crc_value, sizeof(unsigned long)); DWORD dwWritten; if (0 == WriteFile(hFile, pStore, PAGE_SIZE, &dwWritten, NULL)) { SetErrorDuringDownload(); break; } memcpy(pStore, szBuffer + (pStore + PAGE_SIZE - p), (p + nLen) - (pStore + PAGE_SIZE)); p = (p + nLen) - PAGE_SIZE; } else { memcpy(p, szBuffer, nLen); p += nLen; } } if (!g_bStopDownload && !g_bErrorDuringDownload && p > pStore) { // CRC value //unsigned long crc_value = crc32.Calculate(pStore, p - pStore); //memcpy(p, &crc_value, sizeof(unsigned long)); DWORD dwWritten; WriteFile(hFile, pStore, p - pStore, &dwWritten, NULL); } closesocket(hSocket); // 关闭套接字. CloseHandle(hFile); ::VirtualFree(pStore, 0, MEM_RELEASE); //InterlockedIncrement(&g_finishedThreadNumber); pthreadInfo->bState = true; return 0; }