Increasing stack size seems to cause failure
Posted: Fri Feb 04, 2011 10:31 am
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!
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;
}