Exemplo n.º 1
0
void *ThreadInterlockedExchangePointer( void * volatile *pDest, void *value )
{
	AUTO_LOCK( g_InterlockedMutex );
	void *retVal = *pDest;
	*pDest = value;
	return retVal;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
long ThreadInterlockedExchangeAdd( long volatile *pDest, long value )
{
	AUTO_LOCK( g_InterlockedMutex );
	long retVal = *pDest;
	*pDest += value;
	return retVal;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
	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;
	}
Exemplo n.º 7
0
	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;
	}
Exemplo n.º 8
0
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"));
}
Exemplo n.º 9
0
long ThreadInterlockedCompareExchange( long volatile *pDest, long value, long comperand )
{
	AUTO_LOCK( g_InterlockedMutex );
	long retVal = *pDest;
	if ( *pDest == comperand )
		*pDest = value;
	return retVal;
}
Exemplo n.º 10
0
void *ThreadInterlockedCompareExchangePointer( void * volatile *pDest, void *value, void *comperand )
{
	AUTO_LOCK( g_InterlockedMutex );
	void *retVal = *pDest;
	if ( *pDest == comperand )
		*pDest = value;
	return retVal;
}
Exemplo n.º 11
0
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;
}
Exemplo n.º 12
0
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;
}
Exemplo n.º 13
0
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;
}
Exemplo n.º 14
0
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;
}
Exemplo n.º 15
0
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;
}
Exemplo n.º 16
0
void CQueue::Reply(AM_ERR result)
{
  AUTO_LOCK(mpMutex);

  AM_ASSERT(IsMain());
  AM_ASSERT(mpMsgResult);

  *mpMsgResult = result;
  mpCondReply->Signal();
}
Exemplo n.º 17
0
	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;
	}
Exemplo n.º 18
0
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);
    }
}
Exemplo n.º 19
0
bool CQueue::PeekData(void *pBuffer, AM_UINT size)
{
  AM_ASSERT(IsSub());

  AUTO_LOCK(mpMutex);

  if (mnData > 0) {
    ReadData(pBuffer, size);
    return true;
  }

  return false;
}
Exemplo n.º 20
0
void CQueue::Enable(bool bEnabled)
{
//	AM_ASSERT(IsMain());

  AUTO_LOCK(mpMutex);

  mbDisabled = !bEnabled;

  if (mnGet > 0) {
    mnGet = 0;
    mpCondGet->SignalAll();
  }
}
Exemplo n.º 21
0
	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;
	}
Exemplo n.º 22
0
	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;
	}
Exemplo n.º 23
0
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;
}
Exemplo n.º 24
0
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();
}
Exemplo n.º 25
0
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;
}
Exemplo n.º 26
0
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);
  }
}
Exemplo n.º 27
0
// 获取输入事件
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;
    }
}
Exemplo n.º 28
0
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;
}
Exemplo n.º 29
0
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;
}
Exemplo n.º 30
0
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;
}