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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

Jump to solution
583 Views
UnisvrTim
Contributor III

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);
...

0 Kudos
1 Solution
326 Views
UnisvrTim
Contributor III

Switched to CW10.2 (was 10.1) and MQX 3.8 (was MQX 3.7) seemed solved the problem!

 

problems in the old setup:

I. I can not build EWL library in CW10.1. The default project setting does nothing after I clicked 'build project'

2. No pre-emption in RR scheduler in MQX 3.7

 

Correct me if I'm wrong:

For RR scheduler to work (pre-empting blocking function call), the task that start other tasks must have lower priority (larger priority number) than the other tasks listed in the task template.

 

 

View solution in original post

0 Kudos
1 Reply
327 Views
UnisvrTim
Contributor III

Switched to CW10.2 (was 10.1) and MQX 3.8 (was MQX 3.7) seemed solved the problem!

 

problems in the old setup:

I. I can not build EWL library in CW10.1. The default project setting does nothing after I clicked 'build project'

2. No pre-emption in RR scheduler in MQX 3.7

 

Correct me if I'm wrong:

For RR scheduler to work (pre-empting blocking function call), the task that start other tasks must have lower priority (larger priority number) than the other tasks listed in the task template.

 

 

0 Kudos