/* * ======== loopbackFxn======== * Receive and return messages. * Run at priority lower than tsk1Fxn above. * Inputs: * - arg0: number of the thread, appended to MessageQ host and slave names. */ Void loopbackFxn(UArg arg0, UArg arg1) { MessageQ_Msg getMsg; MessageQ_Handle messageQ; MessageQ_QueueId remoteQueueId; Int status; UInt16 msgId = 0; Char localQueueName[64]; Char hostQueueName[64]; System_printf("Thread loopbackFxn: %d\n", arg0); System_sprintf(localQueueName, "%s_%d", SLAVE_MESSAGEQNAME, arg0); System_sprintf(hostQueueName, "%s_%d", HOST_MESSAGEQNAME, arg0); /* Create a message queue. */ messageQ = MessageQ_create(localQueueName, NULL); if (messageQ == NULL) { System_abort("MessageQ_create failed\n"); } System_printf("loopbackFxn: created MessageQ: %s; QueueID: 0x%x\n", localQueueName, MessageQ_getQueueId(messageQ)); System_printf("Start the main loop: %d\n", arg0); while (msgId < NUMLOOPS) { /* Get a message */ status = MessageQ_get(messageQ, &getMsg, MessageQ_FOREVER); if (status != MessageQ_S_SUCCESS) { System_abort("This should not happen since timeout is forever\n"); } remoteQueueId = MessageQ_getReplyQueue(getMsg); #ifndef BENCHMARK System_printf("%d: Received message #%d from core %d\n", arg0, MessageQ_getMsgId(getMsg), MessageQ_getProcId(remoteQueueId)); #endif /* test id of message received */ if (MessageQ_getMsgId(getMsg) != msgId) { System_abort("The id received is incorrect!\n"); } #ifndef BENCHMARK /* Send it back */ System_printf("%d: Sending message Id #%d to core %d\n", arg0, msgId, MessageQ_getProcId(remoteQueueId)); #endif status = MessageQ_put(remoteQueueId, getMsg); if (status != MessageQ_S_SUCCESS) { System_abort("MessageQ_put had a failure/error\n"); } msgId++; } MessageQ_delete(&messageQ); numTests += NUMLOOPS; System_printf("Test thread %d complete!\n", arg0); }
/* * ======== serverTask ======== */ Void serverTask(UArg arg0, UArg arg1) { MessageQ_Handle serverMessageQ; MessageQ_QueueId replyQueue; MessageQ_Msg msg; UInt16 msgId; Int status; serverMessageQ = MessageQ_create(SERVERNAME, NULL); /* Loop forever processing requests */ System_printf("Server is ready to set processing requests\n"); while (TRUE) { /* Wait for a request. */ status = MessageQ_get(serverMessageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("Stopping test\n"); } /* Get the id and increment it to send back as validation */ msgId = MessageQ_getMsgId(msg); msgId += NUMCLIENTS; MessageQ_setMsgId(msg, msgId); /* Use the embedded reply destination */ replyQueue = MessageQ_getReplyQueue(msg); /* Send the response back */ status = MessageQ_put(replyQueue, msg); if (status < 0) { System_abort("MessageQ_put was not successful\n"); } } }
/* * ======== tsk0_func ======== * Allocates a message and ping-pongs the message around the processors. * A local message queue is created and a remote message queue is opened. * Messages are sent to the remote message queue and retrieved from the * local MessageQ. */ Void tsk0_func(UArg arg0, UArg arg1) { MessageQ_Msg msg; MessageQ_Handle messageQ; MessageQ_QueueId remoteQueueId; Int status; UInt16 msgId = 0; HeapBufMP_Handle heapHandle; HeapBufMP_Params heapBufParams; if (MultiProc_self() == 0) { /* * Create the heap that will be used to allocate messages. */ HeapBufMP_Params_init(&heapBufParams); heapBufParams.regionId = 0; heapBufParams.name = HEAP_NAME; heapBufParams.numBlocks = 1; heapBufParams.blockSize = sizeof(MessageQ_MsgHeader); heapHandle = HeapBufMP_create(&heapBufParams); if (heapHandle == NULL) { System_abort("HeapBufMP_create failed\n" ); } } else { /* Open the heap created by the other processor. Loop until opened. */ do { status = HeapBufMP_open(HEAP_NAME, &heapHandle); /* * Sleep for 1 clock tick to avoid inundating remote processor * with interrupts if open failed */ if (status < 0) { Task_sleep(1); } } while (status < 0); } /* Register this heap with MessageQ */ MessageQ_registerHeap((IHeap_Handle)heapHandle, HEAPID); /* Create the local message queue */ messageQ = MessageQ_create(localQueueName, NULL); if (messageQ == NULL) { System_abort("MessageQ_create failed\n" ); } /* Open the remote message queue. Spin until it is ready. */ do { status = MessageQ_open(nextQueueName, &remoteQueueId); /* * Sleep for 1 clock tick to avoid inundating remote processor * with interrupts if open failed */ if (status < 0) { Task_sleep(1); } } while (status < 0); if (MultiProc_self() == 0) { /* Allocate a message to be ping-ponged around the processors */ msg = MessageQ_alloc(HEAPID, sizeof(MessageQ_MsgHeader)); if (msg == NULL) { System_abort("MessageQ_alloc failed\n" ); } /* * Send the message to the next processor and wait for a message * from the previous processor. */ System_printf("Start the main loop\n"); while (msgId < NUMLOOPS) { /* Increment...the remote side will check this */ msgId++; MessageQ_setMsgId(msg, msgId); System_printf("Sending a message #%d to %s\n", msgId, nextQueueName); /* send the message to the remote processor */ status = MessageQ_put(remoteQueueId, msg); if (status < 0) { System_abort("MessageQ_put had a failure/error\n"); } /* Get a message */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("This should not happen since timeout is forever\n"); } } } else { /* * Wait for a message from the previous processor and * send it to the next processor */ System_printf("Start the main loop\n"); while (TRUE) { /* Get a message */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("This should not happen since timeout is forever\n"); } System_printf("Sending a message #%d to %s\n", MessageQ_getMsgId(msg), nextQueueName); /* Get the message id */ msgId = MessageQ_getMsgId(msg); /* send the message to the remote processor */ status = MessageQ_put(remoteQueueId, msg); if (status < 0) { System_abort("MessageQ_put had a failure/error\n"); } /* test done */ if (msgId >= NUMLOOPS) { break; } } } System_printf("The test is complete\n"); BIOS_exit(0); }
/* * ======== tsk1_func ======== * Send and receive messages */ Void tsk1_func(UArg arg0, UArg arg1) { MessageQ_Msg msg; MessageQ_Handle messageQ; MessageQ_QueueId remoteQueueId; HeapBufMP_Handle heapHandle; Int status; UInt16 msgId; /* * Open the heap that is created on the other processor. Loop until opened. * Not using the name since the interrupts are not enabled yet! */ do { status = HeapBufMP_open(HEAP_NAME, &heapHandle); /* * Sleep for 1 clock tick to avoid inundating remote processor * with interrupts if open failed */ if (status < 0) { Task_sleep(1); } } while (status < 0); /* Register this heap with MessageQ */ MessageQ_registerHeap((IHeap_Handle)heapHandle, HEAPID); /* Create a local message queue */ messageQ = MessageQ_create(DSP_MESSAGEQNAME, NULL); if (messageQ == NULL) { System_abort("MessageQ_create failed\n" ); } /* Open the core0 message queue. Spin until it is ready. */ do { status = MessageQ_open(CORE0_MESSAGEQNAME, &remoteQueueId); /* * Sleep for 1 clock tick to avoid inundating remote processor * with interrupts if open failed */ if (status < 0) { Task_sleep(1); } } while (status < 0); /* Wait for a message and send it to core0 */ System_printf("Start the main loop\n"); while (TRUE) { /* Get a message */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("This should not happen since timeout is forever\n"); } System_printf("Sending a message #%d to CORE0\n", MessageQ_getMsgId(msg)); /* Get the message id */ msgId = MessageQ_getMsgId(msg); /* send the message to the remote processor */ status = MessageQ_put(remoteQueueId, msg); if (status < 0) { System_abort("MessageQ_put had a failure/error\n"); } /* test done */ if (msgId == NUMLOOPS) { System_printf("Test complete!\n"); BIOS_exit(0); } } }
/* * ======== clientTask ======== */ Void clientTask(UArg arg0, UArg arg1) { MessageQ_Handle messageQ; MessageQ_QueueId serverQueue; MessageQ_Msg msg; UInt16 msgId = arg0; Int status; Int i; /* * Create client's MessageQ. * * Use 'NULL' for name since no since this queueId is passed by * referene and no one opens it by name. * Use 'NULL' for params to get default parameters. */ messageQ = MessageQ_create(NULL, NULL); if (messageQ == NULL) { System_abort("Failed to create MessageQ\n"); } /* Open the server's MessageQ */ do { status = MessageQ_open(SERVERNAME, &serverQueue); if (status < 0) { Task_sleep(1); /* give server a chance to create queue */ } } while (status < 0); msg = MessageQ_alloc(HEAPID, sizeof(MessageQ_MsgHeader)); if (msg == NULL) { System_abort("MessageQ_alloc failed\n" ); } /* Have the remote processor reply to this message queue */ MessageQ_setReplyQueue(messageQ, msg); /* Loop requesting information from the server task */ System_printf("Client #%d is starting to send requests\n", arg0); for (i = 0; i < NUMMSGS; i++) { /* Server will increment and send back */ MessageQ_setMsgId(msg, msgId); msgId += NUMCLIENTS; /* Send the message off */ status = MessageQ_put(serverQueue, msg); if (status < 0) { MessageQ_free(msg); System_abort("MessageQ_put failed\n"); } /* Wait for the reply... */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("MessageQ_get had an error\n"); } /* Validate the returned message. */ if (MessageQ_getMsgId(msg) != msgId) { System_abort("Unexpected value\n"); } System_printf("Client #%d received response #%d\n", arg0, i + 1); /* To make some variation in the execution order */ Task_sleep(500 * (arg0 + 1)); } System_printf("Client #%d is done sending requests\n", arg0); numCompleted++; if (numCompleted == 3) { /* All client tasks are done sending requests */ BIOS_exit(0); } }
/* * ======== tsk0_func ======== * Allocates a message and ping-pongs the message around the processors. * A local message queue is created and a remote message queue is opened. * Messages are sent to the remote message queue and retrieved from the * local MessageQ. */ Void tsk0_func(UArg arg0, UArg arg1) { MessageQ_Msg msg; MessageQ_Handle messageQ; MessageQ_QueueId remoteQueueId; Int status; UInt16 msgId = 0; Ptr buf; HeapBuf_Handle heapHandle; HeapBuf_Params hbparams; SizeT blockSize; UInt numBlocks; /* Compute the blockSize & numBlocks for the HeapBuf */ numBlocks = 2; blockSize = sizeof(MessageQ_MsgHeader); /* Alloc a buffer from the default heap */ buf = Memory_alloc(0, numBlocks * blockSize, 0, NULL); /* * Create the heap that is used for allocating MessageQ messages. */ HeapBuf_Params_init(&hbparams); hbparams.align = 0; hbparams.numBlocks = numBlocks; hbparams.blockSize = blockSize; hbparams.bufSize = numBlocks * blockSize; hbparams.buf = buf; heapHandle = HeapBuf_create(&hbparams, NULL); if (heapHandle == NULL) { System_abort("HeapBuf_create failed\n" ); } /* Register default system heap with MessageQ */ MessageQ_registerHeap((IHeap_Handle)(heapHandle), HEAPID); /* Create the local message queue */ messageQ = MessageQ_create(localQueueName, NULL); if (messageQ == NULL) { System_abort("MessageQ_create failed\n" ); } /* Open the remote message queue. Spin until it is ready. */ do { status = MessageQ_open(remoteQueueName, &remoteQueueId); /* * Sleep for 1 clock tick to avoid inundating remote processor * with interrupts if open failed */ if (status < 0) { Task_sleep(1); } } while (status < 0); if (MultiProc_self() == 0) { /* Allocate a message to be ping-ponged around the processors */ msg = MessageQ_alloc(HEAPID, sizeof(MessageQ_MsgHeader)); if (msg == NULL) { System_abort("MessageQ_alloc failed\n" ); } /* * Send the message to the remote processor and wait for a message * from the previous processor. */ System_printf("Start the main loop\n"); while (msgId < NUMLOOPS) { /* Increment...the remote side will check this */ msgId++; MessageQ_setMsgId(msg, msgId); System_printf("Sending a message #%d to %s\n", msgId, remoteQueueName); /* send the message to the remote processor */ status = MessageQ_put(remoteQueueId, msg); if (status < 0) { System_abort("MessageQ_put had a failure/error\n"); } /* Get a message */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("This should not happen since timeout is forever\n"); } } } else { /* * Wait for a message from the previous processor and * send it to the remote processor */ System_printf("Start the main loop\n"); while (TRUE) { /* Get a message */ status = MessageQ_get(messageQ, &msg, MessageQ_FOREVER); if (status < 0) { System_abort("This should not happen since timeout is forever\n"); } System_printf("Sending a message #%d to %s\n", MessageQ_getMsgId(msg), remoteQueueName); /* Get the message id */ msgId = MessageQ_getMsgId(msg); /* send the message to the remote processor */ status = MessageQ_put(remoteQueueId, msg); if (status < 0) { System_abort("MessageQ_put had a failure/error\n"); } /* test done */ if (msgId >= NUMLOOPS) { break; } } } System_printf("The test is complete\n"); BIOS_exit(0); }
static Void * pingThreadFxn(void *arg) { Int threadNum = *(int *)arg; Int32 status = 0; MessageQ_Msg msg = NULL; MessageQ_Params msgParams; UInt16 i; MessageQ_Handle handle; MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ; char remoteQueueName[64]; char hostQueueName[64]; printf ("Entered pingThreadFxn: %d\n", threadNum); sprintf(remoteQueueName, "%s_%d%d", SLAVE_MESSAGEQNAME, threadNum, (threadNum % (MultiProc_getNumProcessors() - 1)) + 1); sprintf(hostQueueName, "%s_%d", HOST_MESSAGEQNAME, threadNum ); /* Create the local Message Queue for receiving. */ MessageQ_Params_init (&msgParams); handle = MessageQ_create (hostQueueName, &msgParams); if (handle == NULL) { printf ("Error in MessageQ_create\n"); goto exit; } else { printf ("thread: %d, Local Message: %s, QId: 0x%x\n", threadNum, hostQueueName, MessageQ_getQueueId(handle)); } /* Poll until remote side has it's messageQ created before we send: */ do { status = MessageQ_open (remoteQueueName, &queueId); sleep (1); } while (status == MessageQ_E_NOTFOUND); if (status < 0) { printf ("Error in MessageQ_open [0x%x]\n", status); goto cleanup; } else { printf ("thread: %d, Remote queue: %s, QId: 0x%x\n", threadNum, remoteQueueName, queueId); } printf ("\nthread: %d: Exchanging messages with remote processor...\n", threadNum); for (i = 0 ; i < numLoops ; i++) { /* Allocate message. */ msg = MessageQ_alloc (HEAPID, MSGSIZE); if (msg == NULL) { printf ("Error in MessageQ_alloc\n"); break; } MessageQ_setMsgId (msg, i); /* Have the remote proc reply to this message queue */ MessageQ_setReplyQueue (handle, msg); status = MessageQ_put (queueId, msg); if (status < 0) { printf ("Error in MessageQ_put [0x%x]\n", status); break; } status = MessageQ_get(handle, &msg, MessageQ_FOREVER); if (status < 0) { printf ("Error in MessageQ_get [0x%x]\n", status); break; } else { /* Validate the returned message. */ if ((msg != NULL) && (MessageQ_getMsgId (msg) != i)) { printf ("Data integrity failure!\n" " Expected %d\n" " Received %d\n", i, MessageQ_getMsgId (msg)); break; } status = MessageQ_free (msg); } printf ("thread: %d: Exchanged %d msgs\n", threadNum, (i+1)); } printf ("thread: %d: pingThreadFxn successfully completed!\n", threadNum); MessageQ_close (&queueId); cleanup: /* Clean-up */ status = MessageQ_delete (&handle); if (status < 0) { printf ("Error in MessageQ_delete [0x%x]\n", status); } exit: return ((void *)status); }