Page 1 of 1

Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 8:38 am
by rumata
Hi,
I'm seeing a very weird problem related to the priorities of the threads. I tried to simplify the setup that I have, and here is what I got:

Code: Select all

#include "ch.hpp"
#include "hal.h"
#include "test.h"

using namespace chibios_rt;

#define THREADA_PRIO  NORMALPRIO-10
#define THREADB_PRIO  NORMALPRIO-10
#define MASTERTHREAD_PRIO NORMALPRIO-9

class ThreadA: public EnhancedThread<256>
{
public:
    ThreadA() : EnhancedThread<256>("ThreadA", THREADA_PRIO)
    {  }
protected:
    virtual msg_t Main(void)
    {
        while (true) {
            chThdSleepMilliseconds(50);
        }
        return 0;
    }
};
class ThreadB: public EnhancedThread<256>
{
public:
    ThreadB() : EnhancedThread<256>("ThreadB", THREADB_PRIO)
    {  }
protected:
    virtual msg_t Main(void)
    {
        while (true) {
            chThdSleepMilliseconds(50);
        }
        return 0;
    }
};
class MasterThread : public EnhancedThread<2048>
{
public:
    MasterThread()
      : EnhancedThread<2048>("MasterThread", MASTERTHREAD_PRIO)
    {  }
protected:
    virtual msg_t Main(void)
    {
        ThreadA ta;
        ThreadB tb;
        while (true) {
            chThdSleepMilliseconds(50);
        }
        return 0;
    }
};

int main(void)
{
    halInit();
    System::Init();

    MasterThread mthread;
    while (true) {
        chThdSleepMilliseconds(100);
    }
    return 0;
}



The whole thing basically consists of three threads plus the main thread. The main thread starts a master thread which then starts two child threads. Now I want the child threads to have priority of master thread -1, I know that priorities are relative so i hope it's safe to expect that changing the priorities THREADA_PRIO, THREADB_PRIO and MASTERTHREAD_PRIO that I have in the code above by a constant shouldn't really change the behavior of the system.
Now the problem is that if I set these priorities as in the code, the system works as expected. However if I add say 20 to each of these priorities (preserving the differences) and so making them all above NORMALPRIO, the system doesn't work - only the main while loop works, none of the other three threads gets started.

I can try to debug to see what's wrong, I just want to make sure I'm not missing anything... To me this code looks ok, I don't see why this should be happening, seems like there is a bug in the code that creates/starts a new thread that has a higher priority than the creator itself.... I may be wrong though.

I running this on STM32F4Discovery and using trunk r3925.

Re: Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 9:07 am
by rumata
I did a little bit of digging, and here is one suspect:
All my thread classes are derived from EnhancedThread<xxx> which itself is derived from BaseThread. Now BaseThread has a virtual function Main() which supposed to be reimplemented in the the derived classes, this all is fine. However what is not good (I'd say it's pretty bad in fact :) ) is that the function Main is called from the constructor of BaseThread. I guess the intention is to start the thread immediately after creation.... The intention is fine of course but the problem is that in C++ you can't call a virtual function from a constructor of the base class!

Need to do a little more debugging, but I guess what might be happening here is the following:
- when the priority of the thread-to-be-created is lower than the creator, the scheduler doesn't switch to it immediately, it just creates the thread and pushes it into the list. Then when appropriate, it pops the new thread from the list and starts execution. At that time the object is completely constructed and the invoked Main() is the right one.
- on the other hand when the new thread has higher priority, the scheduler switches to the new task right away, the thread is still half-constructed and the invoked Main() is the one of the base class and not the one you would expect....

Re: Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 9:13 am
by Giovanni
The relative priorities should not matter, there are a lot of examples of threads creating threads at higher priority.

I remember a strange issue with the C++ wrapper, threads whose constructor had no parameters were not started, the compiler did not generate the call to the constructor at all. It was discussed on the forum a while ago (it is possible it was the old forum on SF).

The fix was to make the constructor have a parameter.

Is it possible this is the same issue?

Giovanni

Re: Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 9:15 am
by Giovanni

Re: Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 9:34 am
by rumata
Giovanni wrote:The relative priorities should not matter, there are a lot of examples of threads creating threads at higher priority.

Calling a virtual function from the base class constructor is a problem. It may not always be visible since in this case the call seems to be indirect, but it looks like a potential issue...
And this seems to be a c++ wrapper thing, c threads creating higher priority c threads are not be affected...

Giovanni wrote:I remember a strange issue with the C++ wrapper, threads whose constructor had no parameters were not started, the compiler did not generate the call to the constructor at all. It was discussed on the forum a while ago (it is possible it was the old forum on SF).
The fix was to make the constructor have a parameter.
Is it possible this is the same issue?
Giovanni


I will give it a try, though I don't see how adding a dummy parameter to a constructor can possible help :). There is of course the possibility of a bug in gcc, but it's less likely I guess?

Thank you.

Re: Strange issue with thread priorities...

Posted: Mon Feb 06, 2012 9:48 am
by Giovanni
More than possible, I just hit a huge bug last week, probably this one: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49140

It prevented me committing a kernel improvement I am working on (GCC 4.5.2 is not affected, just 4.6.x).

Giovanni

Re: Strange issue with thread priorities...

Posted: Tue Feb 07, 2012 7:25 am
by rumata
Yep, I was right.
You can put a breakpoint in BaseThread::Main(void) or put an assert if you want to be more brutal. If you start a thread with lower priority, everything works as expected, the breakpoint/assert doesn't hit. However If you make the priority higher than NORMALPRIO, you get the breakpoint/assert. In fact here is even smaller piece of code that reproduces the issue: set a breakpoint and switch between the priorities...

Code: Select all

#include "ch.hpp"
#include "hal.h"
#include "test.h"
using namespace chibios_rt;

#define MASTERTHREAD_PRIO NORMALPRIO-10   // works
//#define MASTERTHREAD_PRIO NORMALPRIO+10 // doesn't work

class MasterThread : public EnhancedThread<256>
{
public:
    MasterThread()
      : EnhancedThread<256>("MasterThread", MASTERTHREAD_PRIO)
    {
    }
protected:
    virtual msg_t Main(void)
    {
        while (true) {
            chThdSleepMilliseconds(50);
        }
        return 0;
    }
};
int main(void)
{
    halInit();
    System::Init();
    MasterThread mthread;
    while (true) {
        chThdSleepMilliseconds(100);
    }
    return 0;
}


I know you are planning to redesign/reimplement c++ wrappers, this is I guess something worth considering.