Threads switching problem

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Adam
Posts: 18
Joined: Thu Sep 05, 2013 1:53 pm

Threads switching problem

Post by Adam »

Hello,

My code has many threads as lwip stack, http_server, UDP sender, UDP receiver etc. Everything worked fine. Until...:

I created a simple Thread, where is only infinite loop with variable test:

Code: Select all

WORKING_AREA(waThread_485toETH, 1024);
msg_t Thread_485toETH(void *arg) {

  while(1){
    if(ptr_var != NULL) {do some stuff}
  }
  return (msg_t)0;
}


MCU works and dont freeze, but in debuuger it looks like he won´ts yield a control to another thread. Another threads are(look like?) stopped and solution is putting some delay into the loop. Delay in loop is not wantable, the thread is necessary to be "every-ready".

How can I fix this problem? Thank you very much for your help;)

Adam
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Threads switching problem

Post by Giovanni »

Hi,

That thread would block any thread at lower priority because it never releases the CPU. Working as intended, probably you just need to redefine the priorities of your threads.

Giovanni
Adam
Posts: 18
Joined: Thu Sep 05, 2013 1:53 pm

Re: Threads switching problem

Post by Adam »

Hi Giovanni,

thanks for your reply.

The thread "Thread_485toETH" has LOWPRIO priority. Other threads have NORMALPRIO priority. I tried "chThdYield()" at the end of loop, but problem is still here:(

Adam
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Threads switching problem

Post by Giovanni »

There is some other kind of problem then, are you running with all debug options enabled? is something caught?

Giovanni
Adam
Posts: 18
Joined: Thu Sep 05, 2013 1:53 pm

Re: Threads switching problem

Post by Adam »

Hi,

I had turned on all debug options and I didnt catch anything about threads switching problem. Then I tried redefine all priorities of the all threads again and seems to be working.

Another question: When I had turned on all debug options, system is halted on chMtxTryLock() and the reason is "dbg_isr_cnt = 1". chMtxLock is located in "rxchar()" callback of UART driver. What does it mean "dbg_isr_cnt" and where is set? I found only this description: "dbg_isr_cnt ... ISR nesting level".
- Does my function trying to lock again, when the mutex is already locked?
- Can I use mutexes in callback?

I´m sorry of my english and maybe my stupid questions.

Adam.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Threads switching problem

Post by Giovanni »

Hi,

When the system is halted the global variable dbg_panic_msg points to a string that describes the problem or is an encoded error message, something like "SV#4".

After stopping the program retrieve the message. In chdebug.c there is the documentation of error codes.

Giovanni
Adam
Posts: 18
Joined: Thu Sep 05, 2013 1:53 pm

Re: Threads switching problem

Post by Adam »

Yes, the error code is "SV#4, misplaced @p chSysLock().", but I dont know, how to fix.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Threads switching problem

Post by Giovanni »

There are two possible causes for that error:
1) You call chSysLock() from an ISR or a callback, this can also happen by calling a normal API from ISR or callback, see your stack trace after stopping the program.
2) You have two consecutive calls to chSysLock(), the function cannot be called recursively.

Giovanni
Post Reply