AnsweredAssumed Answered

Win7-32+CW10+MQX3.7+TWR-K53n512: How to multi task with blocking function call?

Question asked by Tim Li on Apr 10, 2012
Latest reply on Apr 11, 2012 by Tim Li

Question: How to do round robin multi tasking with blocking function call?

If I start tcp task first, it will block at the RTCS_selectall while udp task never get started, and vice versa. The round robin scheduler doesn't seemed to work.

I also tried to set main task to round robin too and get no effect.

 

Please show me the direction of how to get around this problem. Thank you!

 

code snippet below:

 

1. user_config.h has #define MQX_HAS_TIME_SLICE             1


2. Task template:

TASK_TEMPLATE_STRUCT MQX_template_list[] = {

/*     Task number,   Entry point,    Stack,      Pri, String, Auto?,     create-param,         default time slice */

{ MAIN_TASK,  Main_task, 2000, 9, "main", MQX_AUTO_START_TASK ,  0, 0 },

{ UDPSVR_TASK,  udp_task, 2000, 9, "uspSVR", MQX_TIME_SLICE_TASK, 0, 100 },

{ TCP_TASK, tcp_task, 2000, 9, "tcpSVRF", MQX_TIME_SLICE_TASK,  0, 100 },

{ 0, 0, 0, 0, 0, 0, 0, 0 } };

 

3. tasks started with:

// start TCP port module
        task_id = _task_create(0, EMEIF_TASK, 0);
        if(task_id == MQX_NULL_TASK_ID) {
                printf("Could not create EMEIF_TASK\n");
        } else {
                printf("EMEIF_TASK created\n");
        }
               
        // start UDP port module
        task_id = _task_create(0, UDPSVR_TASK, 0);
        if(task_id == MQX_NULL_TASK_ID) {
                printf("Could not create UDPSVR_TASK\n");
        } else {
                printf("UDPSVR_TASK created\n");
        }

 

4. tcp task

Tcptask.c
...
        sockfd = socket(PF_INET, SOCK_STREAM, 0);
        if (sockfd == RTCS_SOCKET_ERROR) {
                printf("emeIF failed to create socket\n");
                _task_block();
        }
        laddr.sin_family = AF_INET;
        laddr.sin_port = 9100;
        laddr.sin_addr.s_addr = INADDR_ANY;
        rv = bind(sockfd, &laddr, sizeof(laddr));
        if (rv != RTCS_OK) {
                printf("emeIF failed to bind socket - 0x%1x\n", rv);
                _task_block();
        }

        if (listen(sockfd, BACKLOG) == -1) {
                perror("listen");
                exit(1);
        }

        while (1) { // main accept() loop
                sin_size = sizeof(raddr);
                if(RTCS_selectall(50) == 0) {   //*** always blocked at this line, time-out will not release CPU either
                        _time_delay(50);
                        printf(".");
                        continue;
                }
                printf("\n");
                new_fd = accept(sockfd, (sockaddr_in *)&raddr, &sin_size);
...


5. udp task

Udptask.c
...
        // create udp socket
        sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (sock == RTCS_SOCKET_ERROR) {
                printf("Failed to create udp socket\n");
                _task_block();
        }
        // bind socket
        laddr.sin_family = AF_INET;
        laddr.sin_port = 9000;
        laddr.sin_addr.s_addr = INADDR_ANY;
        error = bind(sock, &laddr, sizeof(laddr));
        if (error != RTCS_OK) {
                printf("Failed to bind udp socket - 0x%1x\n", error);
                _task_block();
        }

        // start waiting for message
        while (1) {
                printf("Waiting for DCT message ...");
                psock = RTCS_selectall(0);      // *** always blocked at this line, will not release CPU
                if(psock == sock)
                {
                        printf("Incoming ...\n");
                        memset(&raddr, 0, sizeof(raddr));
                        rlen = sizeof(raddr);
                        length = recvfrom(sock, inbuf, 255, 0, &raddr, &rlen);
...

Outcomes