kb_machblue_core_msg.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:7k
- //*****************************************************************************
- //File Name: kb_machblue_core_msg.c
- //
- //Description: msg function
- //
- // used by Machblue to access the platform's thread api
- // used by Machblue to access the platform's message passing
- //
- //Author: steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "gendef.h"
- #include "osp.h"
- #include "machblue_defines.h"
- #include "machblue_porting_core.h"
- /**
- * the msg information struct
- */
- typedef struct _MB_MsgInfo_s
- {
- UINT32 queueID;
- UINT32 msgID;
- struct _MB_MsgInfo_s *pNext;
- }MB_MsgInfo_t;
- /**
- * pointer to store the msg information
- */
- static MB_MsgInfo_t *MsgInfo=NULL;
- #define MB_MAX_MESSAGE 128
- /**
- * add the msg information
- */
- void kb_addMsgInfo(UINT32 queueID,UINT32 msgID)
- {
- MB_MsgInfo_t *pInfo,*pTemp;
- pInfo=(MB_MsgInfo_t *)mb_malloc(sizeof(MB_MsgInfo_t));
- pInfo->queueID=queueID;
- pInfo->msgID=msgID;
- pInfo->pNext=NULL;
- if(MsgInfo==NULL)
- MsgInfo=pInfo;
- else
- {
- pTemp=MsgInfo;
- while(pTemp->pNext!=NULL)
- pTemp=pTemp->pNext;
- pTemp->pNext=pInfo;
- }
- }
- /**
- * delete the msg information
- */
- void kb_deleteMsgInfo(UINT32 msgID)
- {
- MB_MsgInfo_t *pInfo1,*pInfo2;
-
- if(MsgInfo==NULL)
- return;
- pInfo1=MsgInfo;
- pInfo2=pInfo1->pNext;
- if(pInfo1->msgID==msgID)
- {
- MsgInfo=MsgInfo->pNext;
- mb_free(pInfo1);
- return;
- }
- while(pInfo2!=NULL)
- {
- if(pInfo2->msgID==msgID)
- {
- pInfo1->pNext=pInfo2->pNext;
- mb_free(pInfo2);
- break;
- }
-
- pInfo1=pInfo2;
- pInfo2=pInfo1->pNext;
- }
- }
- /**
- * get queue id by msg id from the msg information
- */
- void kb_getQueueID(UINT32 *queueID,UINT32 msgID)
- {
- MB_MsgInfo_t *pInfo;
- pInfo=MsgInfo;
- while(pInfo!=NULL)
- {
- if(pInfo->msgID==msgID)
- {
- *queueID=pInfo->queueID;
- break;
- }
- pInfo=pInfo->pNext;
- }
- }
- /**
- * Creates a new thread.
- * tid < pointer to location to store new thread id >
- * thread_attrib < pointer to attributes of thread to create >
- * start_function < pointer to thread start function >
- * arg < pointer to argument to pass to start function >
-
- * @return a MB_SUCCESS and updates "tid" on success or MB_FAILURE on failure.
- */
- mb_error_t mb_thread_create(mb_tid_t *tid,mb_thread_attributes_t *thread_attrib,
- mb_thread_start_function_t *start_function,void *arg)
- {
- int ret;
- UINT32 queueID,msgID,stackSize;
- T_OSP_QueueInfo *pQueueInfo;
- //create queue
- pQueueInfo = (T_OSP_QueueInfo*)mb_malloc(sizeof(T_OSP_QueueInfo));
- if(pQueueInfo == NULL)
- {
- mb_printf("n[Machblue]:Thread create memory malloc error.");
- return MB_FAILURE;
- }
-
- pQueueInfo->pQueueId = (message_queue_t*)mb_malloc(sizeof(message_queue_t));
- pQueueInfo->pQueueBuf = (void*)mb_malloc(MESSAGE_MEMSIZE_QUEUE(sizeof(mb_message_t),MB_MAX_MESSAGE));
- if( (pQueueInfo->pQueueId == NULL)
- || (pQueueInfo->pQueueBuf == NULL) )
- {
- mb_free((void*)pQueueInfo->pQueueId);
- mb_free((void*)pQueueInfo->pQueueBuf);
- mb_free((void*)pQueueInfo);
- mb_printf("n[Machblue]:Thread create memory malloc error.");
- return MB_FAILURE;
- }
- message_init_queue_timeout(pQueueInfo->pQueueId, pQueueInfo->pQueueBuf, sizeof(mb_message_t),(unsigned int)MB_MAX_MESSAGE);
-
- queueID = (UINT32)pQueueInfo->pQueueId;
- InserQueueNode(pQueueInfo);
-
- //create task
- stackSize=thread_attrib->stack_size;
- if(stackSize==0)
- stackSize=1024 * 8;
- ret=KB_OSPTaskInit("MACHBLUE",stackSize,(void (*)(void*))start_function,3*16,arg,&msgID);
- if(ret!=Ret_OK)
- {
- mb_printf("n[Machblue]:Thread init error[%d].",ret);
- return MB_FAILURE;
- }
-
- *tid=msgID;
- kb_addMsgInfo(queueID,msgID);
-
- return MB_SUCCESS;
- }
- /**
- * Deletes a thread. This simply deletes the handle to the
- * trhead on platforms that have such a notion. The thread should
- * not be killed as a result of this call.
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_thread_delete(mb_tid_t tid)
- {
- KB_OSPRet ret;
- ret=KB_OSPTaskDel(tid);
- if(ret!=Ret_OK)
- {
- mb_printf("n[Machblue]:Thread delete error[%d].",ret);
- return MB_FAILURE;
- }
- kb_deleteMsgInfo(tid);
-
- return MB_SUCCESS;
- }
- /**
- * Joins (waits for completion of) a thread.
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_thread_join(mb_tid_t tid)
- {
- int ret;
- ret=task_wait( (task_t **)&tid,1,(clock_t*)TIMEOUT_INFINITY);
- if(ret<0)
- {
- mb_printf("n[Machblue]:Thread join error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * Gets the current thread id.
- * @return MB_SUCCESS and updates tid on success, MB_FAILURE otherwise.
- */
- mb_error_t mb_thread_id_get(mb_tid_t *tid)
- {
- task_t *taskID=task_id();
-
- if(taskID==NULL)
- {
- mb_printf("n[Machblue]:Thread ID get error.");
- return MB_FAILURE;
- }
- *tid=(mb_tid_t)taskID;
-
- return MB_SUCCESS;
- }
- /**
- * Posts a message to a thread.
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_msg_post(mb_tid_t tid,mb_message_t *msg)
- {
- UINT32 queueID;
- mb_message_t * PtMessage;
- if(msg == NULL)
- {
- mb_printf("n[Machblue]:Msg post NULL.");
- return MB_FAILURE;
- }
- kb_getQueueID(&queueID,tid);
- PtMessage = (mb_message_t *)message_claim_timeout((message_queue_t*)queueID, (clock_t*)TIMEOUT_INFINITY);
- if(PtMessage == NULL)
- {
- mb_printf("n[Machblue]:Msg post error.");
- return MB_FAILURE;
- }
- mb_memcpy(PtMessage, msg, sizeof(mb_message_t));
- message_send((message_queue_t*)queueID, (void*)PtMessage);
-
- return MB_SUCCESS;
- }
- /**
- * Waits for a message in the current thread mesasge queue.
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_msg_wait(mb_message_t *msg)
- {
- UINT32 queueID,msgID;
- void* pMessageReceived;
-
-
- if(msg == NULL)
- {
- mb_printf("n[Machblue]:Msg wait NULL.");
- return MB_FAILURE;
- }
- if(mb_thread_id_get(&msgID)==MB_FAILURE)
- {
- mb_printf("n[Machblue]:Msg wait error.");
- return MB_FAILURE;
- }
-
- kb_getQueueID(&queueID,msgID);
-
- pMessageReceived = message_receive_timeout((message_queue_t*)queueID,(clock_t*)TIMEOUT_INFINITY);
- if(pMessageReceived != NULL)
- {
- mb_memcpy(msg, pMessageReceived, sizeof(mb_message_t));
- message_release((message_queue_t*)queueID, pMessageReceived);
- return MB_SUCCESS;
- }
- else
- {
- mb_printf("n[Machblue]:Msg wait error.");
- return MB_FAILURE;
- }
- }
- /**
- * Gets a message from the current thread message queue without waiting.
- * If the queue is empty it @return immediatly.
- * @return MB_SUCCESS if a message was successfully retrieved, MB_FAILURE otherwise.
- */
- mb_error_t mb_msg_get_no_wait(mb_message_t *msg)
- {
- UINT32 queueID,msgID;
- void* pMessageReceived;
-
-
- if(msg == NULL)
- {
- mb_printf("n[Machblue]:Msg get no wait NULL.");
- return MB_FAILURE;
- }
- if(mb_thread_id_get(&msgID)==MB_FAILURE)
- {
- //mb_printf("n[Machblue]:Msg get no wait error.");
- return MB_FAILURE;
- }
-
- kb_getQueueID(&queueID,msgID);
-
- pMessageReceived = message_receive_timeout((message_queue_t*)queueID, (clock_t*)TIMEOUT_IMMEDIATE);
- if(pMessageReceived != NULL)
- {
- mb_memcpy(msg, pMessageReceived, sizeof(mb_message_t));
- message_release((message_queue_t*)queueID, pMessageReceived);
- return MB_SUCCESS;
- }
- else
- {
- //mb_printf("n[Machblue]:Msg get no wait error.");
- return MB_FAILURE;
- }
- }