Increasing stack size seems to cause failure

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

Moderators: RoccoMarco, lbednarz, tfAteba

JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Increasing stack size seems to cause failure

Post by JetForMe »

I created what I thought was a clever C++ thread class "CThread". It uses a template parameter to establish the stack size. I created a subclass for blinking an LED, and had another thread, created with the C API, also running. Everything was fine.

Then I started making a few more CThread subclasses, and the system kept resetting itself shortly after beginning execution (indicative of thread stacks being to small). So I started removing code, and finally got down to just my blinky LED thread. What I found was that if I make that thread's working area size be large (say, 256), it causes problems. If I make it small (say, 64), it works.

Here are four source files. Two are the CThread class (which has a BaseThread base class), and two are the BlinkThread class. I've stripped out almost everything else from my project. As it stands, it uses only 37% of the available RAM in the ATmega644A. If I declare BlinkThead : public CThread<64>, it works. If I increase that to 256, it fails.

Any ideas? Thanks!

Code: Select all

/**
    CThread.h
    TreasureBox
   
    Created by Roderick Mann on 2/3/11.
    Copyright 2011 Latency: Zero. All rights reserved.

*/

#ifndef __CThread_h__
#define __CThread_h__


//
//  Third-party Includes
//

#include "ch.h"





/**
*/

class
BaseThread
{
public:
                            BaseThread(uint8_t inStackSize);
                           
    void                    start(tprio_t inPriority = NORMALPRIO);
   
protected:
    virtual stkalign_t*     getWorkingArea()                { return NULL; }
    static  msg_t           ThreadEntry(void* inArg);
    virtual msg_t           entry();
   
    uint8_t                 mStackSize;
};

inline
msg_t
BaseThread::ThreadEntry(void* inArg)
{
    BaseThread* self = reinterpret_cast<BaseThread*> (inArg);
    return self->entry();
}

/**
*/

template<int inStackSize>
class
CThread : public BaseThread
{
public:
                            CThread()
                                :
                                BaseThread(inStackSize)
                            {
                            }
                           
protected:
    virtual stkalign_t*     getWorkingArea()                { return mWorkingArea; }
   
private:
    WORKING_AREA(mWorkingArea, inStackSize);
};



#endif  //  __CThread_h__


Code: Select all

/**
    CThread.cpp
    TreasureBox
   
    Created by Roderick Mann on 2/3/11.
    Copyright 2011 Latency: Zero. All rights reserved.
*/

#include "CThread.h"



BaseThread::BaseThread(uint8_t inStackSize)
    :
    mStackSize(inStackSize)
{
}

msg_t
BaseThread::entry()
{
    return 0;
}

void
BaseThread::start(tprio_t inPriority)
{
    chThdCreateStatic(getWorkingArea(),
                        mStackSize,
                        inPriority,
                        ThreadEntry,
                        this);
}


Code: Select all

/**
    BlinkThread.h
    TreasureBox
   
    Created by Roderick Mann on 2/3/11.
    Copyright 2011 Latency: Zero. All rights reserved.

*/

#ifndef __BlinkThread_h__
#define __BlinkThread_h__

#include "CThread.h"




/**
*/

class
BlinkThread : public CThread<256>
{
public:

protected:
    virtual msg_t       entry();

private:
};



#endif  //  __BlinkThread_h__



Code: Select all

/**
    BlinkThread.cpp
    TreasureBox
   
    Created by Roderick Mann on 2/3/11.
    Copyright 2011 Latency: Zero. All rights reserved.
*/

#include "BlinkThread.h"

msg_t
BlinkThread::entry()
{
    while (true)
    {
        PORTA |= 0x01;
        chThdSleepMilliseconds(50);
        PORTA &= ~0x01;
        chThdSleepMilliseconds(950);
    }
   
    return 0;
}
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

I may have answered my own question. There was an error in that I was using the supplied stack size template parameter in the call to chThdCreateStatic(), rather than the working area size, which is somewhat larger. It's not clear to me exactly why this would fail, but adjusting for the overhead, it seems to be working better. I'll try re-enabling the other threads to see if that's true.

EDIT: And, the answer is NOPE. Still doesn't work. If I start the second thread, everything fails.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Increasing stack size seems to cause failure

Post by Giovanni »

I never tested C++ on the AVR, not sure.

There is a C++ wrapper for ChibiOS under ./os/various but it has only been tested on ARM. You may try to start from that code and see if there are problems on AVR. There is also a C++ demo for LPC2148 in the distribution.

Giovanni
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

That wrapper is a little thin, although I'm not really doing much more. After fixing the stack size computation error, increasing the size doesn't cause the single thread to fail, but the other threads still don't work.

Is there a facility for detecting stack overflow, or other robustness checks I can apply to my working code? I see some debugging code in the thread code...
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Increasing stack size seems to cause failure

Post by Giovanni »

There are several debug switches you can enable but stack checking is not implemented in the AVR port, it should be easy enough to implement, it is just that the AVR port received very little interest until now and I tend to focus development where there is feedback.

Do you use library functions in your threads? those can use a lot of stack space.

What I can recommend is to enable assertions and parameters check into chconf.h, strip your code of everything and then start to re-enable parts incrementally.

Giovanni
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

I'm not doing anything I hadn't already done before, except now I'm using my new CThread class. I was calling snprintf, among other things, but even removing that, things fail.

I stripped everything down to nothing and it still fails. That is, a single thread works (I guess there's an implicit "main" thread as well?). I'll try using the debugging features, see where that gets me.
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

Okay, I've verified that individually, and together, my two threads can both call snprintf() successfully. But only if their stack size is about 64 or 128 (the actual working area size is larger). If I increase either or both threads to 256, they fail.

I guess the next step is to reproduce this behavior with threads created only with the C API.
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

SOLVED IT!

It was my error. I had to store away the working area size, and I was storing it in a uint8_t. As soon as my working area size got big enough to exceed this, the call to the thread create function was passing a bad value.

Interestingly, the compiler generated no warnings about the attempt to assign an int to a uint8_t.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Increasing stack size seems to cause failure

Post by Giovanni »

I think -Wall -Wextra enable all the warnings, try to add them to the makefile.

Giovanni
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Increasing stack size seems to cause failure

Post by JetForMe »

I already had -Wall, adding -Wextra didn't seem to generate any more warnings.
Post Reply