void *ThreadInterlockedExchangePointer( void * volatile *pDest, void *value ) { AUTO_LOCK( g_InterlockedMutex ); void *retVal = *pDest; *pDest = value; return retVal; }
int CWorkerThread::Call(unsigned dwParam, unsigned timeout, bool fBoostPriority, WaitFunc_t pfnWait) { AssertMsg(!m_EventSend.Check(), "Cannot perform call if there's an existing call pending" ); AUTO_LOCK( m_Lock ); if (!IsAlive()) return WTCR_FAIL; int iInitialPriority = 0; if (fBoostPriority) { iInitialPriority = BoostPriority(); } // set the parameter, signal the worker thread, wait for the completion to be signaled m_Param = dwParam; m_EventComplete.Reset(); m_EventSend.Set(); WaitForReply( timeout, pfnWait ); if (fBoostPriority) SetPriority(iInitialPriority); return m_ReturnVal; }
long ThreadInterlockedExchangeAdd( long volatile *pDest, long value ) { AUTO_LOCK( g_InterlockedMutex ); long retVal = *pDest; *pDest += value; return retVal; }
AM_ERR CQueue::SendMsg(const void *pMsg, AM_UINT msgSize) { AM_ASSERT(IsMain()); AUTO_LOCK(mpMutex); while (1) { if (mpMsgResult == NULL) { WriteData(mpSendBuffer, pMsg, msgSize); if (mnGet > 0) { mnGet --; mpCondGet->Signal(); } AM_ERR result; mpMsgResult = &result; mpCondReply->Wait(mpMutex); mpMsgResult = NULL; if (mnSendMsg > 0) { mnSendMsg --; mpCondSendMsg->Signal(); } return result; } mnSendMsg ++; mpCondSendMsg->Wait(mpMutex); } return ME_ERROR; }
double scaling_t::progress( std::string& phase, std::string* detailed ) { AUTO_LOCK( mutex ); if ( ! calculate_scale_factors ) return 1.0; if ( num_scaling_stats <= 0 ) return 0.0; if ( current_scaling_stat <= 0 ) { phase = "Baseline"; if ( ! baseline_sim ) return 0; return baseline_sim -> progress(detailed ).pct(); } phase = "Scaling - "; phase += util::stat_type_abbrev( current_scaling_stat ); int completed_scaling_stats = ( num_scaling_stats - remaining_scaling_stats ); double stat_progress = completed_scaling_stats / static_cast<double>( num_scaling_stats ); sim -> detailed_progress( detailed, completed_scaling_stats, num_scaling_stats ); double divisor = num_scaling_stats * 2.0; if ( ref_sim ) stat_progress += divisor * ref_sim -> progress().pct(); if ( ref_sim2 ) stat_progress += divisor * ref_sim2 -> progress().pct(); if ( delta_sim ) stat_progress += divisor * delta_sim -> progress().pct(); if ( delta_sim2 ) stat_progress += divisor * delta_sim2 -> progress().pct(); return stat_progress; }
CloHttp* CloHttpMagr::AddHttp(LPCTSTR szUrl ,bool bFront) { if( ! szUrl ) return NULL; AUTO_LOCK(); CloHttp * phttp = new CloHttp(); if( ! phttp ) { return phttp; } phttp->AddRef(); phttp->SetHttpUrl( szUrl ); POSITION_l insert = NULL; DLHANDLE handle = -1; if( bFront ) { insert = m_pAssoc->m_dlTaskli.first(); } if( -1 != m_pAssoc->m_dlTaskli.add(phttp,insert) ) { handle = m_pAssoc->m_nextId++; phttp->SetHandle(handle); } else { phttp->Release(); phttp = NULL; } return phttp; }
void CloHttpMagr::FreeAll() { // check if the thread is stopped. if( m_pAssoc->m_hMagrThr && WAIT_OBJECT_0 != ::WaitForSingleObject( m_pAssoc->m_hMagrThr, 0 ) ) { while( ResumeThread(m_pAssoc->m_hMagrThr) > 0){}; } m_pAssoc->m_bExit = true; if(m_pAssoc->m_hMagrThr ) {// 退出线程 WaitForSingleObject( m_pAssoc->m_hMagrThr, 1000 ); CloseHandle( m_pAssoc->m_hMagrThr ); m_pAssoc->m_hMagrThr = NULL; } // { AUTO_LOCK(); // 正在下载器 m_pAssoc->m_dlMap.free(call_free_downloader); // 等待下载器 m_pAssoc->m_dlWaitMap.free(call_free_downloader); // 任务下载器 m_pAssoc->m_dlTaskli.free(call_free_downloader); } m_pAssoc->m_bExit = false; }
void RemoteLogImpl::post_to_server(PostType* pTableName, PostData* pData) { AUTO_LOCK(_lock); _post_queue.push_back(*pData); PostData& backData = _post_queue.back(); backData.insert(std::make_pair("tablename", *pTableName)); backData.insert(std::make_pair("privatekey", "a8Dsj12D2p")); }
long ThreadInterlockedCompareExchange( long volatile *pDest, long value, long comperand ) { AUTO_LOCK( g_InterlockedMutex ); long retVal = *pDest; if ( *pDest == comperand ) *pDest = value; return retVal; }
void *ThreadInterlockedCompareExchangePointer( void * volatile *pDest, void *value, void *comperand ) { AUTO_LOCK( g_InterlockedMutex ); void *retVal = *pDest; if ( *pDest == comperand ) *pDest = value; return retVal; }
int DbManagment::Add(DATA_S DataInfo[], unsigned int size) { AUTO_LOCK(mtx); for (unsigned int i = 0; i < size; i++) { m_pDb->Add(DataInfo[i]); } return DATABASE_OK; }
int DbManagment::Delete(unsigned int SeqenceNumber[], unsigned int size) { AUTO_LOCK(mtx); for (unsigned int i = 0; i < size; i++) { m_pDb->Delete(SeqenceNumber[i]); } return DATABASE_OK; }
int64 ThreadInterlockedCompareExchange64( int64 volatile *pDest, int64 value, int64 comperand ) { Assert( (size_t)pDest % 8 == 0 ); AUTO_LOCK( g_InterlockedMutex ); int64 retVal = *pDest; if ( *pDest == comperand ) *pDest = value; return retVal; }
void *CX360SmallBlockPool::Alloc() { void *pResult = m_FreeList.Pop(); if ( !pResult ) { if ( !m_pNextAlloc && gm_pPhysicalBlock >= gm_pPhysicalLimit ) { return NULL; } int nBlockSize = m_nBlockSize; byte *pCurBlockEnd; byte *pNextAlloc; for (;;) { pCurBlockEnd = m_pCurBlockEnd; pNextAlloc = m_pNextAlloc; if ( pNextAlloc + nBlockSize <= pCurBlockEnd ) { if ( m_pNextAlloc.AssignIf( pNextAlloc, pNextAlloc + m_nBlockSize ) ) { pResult = pNextAlloc; break; } } else { AUTO_LOCK( m_CommitMutex ); if ( pCurBlockEnd == m_pCurBlockEnd ) { for (;;) { if ( gm_pPhysicalBlock >= gm_pPhysicalLimit ) { m_pCurBlockEnd = m_pNextAlloc = NULL; return NULL; } byte *pPhysicalBlock = gm_pPhysicalBlock; if ( ThreadInterlockedAssignPointerIf( (void **)&gm_pPhysicalBlock, (void *)(pPhysicalBlock + PAGESIZE_X360_SBH), (void *)pPhysicalBlock ) ) { int index = (size_t)((byte *)pPhysicalBlock - gm_pPhysicalBase) / PAGESIZE_X360_SBH; gm_AddressToPool[index] = this; m_pNextAlloc = pPhysicalBlock; m_CommittedSize += PAGESIZE_X360_SBH; __sync(); m_pCurBlockEnd = pPhysicalBlock + PAGESIZE_X360_SBH; break; } } } } } } return pResult; }
AM_ERR CQueue::Construct(AM_UINT blockSize, AM_UINT nReservedSlots) { mBlockSize = ROUND_UP(blockSize, 4); // mpSendBuffer + list_node + list_node + ... + list_node mpReservedMemory = new AM_U8[NODE_SIZE * (nReservedSlots + 1)]; if (mpReservedMemory == NULL) { return ME_NO_MEMORY; } // for SendMsg() mpSendBuffer = (List*) mpReservedMemory; mpSendBuffer->pNext = NULL; mpSendBuffer->bAllocated = false; // reserved nodes, keep in free-list List *pNode = (List*) (mpReservedMemory + NODE_SIZE); for (; nReservedSlots > 0; nReservedSlots --) { pNode->bAllocated = false; pNode->pNext = mpFreeList; mpFreeList = pNode; pNode = (List*) ((AM_U8*) pNode + NODE_SIZE); } if (IsMain()) { if ((mpMutex = CMutex::Create(false)) == NULL) { return ME_OS_ERROR; } if ((mpCondGet = CCondition::Create()) == NULL) { return ME_OS_ERROR; } if ((mpCondReply = CCondition::Create()) == NULL) { return ME_OS_ERROR; } if ((mpCondSendMsg = CCondition::Create()) == NULL) { return ME_OS_ERROR; } } else { mpMutex = mpMainQ->mpMutex; mpCondGet = mpMainQ->mpCondGet; mpCondReply = mpMainQ->mpCondReply; mpCondSendMsg = mpMainQ->mpCondSendMsg; // attach to main-Q AUTO_LOCK(mpMainQ->mpMutex); mpPrevQ = mpMainQ->mpPrevQ; mpNextQ = mpMainQ; mpPrevQ->mpNextQ = this; mpNextQ->mpPrevQ = this; } return ME_OK; }
void CQueue::Reply(AM_ERR result) { AUTO_LOCK(mpMutex); AM_ASSERT(IsMain()); AM_ASSERT(mpMsgResult); *mpMsgResult = result; mpCondReply->Signal(); }
bool CloHttpMagr::FreeHttp( DLHANDLE dlHandle ) { AUTO_LOCK(); CloHttpManagerAssoc::Free(&m_pAssoc->m_dlTaskli,dlHandle); CloHttpManagerAssoc::Free(&m_pAssoc->m_dlWaitMap,dlHandle); CloHttpManagerAssoc::Free(&m_pAssoc->m_dlMap,dlHandle); return true; }
void Thread::resume() { //错误也能返回 AUTO_LOCK(&lock); is_change = true; //if(name.length() > 0) trace("start resume thread: %s", name.c_str()); if(is_awake == false) { is_awake = true; pthread_cond_signal(&cond_t); } }
bool CQueue::PeekData(void *pBuffer, AM_UINT size) { AM_ASSERT(IsSub()); AUTO_LOCK(mpMutex); if (mnData > 0) { ReadData(pBuffer, size); return true; } return false; }
void CQueue::Enable(bool bEnabled) { // AM_ASSERT(IsMain()); AUTO_LOCK(mpMutex); mbDisabled = !bEnabled; if (mnGet > 0) { mnGet = 0; mpCondGet->SignalAll(); } }
bool CloHttpMagr::FreeTaskHttp(POSITION_l pos) { AUTO_LOCK(); void* element = m_pAssoc->m_dlTaskli.remove(pos); if( element ) { call_free_downloader(element); return true; } return false; }
CloHttp * CloHttpMagr::GetTaskHttp( DLHANDLE dlHandle ) { if( dlHandle == -1 ) return NULL; AUTO_LOCK(); POSITION_l pos = m_pAssoc->m_dlTaskli.query( call_query_download_with_dlhandle ,(void*)dlHandle); if( pos ) { return (CloHttp*)m_pAssoc->m_dlTaskli.get( pos ); } return NULL; }
bool CQueue::PeekMsg(void *pMsg, AM_UINT msgSize) { AM_ASSERT(IsMain()); AUTO_LOCK(mpMutex); if (mnData > 0) { if (pMsg) ReadData(pMsg, msgSize); return true; } return false; }
void CSotpClient::stopAllClient(void) { AUTO_LOCK(theSotpClientList); //for_each(theSotpClientList.begin(), theSotpClientList.end(), // boost::bind(&CgcBaseClient::StopClient, boost::bind(&std::map<unsigned long, CgcBaseClient::pointer>::value_type::second,_1))); CLockMap<DoSotpClientHandler*, CgcBaseClient::pointer>::iterator pIter; for (pIter=theSotpClientList.begin(); pIter!=theSotpClientList.end(); pIter++) { pIter->second->StopClient(); } theSotpClientList.clear(false); //m_pIoService.reset(); }
const char *CThread::GetName() { AUTO_LOCK( m_Lock ); if ( !m_szName[0] ) { #ifdef _WIN32 _snprintf( m_szName, sizeof(m_szName) - 1, "Thread(%p/%p)", this, m_hThread ); #elif _LINUX _snprintf( m_szName, sizeof(m_szName) - 1, "Thread(%0x%x/0x%x)", this, m_threadId ); #endif m_szName[sizeof(m_szName) - 1] = 0; } return m_szName; }
void CQueue::GetMsg(void *pMsg, AM_UINT msgSize) { AM_ASSERT(IsMain()); AUTO_LOCK(mpMutex); while (1) { if (mnData > 0) { ReadData(pMsg, msgSize); return; } mnGet ++; mpCondGet->Wait(mpMutex); } }
// 获取输入事件 BOOL MSCALL CMs2DInput::Ms2DInput_GetEvent(CMs2DInputEvent* pEvent) { AUTO_LOCK(m_InputLock); if (m_Ms2DInputEventQueue.m_CMsQueueManager.GetCurrentCount() > 0) { DWORD dwSize; ULONG_PTR ulIndex = (ULONG_PTR)(m_Ms2DInputEventQueue.m_CMsQueueManager.GetData(dwSize)); m_Ms2DInputEventQueue.m_CMsQueueManager.DeleteData(); memcpy(pEvent, &m_Ms2DInputEventQueue.m_pInputEventQueue[ulIndex], sizeof(*pEvent)); return TRUE; } else { return FALSE; } }
void sim_close_sock (SOCKET sock, t_bool master) { #if defined (_WIN32) closesocket (sock); if (master) { AUTO_LOCK(sim_sock_cnt_lock); sim_sock_cnt = sim_sock_cnt - 1; if (sim_sock_cnt <= 0) { WSACleanup (); sim_sock_cnt = 0; } } #else close (sock); #endif return; }
AM_ERR CQueue::PutData(const void *pBuffer, AM_UINT size) { AM_ASSERT(IsSub()); AUTO_LOCK(mpMutex); List *pNode = AllocNode(); if (pNode == NULL) return ME_NO_MEMORY; WriteData(pNode, pBuffer, size); if (mpMainQ->mnGet > 0) { mpMainQ->mnGet --; mpMainQ->mpCondGet->Signal(); } return ME_OK; }
void *CSmallBlockPool::Alloc() { void *pResult = m_FreeList.Pop(); if ( !pResult ) { int nBlockSize = m_nBlockSize; byte *pCommitLimit; byte *pNextAlloc; for (;;) { pCommitLimit = m_pCommitLimit; pNextAlloc = m_pNextAlloc; if ( pNextAlloc + nBlockSize <= pCommitLimit ) { if ( m_pNextAlloc.AssignIf( pNextAlloc, pNextAlloc + m_nBlockSize ) ) { pResult = pNextAlloc; break; } } else { AUTO_LOCK( m_CommitMutex ); if ( pCommitLimit == m_pCommitLimit ) { if ( pCommitLimit + COMMIT_SIZE <= m_pAllocLimit ) { if ( !VirtualAlloc( pCommitLimit, COMMIT_SIZE, VA_COMMIT_FLAGS, PAGE_READWRITE ) ) { Assert( 0 ); return NULL; } m_pCommitLimit = pCommitLimit + COMMIT_SIZE; } else { return NULL; } } } } } return pResult; }