Example #1
0
AMPVoid _AMRunTaskThreadRun(_AMTask* task)
{
    if(task == AMNULL)
        return AMNULL;
    else
    {
        //线程运行的第一个task.
        AMInt32 index = task->mThreadIndex;// get the index of the current thread from the first task that started it.

        _AMTaskMgr* taskMgr = task->mTaskMgr;
        task->mProc(task->mProcArg);
        AMThreadMutexLock(&taskMgr->mMutex);
        task->mStatus = 2;
        AMThreadMutexUnlock(&taskMgr->mMutex);
        AMTaskMgrRemove((AMTaskMgr*)taskMgr, (AMTask*)task);
       
        //如果还有Task, 继续运行.
        while(1)
        {
            AMInt32 retCode = _AMRunTask(taskMgr, AMTRUE);
            if(retCode != 1)
                break;
        }

		AMThreadMutexLock(&taskMgr->mMutex);
		taskMgr->mThreadsFlag[index] = 1;
		AMThreadMutexUnlock(&taskMgr->mMutex);
    }   
	
    return AMNULL;
}
Example #2
0
AMInt32 AMLooperAddTimeMessage(struct AMLooper* looper, struct AMMessage* message)
{
    //AMLogForDebug("AMLooper", "AMLooperAddTimeMessage");
	AMInt32 ret_code = AME_LOOPER_SCUESS;
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
		
	if( NULL == message)
		return AME_LOOPER_MESSAGE_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if(looper->start_run_falg != LOOPER_RUNNING_TRUE)
		return AME_LOOPER_LOOPER_NOT_STARTED;

	//添加到消息队列尾部, 注意要保持列表线程安全.
	{
		AMInt32 err_code;
		err_code = AMThreadMutexLock(&looper->queue_mutex);
		insert_List(&looper->time_message_queue, message, sizeof(struct AMMessage), STATIC);
		//print_List(&looper->time_message_queue);
		//AMPrintf("\n");
		err_code = AMThreadMutexUnlock(&looper->queue_mutex);
		//添加了一个消息, 条件变量通知
        AMThreadMutexLock(&looper->loop_cond_mutex);
        looper->isHasRecord = 1;
        AMThreadMutexUnlock(&looper->loop_cond_mutex);
		AMThreadCondSignal(&looper->loop_cond);
	}
	return AME_LOOPER_SCUESS;
}
Example #3
0
static AMInt32 _AMRunTask(_AMTaskMgr* _taskMgr, AMBool isUseThisThread)
{
    if(_taskMgr == AMNULL)
        return -1;
    else
    {
        AMInt32 index = 0;
        _AMTask* task = AMNULL;
        _AMTask* nextRunTask = AMNULL;
        AMThreadMutexLock(&_taskMgr->mMutex);
        //find a unrun task.
        task = _taskMgr->mTasks;
        while(task != AMNULL)
        {
            if(task->mStatus == 0)
            {
                nextRunTask = task;
                break;
            }
            task = task->mNext;
        }
        if(nextRunTask != AMNULL && isUseThisThread == AMFALSE)
        {
            //find a empty thread.
            for(index = 0; index < _taskMgr->mConcurNum; index++)
            {
                if(_taskMgr->mThreadsFlag[index] == 1)
                {
                    AMThreadWait(_taskMgr->mThreads[index], AMNULL);
                    _taskMgr->mThreads[index] = AMNULL;
                    _taskMgr->mThreadsFlag[index] = 0;
                }

                if(_taskMgr->mThreadsFlag[index] == 0)
                {
                    nextRunTask->mStatus = 1;
                    _taskMgr->mThreadsFlag[index] = 2;
					nextRunTask->mThreadIndex = index;
                    AMPrintf("Create Thread\n");
                    AMThreadCreate(&_taskMgr->mThreads[index], AMNULL, _AMRunTaskThreadRun, nextRunTask);
                    break;
                }
            }
        }
        else if(nextRunTask != AMNULL && isUseThisThread == AMTRUE)
        {
            nextRunTask->mStatus = 1;
            AMThreadMutexUnlock(&_taskMgr->mMutex);
            AMPrintf("Re use Thread\n");
            task->mProc(task->mProcArg);
            AMThreadMutexLock(&_taskMgr->mMutex);
            task->mStatus = 2;
            AMThreadMutexUnlock(&_taskMgr->mMutex);
            AMTaskMgrRemove((AMTaskMgr*)_taskMgr, (AMTask*)task);
            return 1;
        }
        AMThreadMutexUnlock(&_taskMgr->mMutex);
        return 0;
    }
}
Example #4
0
AMTaskMgr* AMTaskMgrCreate(AMInt32 concurNum)
{
    AMInt32 index = 0;
    if(concurNum < 0)
        concurNum = AMASYNCTASK_DEFAULT_EXECUTE_COUNT;
    else if(concurNum > AMASYNCTASK_MAX_EXECUTE_COUNT)
        concurNum = AMASYNCTASK_MAX_EXECUTE_COUNT;

    _AMTaskMgrInit();
    AMThreadMutexLock(&gMutex);
    for(;index < AMASYNCTASK_SUPPORTED_MAXMGR; index++)
    {
        if(gTaskMgr[index] == AMNULL)
        {
            _AMTaskMgr* _taskMgr = (_AMTaskMgr*)AMMalloc(sizeof(_AMTaskMgr));
            if(_taskMgr == AMNULL)
            {
                AMThreadMutexUnlock(&gMutex);
                return AMNULL;
            }
			gTaskMgrCount++;
            AMMemset(_taskMgr, 0 , sizeof(_AMTaskMgr));
            _taskMgr->mId = index;
            _taskMgr->mConcurNum = concurNum;
            AMThreadMutexCreate(&_taskMgr->mMutex);
            AMThreadMutexUnlock(&gMutex);
            return (AMTaskMgr*)_taskMgr;
        }
    }
    AMThreadMutexUnlock(&gMutex);
    return AMNULL;
}
Example #5
0
//移除一个消息
AMInt32 _AMLooperGetMessage(struct AMLooper* looper, struct AMMessage* message)
{
    //AMLogForDebug("AMLooper", "_AMLooperGetMessage");
	AMInt32 ret_code = AME_LOOPER_SCUESS;
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
		
	if( NULL == message)
		return AME_LOOPER_MESSAGE_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if(looper->start_run_falg != LOOPER_RUNNING_TRUE)
		return AME_LOOPER_LOOPER_NOT_STARTED;

	//拿到第一条消息
	{
		AMInt32 err_code;
		struct AMMessage* front_message;
		err_code = AMThreadMutexLock(&looper->queue_mutex);
		if(empty_List(&looper->message_queue))
		{
			front_message = (struct AMMessage*)front_List(&looper->message_queue);
			memcpy(message, front_message, sizeof(struct AMMessage));
			err_code = pop_front_List(&looper->message_queue);
		}
		else
			ret_code = AME_LOOPER_MESSAGE_NOT_EXIST;
		err_code = AMThreadMutexUnlock(&looper->queue_mutex);
	}

	return ret_code;
}
Example #6
0
//添加一个Handler
AMInt32 AMLooperAddHandler(struct AMLooper* looper, struct AMHandler* handler)
{
    //AMLogForDebug("AMLooper", "AMLooperAddHandler");
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
		
	if( NULL == handler)
		return AME_LOOPER_HANDLE_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if(HANDLE_END_TRUE == handler->end_run_flag)
		return AME_LOOPER_HANDLE_INVALID;

	/*
	if(looper->start_run_falg != LOOPER_RUNNING_TRUE)
		return AME_LOOPER_LOOPER_NOT_STARTED;
	*/
	
	//添加到队列尾部, 注意要保持列表线程安全.
	{
		AMInt32 err_code;
		err_code = AMThreadMutexLock(&looper->queue_mutex);
		err_code = push_back_List(&looper->handler_queue, handler, sizeof(struct AMHandler), DYNAMIC);
		err_code = AMThreadMutexUnlock(&looper->queue_mutex);
		
	}
	
	return AME_LOOPER_SCUESS;
}
Example #7
0
//销毁整个looper.
AMInt32 _AMLooperDestory(struct AMLooper* looper)
{
    //AMLogForDebug("AMLooper", "_AMLooperDestory");
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
	{
		AMInt32 err_code;
		struct AMHandler* temp_handler;
		err_code = AMThreadMutexLock(&looper->queue_mutex);

		//消息销毁
		_AMLooperRemoveAllMsg(looper);
		//Handler销毁
		for(temp_handler = (struct AMHandler*)front_List(&looper->handler_queue); temp_handler != NULL;
			temp_handler = (struct AMHandler*)front_List(&looper->handler_queue))
		{
			pop_front_List(&looper->handler_queue);
			free(temp_handler);
		}

		err_code =AMThreadMutexUnlock(&looper->queue_mutex);

		err_code = AMThreadMutexDestroy(&looper->queue_mutex);
		err_code = AMThreadCondDestroy(&looper->loop_cond);
		err_code = AMThreadMutexDestroy(&looper->loop_cond_mutex);

		destruct_List(&looper->handler_queue);
		destruct_List(&looper->message_queue);
		destruct_List(&looper->time_message_queue);
	}
	free(looper);
	looper = NULL;
	return AME_LOOPER_SCUESS;
}
Example #8
0
//关闭looper
AMInt32 AMLooperClose(struct AMLooper* looper)
{
    //AMLogForDebug("AMLooper", "AMLooperClose");
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	looper->end_run_flag = LOOPER_END_TRUE;
	if(LOOPER_RUNNING_FALSE == looper->start_run_falg)
	{
		return AME_LOOPER_LOOPER_NOT_STARTED;
	}
	else
	{
		//添加了一个消息, 条件变量通知
        AMThreadMutexLock(&looper->loop_cond_mutex);
        looper->isHasRecord = 1;
        AMThreadMutexUnlock(&looper->loop_cond_mutex);
		AMThreadCondSignal(&looper->loop_cond);
	}

	return AME_LOOPER_SCUESS;
}
Example #9
0
AMTask	*	AMTaskMgrGetTask(AMTaskMgr *taskMgr)
{
	_AMTaskMgr* _taskMgr = (_AMTaskMgr*)taskMgr;
	AMAssert(taskMgr);
	
	AMThreadMutexLock(&_taskMgr->mMutex);

	return _taskMgr->mTasks;
}
Example #10
0
AMInt32  AMTaskSetConcurNum(AMInt32 num)
{
    _AMTaskMgrInit();
    AMThreadMutexLock(&gMutex);
    if(num < 0)
        num = AMASYNCTASK_DEFAULT_EXECUTE_COUNT;
    else if(num > AMASYNCTASK_MAX_EXECUTE_COUNT)
        num = AMASYNCTASK_MAX_EXECUTE_COUNT;
    if(gTaskMgrIn != AMNULL)
    {
        _AMTaskMgr* _taskMgr = (_AMTaskMgr*)gTaskMgrIn;
        AMThreadMutexLock(gTaskMgrIn);
        _taskMgr->mConcurNum = num;
        AMThreadMutexUnlock(gTaskMgrIn);
    }
    gConcurNum = num;
    AMThreadMutexUnlock(&gMutex);
    return 0;
}
Example #11
0
AMInt32   AMTaskMgrDestory(AMTaskMgr* taskMgr)
{   
    _AMTaskMgr* _taskMgr = (_AMTaskMgr*)taskMgr;
    if(_taskMgr == AMNULL)
        return -1;
    else
    {
        _AMTask* task = AMNULL;
        _AMTask* taskNext = AMNULL;
        AMInt32  index = 0;
        AMThreadMutexLock(&_taskMgr->mMutex);
        //Remove all finish and unstarded.
        task = _taskMgr->mTasks;
        while(task != AMNULL)
        {
            AMInt32 retCode = 0;
            taskNext = task->mNext;
            AMTaskMgrRemove((AMTaskMgr*)_taskMgr, (AMTask*)task);
            task = taskNext;
        }
        AMThreadMutexUnlock(&_taskMgr->mMutex);
        //WaitFor running thread.
        for(; index < _taskMgr->mConcurNum; index++)
        {
            if(_taskMgr->mThreads[index] != AMNULL)
            {
                AMThreadWait(_taskMgr->mThreads[index], AMNULL);
            }
        }
        //Remove Form  TaskManager.
        AMThreadMutexLock(&gMutex); 
		AMThreadMutexDestroy(&_taskMgr->mMutex);
        gTaskMgr[_taskMgr->mId] = AMNULL;
        gTaskMgrCount--;
        AMThreadMutexUnlock(&gMutex);
		_AMTaskMgrUnint();
        AMFree(_taskMgr);
        return 0;
    }
}
Example #12
0
IMnetRetCode AIMAuthModule_onRspCheckCode(IMnetHandle hIMnetHandle, EventContent* pEventContent)
{
	IAIM *pIM = (IAIM *)IMnetGetReference(hIMnetHandle);
	AIMAuthModule *pAuthModule = AMNULL;
	ImRspCheckAuthCode *pCheckAuth = (ImRspCheckAuthCode *)pEventContent;
	AIMAuth *pAuthFind = AMNULL;
	AMChar *szImgData = AMNULL, *szSession = 0;
	AMInt32 iImgLen = 0, iRetCode = eAIM_RESULT_OK;
	AIM_RSP_CHECK_CODE rspCheckCode;

	AMAssert(pIM);
	pAuthModule = (AIMAuthModule *)pIM->hAuthModule;

	DPRINT("%s >>>>>> session: %s, retCode %d\n", __FUNCTION__, pCheckAuth->szSession, pCheckAuth->retCode);

	AMThreadMutexLock(&pAuthModule->mtxAuth);

	pAuthFind = _AIMAuthModule_findSession(pAuthModule, pCheckAuth->szSession);
	if(pAuthFind)
	{
		rspCheckCode.iAuthId = pAuthFind->iAuth;
		rspCheckCode.iRetCode = pCheckAuth->retCode;

		_AIMAuth_notify(pIM, OnRspCheckCode, &rspCheckCode);

		if(!pCheckAuth->retCode)
		{
			IMnetSendRawPacket(hIMnetHandle, pAuthFind->szRawPacket, pAuthFind->uiPacketLen);
			set_compare(List, &pAuthModule->lstAuth, _AIMAuth_cmpPtr);
			delete_List(&pAuthModule->lstAuth, pAuthFind, sizeof(AIMAuth));
		}
		else
		{
			AIM_NTF_NEED_AUTH ntfNeedAuth;

			ntfNeedAuth.iAuthId = pAuthFind->iAuth;

			ntfNeedAuth.iRetCode  = _AIMAuth_getAuthImg(&szSession, &ntfNeedAuth.pvData, &ntfNeedAuth.iDataLen);
			_AIMAuth_setSession(pAuthFind, szSession);
			
			_AIMAuth_notify(pIM, OnNtfNeedAuth, &ntfNeedAuth);
		}
	}
	else
	{
		iRetCode = eAIM_RESULT_NOT_READY;
	}

	AMThreadMutexUnlock(&pAuthModule->mtxAuth);

	return eIMnetSkip;
}
Example #13
0
AMInt32 _AMLooperGetTimeMessage(struct AMLooper* looper, struct AMMessage* message, AMInt32* left_time)
{
    //AMLogForDebug("AMLooper", "_AMLooperGetTimeMessage");
	AMInt32 ret_code = AME_LOOPER_SCUESS;
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
		
	if( NULL == message)
		return AME_LOOPER_MESSAGE_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if(looper->start_run_falg != LOOPER_RUNNING_TRUE)
		return AME_LOOPER_LOOPER_NOT_STARTED;

	if(NULL == left_time)
		return AME_LOOPER_TIMEVAL_NULL;

	*left_time  = 0;
	//拿到第一条消息
	{
		AMInt32 err_code;
		struct AMMessage* front_message;
		err_code = AMThreadMutexLock(&looper->queue_mutex);
		
		if(empty_List(&looper->time_message_queue))
		{
			struct AMTimeval tp;
			front_message = (struct AMMessage*)back_List(&looper->time_message_queue);
			AMGetUTCTimeEx(&tp);
			*left_time = (front_message->run_tm.tv_sec - tp.tv_sec) * 1000;
			*left_time += (front_message->run_tm.tv_usec - tp.tv_usec) / 1000;
			if(*left_time  > 0)
			{
				ret_code = AME_LOOPER_MESSAGE_NOT_EXIST;
			}
			else 
			{
				*left_time  = 0;
				memcpy(message, front_message, sizeof(struct AMMessage));
				err_code = pop_back_List(&looper->time_message_queue);
			}
		}
		else
			ret_code = AME_LOOPER_MESSAGE_NOT_EXIST;
		err_code = AMThreadMutexUnlock(&looper->queue_mutex);
	}

	return ret_code;
}
Example #14
0
AMInt32 AMTaskRemove(AMTask* task)
{
    AMInt32 retCode = 0;
    _AMTaskMgrInit();
    AMThreadMutexLock(&gMutex);
    if(gTaskMgrIn == AMNULL)
    {
        AMThreadMutexUnlock(&gMutex);
        return -1;
    }
    AMThreadMutexUnlock(&gMutex);
    retCode = AMTaskMgrRemove(gTaskMgrIn, task);
    return retCode;
}
Example #15
0
AMInt32	AIMAuthModule_updateCode(AMHandle hAuthModule, AMInt32 iAuthId)
{
	AIMAuthModule *pAuthModule = (AIMAuthModule *)hAuthModule;
	AIMAuth tmpAuth, *pAuthFind = AMNULL;
	AMInt32 iRetCode = eAIM_RESULT_OK, iImgLen = 0;
	AMChar *szSessin = AMNULL, *szImgData = AMNULL;
	AIM_NTF_NEED_AUTH ntfNeedAuth;

	AMAssert(pAuthModule && iAuthId);

	iRetCode = _AIMAuth_getAuthImg(&szSessin, &szImgData, &iImgLen);

	DPRINT("%s >>>>>> session: %s, imgLen: %d\n", __FUNCTION__, szSessin, iImgLen);

	if(eAIM_RESULT_OK == iRetCode)
	{
		tmpAuth.iAuth = iAuthId;

		AMThreadMutexLock(&pAuthModule->mtxAuth);

		set_compare(List, &pAuthModule->lstAuth, _AIMAuth_cmpId);

		pAuthFind = _AIMAuthModule_findId(pAuthModule, iAuthId);

		if(pAuthFind)
			_AIMAuth_setSession(pAuthFind, szSessin);
		else
			iRetCode = eAIM_RESULT_NOT_READY;

		AMThreadMutexUnlock(&pAuthModule->mtxAuth);
	}

	AMMemset(&ntfNeedAuth, 0, sizeof(ntfNeedAuth));
	ntfNeedAuth.iAuthId = iAuthId;
	ntfNeedAuth.iRetCode = iRetCode;
	if(eAIM_RESULT_OK == iRetCode)
	{
		ntfNeedAuth.pvData = szImgData;
		ntfNeedAuth.iDataLen = iImgLen;
	}

	if(eAIM_RESULT_NOT_READY != iRetCode)
		_AIMAuth_notify(pAuthModule->pIM, OnNtfNeedAuth, &ntfNeedAuth);

	if(ntfNeedAuth.pvData)
		AMFree(ntfNeedAuth.pvData);

	return eAIM_RESULT_OK;
}
Example #16
0
static AMVoid _AMTaskMgrUnint()
{
    if(gMutex == AMNULL)
        return;
    else
    {
        AMInt32 taskCount = 0;
        AMThreadMutexLock(&gMutex);
        taskCount = gTaskMgrCount;
        AMThreadMutexUnlock(&gMutex);
        if(taskCount == 0)
        {
            AMThreadMutexDestroy(&gMutex);
            gMutex = AMNULL;
        }
    }
}
Example #17
0
AMTask* AMTaskRun(AMTaskProc proc, AMPVoid procArg)
{
    AMTask* task;
    _AMTaskMgrInit();
    AMThreadMutexLock(&gMutex);
    if(gTaskMgrIn == AMNULL)
    {
        gTaskMgrIn = AMTaskMgrCreate(gConcurNum);
        if(gTaskMgrIn == AMNULL)
        {
            AMThreadMutexUnlock(&gMutex);
            return AMNULL;
        }
    }
    task = AMTaskMgrRun(gTaskMgrIn, proc, procArg);
    AMThreadMutexUnlock(&gMutex);
    return task;
}
Example #18
0
AMInt32  AMTaskMgrRemove(AMTaskMgr* taskMgr, AMTask* task)
{
    _AMTaskMgr* _taskMgr = (_AMTaskMgr*)taskMgr;
    _AMTask* _task = (_AMTask*)task;
    if(_taskMgr == AMNULL || _task == AMNULL)
        return -1;
    else
    {
        _AMTask* taskNode1 = AMNULL;
        _AMTask* taskNode2 = AMNULL;
        AMThreadMutexLock(&_taskMgr->mMutex);
        taskNode1 = _taskMgr->mTasks;
        while(taskNode1 != AMNULL)
        {
            if(taskNode1->mId == _task->mId && taskNode1->mTaskMgr->mId == _taskMgr->mId)
            {
                if(taskNode1->mStatus == 1)
                {
                    AMThreadMutexUnlock(&_taskMgr->mMutex);
                    return -1;
                }
                else
                {
                    if(taskNode2 == AMNULL)
                        _taskMgr->mTasks = taskNode1->mNext;
                    else
                        taskNode2->mNext = taskNode1->mNext;
                    
					if(taskNode1->mProcFree)
						taskNode1->mProcFree(taskNode1->mProcArg);// free ArgFree.
					
                    AMFree(taskNode1);
                    AMThreadMutexUnlock(&_taskMgr->mMutex);
                    return 0;
                }
            }
            taskNode2 = taskNode1;
            taskNode1 = taskNode2->mNext;
        }
        AMThreadMutexUnlock(&_taskMgr->mMutex);
        return -1;
    }
}
Example #19
0
AMInt32	AIMAuthModule_cancelAuth(AMHandle hAuthModule, AMInt32 iAuthId)
{
	AIMAuthModule *pAuthModule = (AIMAuthModule *)hAuthModule;
	AIMAuth tmpAuth;
	AMInt32 iRetCode = eAIM_RESULT_OK;
	
	tmpAuth.iAuth = iAuthId;

	AMAssert(pAuthModule && iAuthId);

	AMThreadMutexLock(&pAuthModule->mtxAuth);

	set_compare(List, &pAuthModule->lstAuth, _AIMAuth_cmpId);
	delete_List(&pAuthModule->lstAuth, &tmpAuth, sizeof(AIMAuth));

	AMThreadMutexUnlock(&pAuthModule->mtxAuth);

	return iRetCode;
}
Example #20
0
static AMInt32 _AIMAuthModule_addAuth(AMHandle hAuthModule, const AMChar *szSession, 
									  const AMChar *szRawPacket, AMInt32 iPacketLen, AMInt32 *piId)
{
	AIMAuthModule *pAuthModule = (AIMAuthModule *)hAuthModule;
	AIMAuth *pTmpAuth = AMNULL;
	
	AMAssert(piId);

	pTmpAuth = _AIMAuth_create(szSession, szRawPacket, iPacketLen);

	if(pTmpAuth)
	{
		AMThreadMutexLock(&pAuthModule->mtxAuth);
		*piId = pTmpAuth->iAuth = ++pAuthModule->iLastId;
		push_back_List(&pAuthModule->lstAuth, pTmpAuth, sizeof(AIMAuth), DYNAMIC);
		AMThreadMutexUnlock(&pAuthModule->mtxAuth);

		return eAIM_RESULT_OK;
	}
	
	return eAIM_RESULT_MALLOC_ERROR;
}
Example #21
0
AMInt32 AMLooperRemoveTimerMessage(struct AMHandler* handler, AMUInt32 timer)
{
    //AMLogForDebug("AMLooper", "AMLooperRemoveTimerMessage");
	if( NULL == handler)
		return AME_LOOPER_HANDLE_NULL;

	if(NULL == handler->looper)
		return AME_LOOPER_LOOPER_NULL;

	if(LOOPER_END_TRUE == handler->looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if( HANDLE_END_TRUE == handler->end_run_flag)
		return AME_LOOPER_HANDLE_INVALID;

	//移除消息。 
	{
		AMInt32 err_code;
		ListIter* iter = NULL;
		err_code = AMThreadMutexLock(&handler->looper->queue_mutex);
		iter = create_ListIter(&handler->looper->time_message_queue);
		if(!head_ListIter(iter))											
		{
			do
			{
				struct AMMessage* node = (struct AMMessage*)retrieve_ListIter(iter);
				if(node->type == AM_MESSAGE_TIMER && node->v1.ui == timer)
				{
					extract_ListIter(iter);
				}
			}while(!next_ListIter(iter));
		}
		destroy_ListIter(iter);
		err_code = AMThreadMutexUnlock(&handler->looper->queue_mutex);
	}

	return AME_LOOPER_SCUESS;
}
Example #22
0
AMTask*  AMTaskMgrRun(AMTaskMgr* taskMgr, AMTaskProc proc, AMPVoid procArg, AMTaskArgFree procFree)
{
    _AMTaskMgr* _taskMgr = (_AMTaskMgr*)taskMgr;
    if(_taskMgr == AMNULL || proc == AMNULL)
        return AMNULL;
    else
    {
        _AMTask* newTask = AMNULL;
        AMThreadMutexLock(&_taskMgr->mMutex);
        //Create a new _AMTask.
        newTask = (_AMTask*)AMMalloc(sizeof(_AMTask));
        if(newTask == AMNULL)
        {
            AMThreadMutexUnlock(&_taskMgr->mMutex);
            return AMNULL;
        }
        AMMemset(newTask, 0, sizeof(_AMTask));
        newTask->mProc = proc;
        newTask->mProcArg = procArg;
		newTask->mProcFree = procFree;
        newTask->mId = _taskMgr->mNextTaskId++;
        newTask->mTaskMgr = _taskMgr;
        if(_taskMgr->mTasks == AMNULL)
            _taskMgr->mTasks = newTask;
        else
        {
            _AMTask* taskNode = _taskMgr->mTasks;
            while(taskNode->mNext != AMNULL)
            {
                taskNode = taskNode->mNext;
            }
            taskNode->mNext = newTask;
        }
        AMThreadMutexUnlock(&_taskMgr->mMutex);
        _AMRunTask(_taskMgr, AMFALSE);
        return (AMTask*)newTask;
    }
}
Example #23
0
AMInt32	AIMAuthModule_checkCode(AMHandle hAuthModule, AMInt32 iAuthId, const AMChar *szCode)
{
	AIMAuthModule *pAuthModule = (AIMAuthModule *)hAuthModule;
	AIMAuth *pAuthFind = AMNULL;
	AMInt32 iRetCode = eAIM_RESULT_OK;
	AMAssert(pAuthModule && iAuthId && szCode);

	AMThreadMutexLock(&pAuthModule->mtxAuth);

	pAuthFind = _AIMAuthModule_findId(pAuthModule, iAuthId);

	if(pAuthFind && eAUTH_SESSION == pAuthFind->iStatus)
	{
		pAuthFind->iStatus = eAUTH_CHECK;
		IMnetCheckAuthCode(pAuthModule->pIM->pNetModel, pAuthFind->szSession, szCode);
	}
	else
		iRetCode = eAIM_RESULT_NOT_READY;

	AMThreadMutexUnlock(&pAuthModule->mtxAuth);

	return iRetCode;
}
Example #24
0
AMInt32 AMLooperLoop(struct AMLooper* looper)
{
    //AMLogForDebug("AMLooper", "AMLooperLoop");
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	looper->start_run_falg = LOOPER_RUNNING_TRUE;

	do
	{
		struct AMMessage message;
		AMInt32 left_time;
		AMInt32 err_code = 0;
		AMInt32 is_cond_time_set = 0;
		//处理完所有的一般消息
		do
		{
			if(LOOPER_END_TRUE == looper->end_run_flag)
			{
				looper->start_run_falg = LOOPER_RUNNING_FALSE;
				_AMLooperDestory(looper);
				return AME_LOOPER_SCUESS;
			}

			if(_AMLooperGetMessage(looper, &message) == AME_LOOPER_SCUESS)
			{
				message.handler->callback(&message, message.handler->cbArgs);
                if(message.cleanUp != NULL)
                    message.cleanUp(&message);
			}
			else
			{
				break;
			}
		}while(1);

		//处理所有定时消息
		do
		{
			if(LOOPER_END_TRUE == looper->end_run_flag)
			{
				looper->start_run_falg = LOOPER_RUNNING_FALSE;
				_AMLooperDestory(looper);
				return AME_LOOPER_SCUESS;
			}
			err_code = _AMLooperGetTimeMessage(looper, &message, &left_time);
			if(err_code == AME_LOOPER_SCUESS)
			{
				if(message.type == AM_MESSAGE_TIMER)
					((AMVoid(*)(AMPVoid* pArg))message.ref1.ptr)(message.ref2.ptr);
				else
                {
					message.handler->callback(&message, message.handler->cbArgs);
                    if(message.cleanUp != NULL)
                        message.cleanUp(&message);
                }
			}
			else if(left_time > 0)
			{
				is_cond_time_set = 1;
				break;
			}
			else 
			{
				is_cond_time_set = 0;
				break;
			}
		}while(1);
		//用athread_cond来处理.
        AMThreadMutexLock(&looper->loop_cond_mutex);
        while(looper->isHasRecord == 0)
        {
		    if(is_cond_time_set == 0)
		    {
                AMPrintf("AMThreadCondWait Start\n");
			    AMThreadCondWait(&looper->loop_cond, &looper->loop_cond_mutex); 
                AMPrintf("AMThreadCondWait End\n");
			    
		    }
		    else
		    {
			    //还要加上当前的时间
			    struct AMTimeval tm;
			    AMGetUTCTimeEx(&tm);
			    tm.tv_sec += (left_time + tm.tv_usec / 1000) / 1000;
			    tm.tv_usec = (((left_time + tm.tv_usec /1000)) % 1000) * 1000;
			    AMThreadCondTimewait(&looper->loop_cond, &looper->loop_cond_mutex, &tm);
		    }
        }
        looper->isHasRecord = 0;
        AMThreadMutexUnlock(&looper->loop_cond_mutex);
	}while(1);

	return AME_LOOPER_SCUESS;
}
Example #25
0
// 移除一个handler
AMInt32 AMLooperRemoveHandler(struct AMLooper* looper, struct AMHandler* handler)
{
    //AMLogForDebug("AMLooper", "AMLooperRemoveHandler");
	AMInt32 ret_code = AME_LOOPER_SCUESS;
	if(NULL == looper)
		return AME_LOOPER_LOOPER_NULL;
		
	if( NULL == handler)
		return AME_LOOPER_HANDLE_NULL;

	if(LOOPER_END_TRUE == looper->end_run_flag)
		return AME_LOOPER_LOOPER_INVALID;

	if(HANDLE_END_TRUE == handler->end_run_flag)
		return AME_LOOPER_HANDLE_INVALID;

	if(looper->start_run_falg != LOOPER_RUNNING_TRUE)
		return AME_LOOPER_LOOPER_NOT_STARTED;

	//查找Handler
	{
		ListIter* iter;
		AMInt32 err_code;

		//在处理链表之前要锁住
		err_code = AMThreadMutexLock(&looper->queue_mutex);
		iter = create_ListIter(&looper->handler_queue);
		//链表头
		if(!head_ListIter(iter))											
		{
			struct AMHandler* removed_node = NULL;
			do
			{
				struct AMHandler* node = (struct AMHandler*)retrieve_ListIter(iter);
				if(node == handler)
				{
					extract_ListIter(iter);
					removed_node = node;
					break;
				}
			}while(!next_ListIter(iter));

			//找到了要移除的Handler
			if(NULL != removed_node)
			{
				//移除所有该Handler还没有处理的一般消息.
				_AMLooperRemoveHandlerMsg(removed_node, &looper->message_queue);
				//移除所有定时消息.
				_AMLooperRemoveHandlerMsg(removed_node, &looper->time_message_queue);
				//删除Handler对象.
				free(removed_node);
			}
			else
			{
				ret_code =  AME_LOOPER_HANDLE_NOT_EXIST;
				goto AM_REMOVE_LOOPER_HANDLE_END;
			}
		}

AM_REMOVE_LOOPER_HANDLE_END:
		destroy_ListIter(iter);
		err_code =AMThreadMutexUnlock(&looper->queue_mutex);
	}

	return ret_code;
}