summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Kamm <christian.d.kamm@nokia.com>2010-03-23 11:18:23 +0100
committerChristian Kamm <christian.d.kamm@nokia.com>2010-03-26 13:05:03 +0100
commitf83809134d699ae3e0056eb3b674d657bd36c638 (patch)
treecad985db13e2b5537e0abff467c50191ebefc00c
parent86153c191a6e0a6228d6d6c705a3e3271a4239be (diff)
Create a default stack if the user doesn't.
-rw-r--r--src/coroutine.cpp27
-rw-r--r--tests/auto/basic/tst_basic.cpp1
-rw-r--r--tests/auto/build/tst_build.cpp2
3 files changed, 17 insertions, 13 deletions
diff --git a/src/coroutine.cpp b/src/coroutine.cpp
index 9c58e2d..80b007e 100644
--- a/src/coroutine.cpp
+++ b/src/coroutine.cpp
@@ -15,12 +15,6 @@
on them. Alternatively, it is possible to derive from Coroutine and overriding
the run() method.
- After creation it is necessary to set up the coroutine's stack. Call either
- createStack() or setStack() to do so. createStack() will make the coroutine allocate
- some stack space that it will release on destruction. Using setStack() allows
- passing in memory that the coroutine will not take ownership of. The latter
- is useful if you have a lot of coroutines and want to reuse their stack memory.
-
A coroutine doesn't start execution when it is built. Call cont() to run it.
This will execute the coroutine's code until it calls Coroutine::yield().
At that point, the call to cont() returns. Subsequent calls to cont() will
@@ -35,11 +29,15 @@
}
Coroutine *c = Coroutine::build(&myCoroutine);
- c->createStack();
qDebug() << "0.5";
c.cont(); // prints 1
qDebug() << "1.5";
c.cont(); // prints 2
+
+ By default, a Coroutine will create its own stack space using createStack()
+ with the default argument. To manage the stack memory manually or use
+ a stack of a different size, use createStack() or setStack() before starting
+ execution.
*/
/*!
@@ -50,8 +48,6 @@
pointer to functor, object and member function pointer, or pointer to object
and member function pointer. In the case of passing functor pointers or
object pointers, the Coroutine object doesn't take ownership.
-
- The coroutine will be ready for use after it's stack has been set up.
*/
@@ -84,6 +80,8 @@ Coroutine::~Coroutine()
void Coroutine::createStack(int size)
{
+ Q_ASSERT(_status == NotStarted);
+
if (_stackData)
free(_stackData);
@@ -93,6 +91,13 @@ void Coroutine::createStack(int size)
void Coroutine::setStack(void *memory, int size)
{
+ Q_ASSERT(_status == NotStarted);
+
+ if (_stackData) {
+ free(_stackData);
+ _stackData = 0;
+ }
+
initializeStack(memory, size, &entryPoint, &_stackPointer);
}
@@ -124,7 +129,9 @@ bool Coroutine::cont()
{
Q_ASSERT(_status == NotStarted || _status == Stopped);
Q_ASSERT(!_previousCoroutine);
- Q_ASSERT(_stackPointer);
+
+ if (!_stackPointer)
+ createStack();
_status = Running;
diff --git a/tests/auto/basic/tst_basic.cpp b/tests/auto/basic/tst_basic.cpp
index 8ac7958..c33ef40 100644
--- a/tests/auto/basic/tst_basic.cpp
+++ b/tests/auto/basic/tst_basic.cpp
@@ -25,7 +25,6 @@ public:
void tst_basic::noYield()
{
NoYieldCoro coro;
- coro.createStack();
coro.i = 0;
QCOMPARE(coro.status(), Coroutine::NotStarted);
QCOMPARE(coro.cont(), false);
diff --git a/tests/auto/build/tst_build.cpp b/tests/auto/build/tst_build.cpp
index 3b94169..aa7243d 100644
--- a/tests/auto/build/tst_build.cpp
+++ b/tests/auto/build/tst_build.cpp
@@ -33,7 +33,6 @@ static void fnArg(int start)
void tst_build::staticFn()
{
Coroutine* c1 = Coroutine::build(&fnNoArg);
- c1->createStack();
QCOMPARE(fnCounter, -99);
QCOMPARE(c1->cont(), true);
QCOMPARE(fnCounter, 0);
@@ -44,7 +43,6 @@ void tst_build::staticFn()
delete c1;
Coroutine* c2 = Coroutine::build(&fnArg, 40);
- c2->createStack();
QCOMPARE(c2->cont(), true);
QCOMPARE(fnCounter, 40);
QCOMPARE(c2->cont(), true);