This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MessageQ地址的问题



大家好,

我定义MessageQ的msg为全局变量,如下

#define ARRAY_SIZE 3200
typedef struct MyMessg{
	MessageQ_MsgHeader header;
	UInt8 var[ARRAY_SIZE];
}MyMsg;
MyMsg *msg;

在一个任务中初始化了Heap,使用MessageQ_alloc申请了msg的内存,得到一个地址。改任务如下

static Void InterCoreDataTransfer_Task(UArg arg0, UArg arg1)
{
    //MyMsg           *msg;
    MessageQ_Handle  messageQ;
    MessageQ_QueueId remoteQueueId;
    MessageQ_QueueId remoteQueueId_core1;
    MessageQ_QueueId remoteQueueId_core2;
    MessageQ_QueueId remoteQueueId_core3;
    Int              status;
    UInt16           msgId = 0;
    HeapBufMP_Handle              heapHandle;
    HeapBufMP_Params              heapBufParams;
    Int              i;

    System_printf ("****************************************************************\n");
    System_printf ("**************Inter-Core Data Transfer Task Start***************\n");
    System_printf ("****************************************************************\n");

    if (MultiProc_self() == 0)
    {
        HeapBufMP_Params_init(&heapBufParams);
        heapBufParams.regionId       = 0;
        heapBufParams.name           = HEAP_NAME;
        heapBufParams.numBlocks      = 1;
        heapBufParams.blockSize      = 4096;
        heapHandle = HeapBufMP_create(&heapBufParams);
        if (heapHandle == NULL)
        {
            System_abort("HeapBufMP_create failed\n" );
        }
    }
    else
    {
        do
        {
            status = HeapBufMP_open(HEAP_NAME, &heapHandle);
            if (status < 0)
            {
                Task_sleep(1);
            }
        } while (status < 0);
    }

    MessageQ_registerHeap((IHeap_Handle)heapHandle, HEAPID);
    messageQ = MessageQ_create(localQueueName, NULL);
    if (messageQ == NULL)
    {
        System_abort("MessageQ_create failed\n" );
    }

    //On Core0, open three Message Queue for each core of Core1~Core3
    if(MultiProc_self() == 0)
    {
	do {
	    status = MessageQ_open(nextQueueName_core1, &remoteQueueId_core1);
	    if (status < 0)
	    {
		Task_sleep(1);
	    }
	} while (status < 0);
	do {
	    status = MessageQ_open(nextQueueName_core2, &remoteQueueId_core2);
	    if (status < 0)
	    {
		Task_sleep(1);
	    }
	} while (status < 0);
	do {
	    status = MessageQ_open(nextQueueName_core3, &remoteQueueId_core3);
	    if (status < 0)
	    {
		Task_sleep(1);
	    }
	} while (status < 0);
    }
    //On each core of Core1~Core3, open a Message Queue for Core0
    else
    {
	do {
	    status = MessageQ_open(nextQueueName, &remoteQueueId);
	    if (status < 0)
	    {
		Task_sleep(1);
	    }
	   } while (status < 0);
    }

    if (MultiProc_self() == 0)
    {
        msg = (MyMsg *)MessageQ_alloc(HEAPID, sizeof(MyMsg));
        System_printf("MyMsg Size: %d\n", sizeof(MyMsg) );
        if (msg == NULL)
        {
           System_abort("MessageQ_alloc failed\n" );
        }

	for(i=0; i<ARRAY_SIZE; i++)
	   msg->var[i] = 0;

        while (TRUE)
        {

		for(i=0; i<ARRAY_SIZE; i++)
			msg->var[i]++;

		//Send a msg to Core1's Message Queue
		status = MessageQ_put(remoteQueueId_core1, (MessageQ_Msg)msg);
		if (status < 0)
		{
			System_abort("This should not happen since timeout is forever\n");
		}
		messageq_put_control = 5;        	
        }
    }
    else
    {
	for(i=0; i<ARRAY_SIZE; i++)
	    msg->var[i] = 0;
        while (TRUE)
        {
        	//On each core of Core1~Core3, wait the Core0 to send msg to itself
		status = MessageQ_get(messageQ, (MessageQ_Msg *)&msg, MessageQ_FOREVER);
		if (status < 0)
		{
		      System_abort("MessageQ_put had a failure/error\n");
		}
        }
    }


    System_printf("Inter-Core Data Transfer Task exit!\n");
    BIOS_exit(0);
}

然后再另一个优先级更高的任务中,使用MessageQ_put去发送Message

遇到的问题是:

在任务InterCoreDataTransfer_Task中使用MessageQ_alloc时,得到的msg地址有效,为0x0C00EB80

而在另一个优先级更高的任务中去MessageQ_put这个msg(这个msg为全局变量)时,该msg的地址就变了,变成了一个无效的地址。

导致出现了报错信息。

简单说,我遇到的问题就是:

一个全局的Messgae变量,在一个Task里面MessageQ_alloc了,可以在该Task中MessageQ_put出去。但是却无法在另一个Task中MessageQ_put出去。

  • 看你代码怎么msg是一个函数内部局部变量呢,确认在put msg之前有alloc么。

  • 你好,Andy!

    我贴的代码有误,代码中的 MyMsg           *msg;是被注释掉的,已经改过来了。

    我在另一个Task中调用MessageQ_put之前没有调用MessageQ_alloc。

    如果您能确认,一个全局的Message,可以在一个Task中初始化Heap、注册Heap、alloc Message地址,并在另一个优先级更高的Task中直接MessageQ_put的话,那估计就是我代码别的地方出问题了。