Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
unittest for thread multiplexed fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinNowak committed May 29, 2011
1 parent f7e7eae commit 388e0f4
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions src/core/thread.d
Expand Up @@ -3801,6 +3801,8 @@ private:

version( unittest )
{
import core.atomic;

class TestFiber : Fiber
{
this()
Expand All @@ -3817,6 +3819,7 @@ version( unittest )
}
}

enum expSum = 1000 * 999 / 2;
size_t sum;
}

Expand All @@ -3838,22 +3841,71 @@ version( unittest )
}
} while (cont);

enum expSum = 1000 * 999 / 2;
foreach(fib; fibs)
{
assert(fib.sum == expSum);
assert(fib.sum == TestFiber.expSum);
}
}

// Single thread running separate fibers
unittest
{
runTen();
Thread[4] threads;
foreach(ref thr; threads)
}

// Multiple threads running separate fibers
unittest
{
foreach(_; 0 .. 4)
{
auto thr = new Thread(&runTen);
thr.start();
}
thread_joinAll();
}

// Multiple threads running shared fibers
unittest
{
shared bool[10] locks;
TestFiber[10] fibs;

void runShared()
{
thr = new Thread(&runTen);
bool cont;
do {
cont = false;
foreach(idx; 0 .. 10)
{
if (cas(&locks[idx], false, true))
{
if (fibs[idx].state == Fiber.State.HOLD)
{
fibs[idx].call();
cont |= fibs[idx].state != Fiber.State.TERM;
}
locks[idx] = false;
}
else
{
cont = true;
}
}
} while (cont);
}

foreach(ref fib; fibs)
fib = new TestFiber();

foreach(_; 0 .. 4)
{
auto thr = new Thread(&runShared);
thr.start();
}
thread_joinAll();

foreach(fib; fibs)
assert(fib.sum == TestFiber.expSum);
}
}

Expand Down

0 comments on commit 388e0f4

Please sign in to comment.